Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

Add selfdescribing test #407

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions core/src/test/scala/pickling/run/self-describing-test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package scala.pickling.runtime

import org.scalatest.FunSuite
import scala.pickling.{Ops, Pickler, Unpickler}
import scala.pickling.pickler.AllPicklers
import scala.pickling.binary.{BinaryFormats, BinaryPickleArray}
import scala.pickling.internal.{HybridRuntime}

abstract class SDPicklingLogic extends Ops with AllPicklers with BinaryFormats

object SDPicklingProtocol extends {
val ignoreMe = scala.pickling.internal.replaceRuntime(new HybridRuntime)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is problematic, since it makes several other tests break. It appears that it is not necessary anyway for this test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is really necessary that HybridRuntime is enabled, otherwise it will use runtime reflection to unpickle.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah, you'll have to create anew project to run tests off of. Make the new project depend on "core" and It can have this new protocol.

The issue is, runtime is a "global" so while you can replace it, we'll need different JVMs to test the various runtime options.

It'd be good to have a hybrid test-project anyway, so if you need, I can create it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem @jsuereth, I'll take over it, thanks 👍

} with SDPicklingLogic {
implicit val so = scala.pickling.static.StaticOnly
}


final case class SelfDescribing(unpicklerClassName: String, blob: Array[Byte]) {
import SDPicklingProtocol._

def result(): Any = {
val unpicklerInst = Class.forName(unpicklerClassName).newInstance().asInstanceOf[Unpickler[Any]]
val pickle = BinaryPickleArray(blob)
val reader = pickleFormat.createReader(pickle)
val typeString = reader.beginEntry()
reader.hintElidedType(unpicklerInst.tag)
unpicklerInst.unpickle(unpicklerInst.tag.key, reader)
}
}


sealed abstract class Message
case class Req[T](x: Int, y: T) extends Message
case class Foo[B](b: B)


class SelfDescribingTest extends FunSuite {

test("SelfDescribing should work") {

import SDPicklingProtocol._

val pickler = implicitly[Pickler[Foo[Req[List[String]]]]]
val unpickler = implicitly[Unpickler[Foo[Req[List[String]]]]]

println(s"found pickler of class ${pickler.getClass.getName}")
println(s"found unpickler of class ${unpickler.getClass.getName}")

val req = Foo(Req(5, List("hello")))
val p = req.pickle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So another possible way to handle this is with a custom pickler ONLY for the SelfDescribing class.

case class SelfDescribing[T](value: T)
object SelfDescribing {
   implicit def pickler[T](implicit up: PicklerUnpickler[T], tag: FastTypeTag[SelfDescribing[T]]])= {
      new PicklerUnpickler[SelfDescribing[T]] {
          def pickle = {
                ... pickle tag ...
                ... pickle the unpickler class ...
                ... literally just pickle the class naturally rather than as a blob...
            }
            def unpickle ... = {
               .. unpickle the tag ...
               ... check to see if we've unpickled this before, if so use from cache and ignore the unpickler,
                    otherwise extract the unpickler and instantiate it...
                ... Unpickle the entry directly with the unpickler ...
            }
            def tag = tag
   }
}

val sd = SelfDescribing(unpickler.getClass.getName, p.value)
val sdp = sd.pickle

val up = sdp.unpickle[SelfDescribing]
val res = up.result()
println(res)

assert(res === req)

}

}