2020-12-21 11:45:07 +00:00
|
|
|
import
|
|
|
|
unittest, json, chronicles,
|
2021-03-17 09:20:33 +00:00
|
|
|
../json_rpc/[rpcclient, rpcserver]
|
2020-12-21 11:45:07 +00:00
|
|
|
|
|
|
|
var srv = newRpcSocketServer(["localhost:8545"])
|
|
|
|
var client = newRpcSocketClient()
|
|
|
|
|
|
|
|
# Create RPC on server
|
|
|
|
srv.rpc("myProc") do(input: string, data: array[0..3, int]):
|
2021-03-17 09:20:33 +00:00
|
|
|
return %("Hello " & input & " data: " & $data)
|
|
|
|
|
|
|
|
srv.rpc("myError") do(input: string, data: array[0..3, int]):
|
|
|
|
raise (ref ValueError)(msg: "someMessage")
|
2020-12-21 11:45:07 +00:00
|
|
|
|
|
|
|
srv.start()
|
|
|
|
waitFor client.connect("localhost", Port(8545))
|
|
|
|
|
|
|
|
suite "Server/Client RPC":
|
2021-03-17 09:20:33 +00:00
|
|
|
test "Successful RPC call":
|
|
|
|
let r = waitFor client.call("myProc", %[%"abc", %[1, 2, 3, 4]])
|
|
|
|
check r.getStr == "Hello abc data: [1, 2, 3, 4]"
|
|
|
|
|
|
|
|
test "Missing params":
|
|
|
|
expect(CatchableError):
|
|
|
|
discard waitFor client.call("myProc", %[%"abc"])
|
|
|
|
|
|
|
|
test "Error RPC call":
|
|
|
|
expect(CatchableError): # The error type wont be translated
|
|
|
|
discard waitFor client.call("myError", %[%"abc", %[1, 2, 3, 4]])
|
2020-12-21 11:45:07 +00:00
|
|
|
|
|
|
|
srv.stop()
|
|
|
|
waitFor srv.closeWait()
|