forked from lihaoyi/autowire
/
InteropTests.scala
177 lines (163 loc) · 6.84 KB
/
InteropTests.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package autowire
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream}
import java.nio.ByteBuffer
import acyclic.file
import org.objenesis.strategy.StdInstantiatorStrategy
import utest.PlatformShims.await
import utest._
import utest.framework.ExecutionContext.RunNow
import scala.reflect.ClassTag
object InteropTests extends TestSuite {
val tests: Tests = utest.Tests {
test("default") {
import upickle.default._
implicit val pointReadWrite: ReadWriter[Point] = macroRW
object Bundle extends GenericClientServerBundle[String, Reader, Writer] {
def write[T: Writer](t: T): String = upickle.default.write(t)
def read[T: Reader](t: String): T = upickle.default.read[T](t)
def routes: Server.Router = Server.route[Api](Controller)
}
import Bundle.Client
val res1 = await(Client[Api].add(1, 2, 3).call())
val res2 = await(Client[Api].add(1).call())
val res3 = await(Client[Api].add(1, 2).call())
val res4 = await(Client[Api].multiply(x = 1.2, Seq(2.3)).call())
val res5 = await(Client[Api].multiply(x = 1.1, ys = Seq(2.2, 3.3, 4.4)).call())
val res6 = await(Client[Api].sum(Point(1, 2), Point(10, 20)).call())
assert(
res1 == "1+2+3",
res2 == "1+2+10",
res3 == "1+2+10",
res4 == "1.2*2.3",
res5 == "1.1*2.2*3.3*4.4",
res6 == Point(11, 22)
)
Bundle.transmitted.last
}
test("reflection") {
object Bundle extends GenericClientServerBundle[Array[Byte], Bounds.None, Bounds.None] {
def write[T: Bounds.None](t: T): Array[Byte] = {
val buffer = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(buffer)
oos.writeObject(t)
oos.flush()
oos.close()
buffer.toByteArray
}
def read[T: Bounds.None](s: Array[Byte]): T = {
val in = new ByteArrayInputStream(s)
val ois = new ObjectInputStream(in)
val obj = ois.readObject()
obj.asInstanceOf[T]
}
def routes: Bundle.Server.Router = Server.route[Api](Controller)
}
import Bundle.Client
val res1 = await(Client[Api].add(1, 2, 3).call())
val res2 = await(Client[Api].add(1).call())
val res3 = await(Client[Api].add(1, 2).call())
val res4 = await(Client[Api].multiply(x = 1.2, Seq(2.3)).call())
val res5 = await(Client[Api].multiply(x = 1.1, ys = Seq(2.2, 3.3, 4.4)).call())
val res6 = await(Client[Api].sum(Point(1, 2), Point(10, 20)).call())
assert(
res1 == "1+2+3",
res2 == "1+2+10",
res3 == "1+2+10",
res4 == "1.2*2.3",
res5 == "1.1*2.2*3.3*4.4",
res6 == Point(11, 22)
)
Bundle.transmitted.last
}
test("kryo") {
object Bundle extends GenericClientServerBundle[Array[Byte], ClassTag, ClassTag] {
val kryo = new com.esotericsoftware.kryo.Kryo()
kryo.setRegistrationRequired(false)
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy())
kryo.register(classOf[scala.collection.immutable.::[_]], 60)
def write[T: ClassTag](t: T): Array[Byte] = {
val output = new com.esotericsoftware.kryo.io.Output(new ByteArrayOutputStream())
kryo.writeClassAndObject(output, t)
output.toBytes
}
def read[T: ClassTag](s: Array[Byte]): T = {
val input = new com.esotericsoftware.kryo.io.Input(new ByteArrayInputStream(s))
val res = kryo.readClassAndObject(input).asInstanceOf[T]
res
}
def routes: Bundle.Server.Router = Server.route[Api](Controller)
}
import Bundle.Client
val res1 = await(Client[Api].add(1, 2, 3).call())
val res2 = await(Client[Api].add(1).call())
val res3 = await(Client[Api].add(1, 2).call())
// todo: Seq[Double] to Array[Byte] conversion seems to be broken (kryo problem?)
// val res4 = await(Client[Api].multiply(x = 1.2, Seq(2.3)).call())
// val res5 = await(Client[Api].multiply(x = 1.1, ys = Seq(2.2, 3.3, 4.4)).call())
// val res6 = await(Client[Api].sum(Point(1, 2), Point(10, 20)).call())
assert(
res1 == "1+2+3",
res2 == "1+2+10",
res3 == "1+2+10",
// res4 == "1.2*2.3",
// res5 == "1.1*2.2*3.3*4.4",
// res6 == Point(11, 22)
)
Bundle.transmitted.last
}
test("playJson") {
import play.api.libs.json._
implicit val pointReads = Json.reads[Point]
implicit val pointWrites = Json.writes[Point]
object Bundle extends GenericClientServerBundle[String, Reads, Writes] {
def write[T: Writes](t: T): String = Json.stringify(Json.toJson(t))
def read[T: Reads](s: String): T = Json.fromJson[T](Json.parse(s)).get
def routes: Bundle.Server.Router = Server.route[Api](Controller)
}
import Bundle.Client
val res1 = await(Client[Api].add(1, 2, 3).call())
val res2 = await(Client[Api].add(1).call())
val res3 = await(Client[Api].add(1, 2).call())
val res4 = await(Client[Api].multiply(x = 1.2, Seq(2.3)).call())
val res5 = await(Client[Api].multiply(x = 1.1, ys = Seq(2.2, 3.3, 4.4)).call())
val res6 = await(Client[Api].sum(Point(1, 2), Point(10, 20)).call())
assert(
res1 == "1+2+3",
res2 == "1+2+10",
res3 == "1+2+10",
res4 == "1.2*2.3",
res5 == "1.1*2.2*3.3*4.4",
res6 == Point(11, 22)
)
Bundle.transmitted.last
}
// Will work once a new version of booPickle with the following commit is published
// https://github.com/suzaku-io/boopickle/commit/4d92604ee3e83c018e87a76a561468635634afac
// See also: https://github.com/plasma-umass/doppio/issues/497#issuecomment-334740243
// test("booPickle") {
// import boopickle.Default._
// object Bundle extends GenericClientServerBundle[ByteBuffer, Pickler, Pickler] {
// def write[Result: Pickler](r: Result): ByteBuffer = Pickle.intoBytes(r)
// def read[Result: Pickler](p: ByteBuffer): Result = Unpickle.apply[Result].fromBytes(p)
// def routes: Bundle.Server.Router = Server.route[Api](Controller)
// }
// import Bundle.Client
//
// val res1 = await(Client[Api].add(1, 2, 3).call())
// val res2 = await(Client[Api].add(1).call())
// val res3 = await(Client[Api].add(1, 2).call())
// val res4 = await(Client[Api].multiply(x = 1.2, Seq(2.3)).call())
// val res5 = await(Client[Api].multiply(x = 1.1, ys = Seq(2.2, 3.3, 4.4)).call())
// val res6 = await(Client[Api].sum(Point(1, 2), Point(10, 20)).call())
// assert(
// res1 == "1+2+3",
// res2 == "1+2+10",
// res3 == "1+2+10",
// res4 == "1.2*2.3",
// res5 == "1.1*2.2*3.3*4.4",
// res6 == Point(11, 22)
// )
// Bundle.transmitted.last
// }
}
}