This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Add selfdescribing test #407
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
core/src/test/scala/pickling/run/self-describing-test.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
} | ||
|
||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍