2018-06-19 17:17:56 +00:00
|
|
|
import unittest, json, tables, chronicles
|
|
|
|
import ../rpcserver
|
2018-04-26 18:37:02 +00:00
|
|
|
|
2018-05-08 15:26:13 +00:00
|
|
|
type
|
|
|
|
# some nested types to check object parsing
|
|
|
|
Test2 = object
|
|
|
|
x: array[0..2, int]
|
|
|
|
y: string
|
|
|
|
|
|
|
|
Test = object
|
|
|
|
a: array[0..1, int]
|
|
|
|
b: Test2
|
|
|
|
|
|
|
|
MyObject* = object
|
|
|
|
a: int
|
|
|
|
b: Test
|
|
|
|
c: float
|
|
|
|
let
|
|
|
|
testObj = %*{
|
|
|
|
"a": %1,
|
|
|
|
"b": %*{
|
|
|
|
"a": %[5, 0],
|
|
|
|
"b": %*{
|
|
|
|
"x": %[1, 2, 3],
|
|
|
|
"y": %"test"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"c": %1.23}
|
|
|
|
|
2018-06-15 10:11:10 +00:00
|
|
|
var s = newRpcStreamServer(["localhost:8545"])
|
2018-05-08 15:26:13 +00:00
|
|
|
|
|
|
|
# RPC definitions
|
2018-05-16 22:16:20 +00:00
|
|
|
|
2018-06-07 07:02:14 +00:00
|
|
|
s.rpc("rpc.simplepath"):
|
2018-04-26 18:37:02 +00:00
|
|
|
result = %1
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-06-07 07:02:14 +00:00
|
|
|
s.rpc("rpc.differentparams") do(a: int, b: string):
|
2018-05-09 16:31:28 +00:00
|
|
|
result = %[%a, %b]
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.arrayparam") do(arr: array[0..5, byte], b: string):
|
2018-05-09 16:31:28 +00:00
|
|
|
var res = %arr
|
2018-04-26 18:37:02 +00:00
|
|
|
res.add %b
|
|
|
|
result = %res
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.seqparam") do(a: string, s: seq[int]):
|
2018-04-26 18:37:02 +00:00
|
|
|
var res = newJArray()
|
2018-05-08 15:26:13 +00:00
|
|
|
res.add %a
|
2018-04-26 18:37:02 +00:00
|
|
|
for item in s:
|
|
|
|
res.add %int(item)
|
|
|
|
result = res
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.objparam") do(a: string, obj: MyObject):
|
2018-04-26 18:37:02 +00:00
|
|
|
result = %obj
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-05-17 18:14:31 +00:00
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.returntypesimple") do(i: int) -> int:
|
2018-05-08 16:30:05 +00:00
|
|
|
result = i
|
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.returntypecomplex") do(i: int) -> Test2:
|
2018-05-08 16:30:05 +00:00
|
|
|
result.x = [1, i, 3]
|
|
|
|
result.y = "test"
|
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.testreturns") do() -> int:
|
2018-05-16 22:16:20 +00:00
|
|
|
return 1234
|
|
|
|
|
2018-05-08 15:26:13 +00:00
|
|
|
# Tests
|
2018-04-26 18:37:02 +00:00
|
|
|
suite "Server types":
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-04-26 18:37:02 +00:00
|
|
|
test "On macro registration":
|
|
|
|
check s.procs.hasKey("rpc.simplepath")
|
2018-05-08 19:36:13 +00:00
|
|
|
check s.procs.hasKey("rpc.differentparams")
|
|
|
|
check s.procs.hasKey("rpc.arrayparam")
|
|
|
|
check s.procs.hasKey("rpc.seqparam")
|
|
|
|
check s.procs.hasKey("rpc.objparam")
|
|
|
|
check s.procs.hasKey("rpc.returntypesimple")
|
|
|
|
check s.procs.hasKey("rpc.returntypecomplex")
|
2018-05-16 22:16:20 +00:00
|
|
|
check s.procs.hasKey("rpc.testreturns")
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-05-09 16:31:28 +00:00
|
|
|
test "Simple paths":
|
|
|
|
let r = waitFor rpcSimplePath(%[])
|
|
|
|
check r == %1
|
|
|
|
|
|
|
|
test "Different param types":
|
|
|
|
let
|
|
|
|
inp = %[%1, %"abc"]
|
|
|
|
r = waitFor rpcDifferentParams(inp)
|
|
|
|
check r == inp
|
|
|
|
|
2018-05-08 15:26:13 +00:00
|
|
|
test "Array parameters":
|
2018-04-26 18:37:02 +00:00
|
|
|
let r1 = waitfor rpcArrayParam(%[%[1, 2, 3], %"hello"])
|
|
|
|
var ckR1 = %[1, 2, 3, 0, 0, 0]
|
|
|
|
ckR1.elems.add %"hello"
|
|
|
|
check r1 == ckR1
|
|
|
|
|
2018-05-08 15:26:13 +00:00
|
|
|
test "Seq parameters":
|
2018-04-26 18:37:02 +00:00
|
|
|
let r2 = waitfor rpcSeqParam(%[%"abc", %[1, 2, 3, 4, 5]])
|
|
|
|
var ckR2 = %["abc"]
|
|
|
|
for i in 0..4: ckR2.add %(i + 1)
|
|
|
|
check r2 == ckR2
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-04-26 18:37:02 +00:00
|
|
|
test "Object parameters":
|
2018-05-08 15:26:13 +00:00
|
|
|
let r = waitfor rpcObjParam(%[%"abc", testObj])
|
|
|
|
check r == testObj
|
|
|
|
|
2018-05-08 16:30:05 +00:00
|
|
|
test "Simple return types":
|
|
|
|
let
|
|
|
|
inp = %99
|
|
|
|
r1 = waitfor rpcReturnTypeSimple(%[%inp])
|
|
|
|
check r1 == inp
|
|
|
|
|
|
|
|
test "Complex return types":
|
|
|
|
let
|
|
|
|
inp = 99
|
|
|
|
r1 = waitfor rpcReturnTypeComplex(%[%inp])
|
|
|
|
check r1 == %*{"x": %[1, inp, 3], "y": "test"}
|
|
|
|
|
2018-05-16 22:16:20 +00:00
|
|
|
test "Return statement":
|
|
|
|
let r = waitFor rpcTestReturns(%[])
|
|
|
|
check r == %1234
|
|
|
|
|
2018-04-26 18:37:02 +00:00
|
|
|
test "Runtime errors":
|
|
|
|
expect ValueError:
|
2018-05-08 19:36:13 +00:00
|
|
|
# root param not array
|
|
|
|
discard waitfor rpcArrayParam(%"test")
|
|
|
|
expect ValueError:
|
|
|
|
# too big for array
|
2018-04-26 18:37:02 +00:00
|
|
|
discard waitfor rpcArrayParam(%[%[0, 1, 2, 3, 4, 5, 6], %"hello"])
|
2018-05-08 19:36:13 +00:00
|
|
|
expect ValueError:
|
|
|
|
# wrong sub parameter type
|
|
|
|
discard waitfor rpcArrayParam(%[%"test", %"hello"])
|
2018-05-09 16:31:28 +00:00
|
|
|
expect ValueError:
|
|
|
|
# wrong param type
|
|
|
|
let res = waitFor rpcDifferentParams(%[%"abc", %1])
|
|
|
|
# TODO: When errors are proper return values, check error for param name
|
2018-06-07 08:20:33 +00:00
|
|
|
|
|
|
|
s.stop()
|
2018-06-08 06:31:51 +00:00
|
|
|
s.close()
|