Update tests to use async better, no shared server

This commit is contained in:
coffeepots 2018-06-05 10:45:54 +01:00
parent 1ea23b4df2
commit efeb972763
2 changed files with 66 additions and 46 deletions

View File

@ -1,48 +1,76 @@
import ../ rpcclient, ../ rpcserver import ../ rpcclient, ../ rpcserver
import unittest, asyncdispatch, json, tables import unittest, asyncdispatch, json, tables
import stint, ethtypes, ethprocs, stintjson
from os import getCurrentDir, DirSep from os import getCurrentDir, DirSep
from strutils import rsplit from strutils import rsplit
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0] template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
var srv = sharedRpcServer() var
srv.address = "localhost" server = newRpcServer()
srv.port = Port(8546) client = newRpcClient()
# importing ethprocs creates the server rpc calls ## Generate Ethereum server RPCs
import stint, ethtypes, ethprocs, stintjson server.addEthRpcs()
# generate all client ethereum rpc calls
## Generate client convenience marshalling wrappers from forward declarations
createRpcSigs(sourceDir & DirSep & "ethcallsigs.nim") createRpcSigs(sourceDir & DirSep & "ethcallsigs.nim")
srv.rpc("rpc.uint256param") do(i: UInt256): ## Create custom RPC with StUint input parameter
server.rpc("rpc.uint256param") do(i: UInt256):
let r = i + 1.stUint(256) let r = i + 1.stUint(256)
result = %r result = %r
srv.rpc("rpc.testreturnuint256") do() -> UInt256: ## Create custom RPC with StUInt return parameter
server.rpc("rpc.testreturnuint256") do() -> UInt256:
let r: UInt256 = "0x1234567890abcdef".parse(UInt256, 16) let r: UInt256 = "0x1234567890abcdef".parse(UInt256, 16)
return r return r
asyncCheck srv.serve proc testLocalCalls: Future[seq[JsonNode]] =
## Call RPCs created with `rpc` locally.
## This simply demonstrates async calls of the procs generated by the `rpc` macro.
var
uint256Param = rpcUInt256Param(%[%"0x1234567890"])
returnUint256 = rpcTestReturnUInt256(%[])
result = all(uint256Param, returnUint256)
suite "Ethereum RPCs": proc testRemoteUInt256: Future[seq[Response]] =
proc main {.async.} = ## Call function remotely on server, testing `stint` types
var client = newRpcClient() var
await client.connect("localhost", Port(8546)) uint256Param = client.call("rpc.uint256param", %[%"0x1234567890"])
returnUint256 = client.call("rpc.testreturnuint256", %[])
result = all(uint256Param, returnUint256)
test "UInt256 param": proc testSigCalls: Future[seq[string]] =
let r = waitFor rpcUInt256Param(%[%"0x1234567890"]) ## Remote call using proc generated from signatures in `ethcallsigs.nim`
check r == %"0x1234567891" var
version = client.web3_clientVersion()
sha3 = client.web3_sha3("0x68656c6c6f20776f726c64")
result = all(version, sha3)
test "Return UInt256": server.address = "localhost"
let r = waitFor rpcTestReturnUInt256(%[]) server.port = Port(8546)
check r == %"0x1234567890abcdef" asyncCheck server.serve
waitFor client.connect("localhost", Port(8546))
test "Version":
var
response = waitFor client.web3_clientVersion()
check response == "Nimbus-RPC-Test"
test "SHA3":
var response = waitFor client.web3_sha3("0x68656c6c6f20776f726c64")
check response == "0x47173285A8D7341E5E972FC677286384F802F8EF42A5EC5F03BBFA254CB01FAD"
waitFor main() suite "Local calls":
let localResults = testLocalCalls().waitFor
test "UInt256 param local":
check localResults[0] == %"0x1234567891"
test "Return UInt256 local":
check localResults[1] == %"0x1234567890abcdef"
suite "Remote calls":
let remoteResults = testRemoteUInt256().waitFor
test "UInt256 param":
check remoteResults[0].result == %"0x1234567891"
test "Return UInt256":
check remoteResults[1].result == %"0x1234567890abcdef"
suite "Generated from signatures":
let sigResults = testSigCalls().waitFor
test "Version":
check sigResults[0] == "Nimbus-RPC-Test"
test "SHA3":
check sigResults[1] == "0x47173285A8D7341E5E972FC677286384F802F8EF42A5EC5F03BBFA254CB01FAD"

View File

@ -1,28 +1,20 @@
import ../ rpcclient, ../ rpcserver import ../ rpcclient, ../ rpcserver
import unittest, asyncdispatch, json import unittest, json
var srv = newRpcServer() var srv = newRpcServer()
srv.address = "localhost" srv.address = "localhost"
srv.port = Port(8545) srv.port = Port(8545)
var client = newRpcClient()
proc makeProc(server: RpcServer) = # Create RPC on server
server.rpc("myProc") do(input: string, data: array[0..3, int]): srv.rpc("myProc") do(input: string, data: array[0..3, int]):
result = %("Hello " & input & " data: " & $data) result = %("Hello " & input & " data: " & $data)
asyncCheck srv.serve asyncCheck srv.serve
waitFor client.connect("localhost", Port(8545))
srv.makeProc # TODO: When an error occurs during a test, stop the server
suite "Server/Client RPC": suite "Server/Client RPC":
proc main {.async.} = test "Custom RPC":
var client = newRpcClient() var r = waitFor client.call("myProc", %[%"abc", %[1, 2, 3, 4]])
await client.connect("localhost", Port(8545)) check r.result.getStr == "Hello abc data: [1, 2, 3, 4]"
test "Custom RPC":
# Custom async RPC call
var response = waitFor client.call("myProc", %[%"abc", %[1, 2, 3, 4]])
check response.result.getStr == "Hello abc data: [1, 2, 3, 4]"
# TODO: When an error occurs during a test, stop the server
asyncCheck main()