Skip to content

Commit

Permalink
Fix all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zah committed Mar 17, 2020
1 parent d19de19 commit f53e0b9
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 44 deletions.
3 changes: 2 additions & 1 deletion json_rpc/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ proc rpcCallNode*(path: string, params: JsonNode, id: ClientId): JsonNode =
%{"jsonrpc": %"2.0", "method": %path, "params": params, "id": %id}

method call*(client: RpcClient, name: string,
params: JsonNode): Future[Response] {.gcsafe, async, base.} = discard
params: JsonNode): Future[Response] {.gcsafe, async, base.} =
discard

method close*(client: RpcClient) {.base, gcsafe, async.} = discard

Expand Down
3 changes: 1 addition & 2 deletions json_rpc/clients/httpclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,12 @@ proc httpMethod*(client: RpcHttpClient, m: HttpMethod) =
client.options.httpMethod = m

method call*(client: RpcHttpClient, name: string,
params: JsonNode): Future[Response] {.async, gcsafe.} =
params: JsonNode): Future[Response] {.async, gcsafe.} =
## Remotely calls the specified RPC method.
let id = client.getNextId()

let transp = await connect(client.addresses[0])
var reqBody = $rpcCallNode(name, params, id)
echo "Sending (", client.httpMethod, "): ", reqBody
let res = await transp.sendRequest(reqBody, client.httpMethod)
if not res:
debug "Failed to send message to RPC server",
Expand Down
4 changes: 2 additions & 2 deletions json_rpc/clients/socketclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ proc newRpcSocketClient*: RpcSocketClient =
result.initRpcClient()

method call*(self: RpcSocketClient, name: string,
params: JsonNode): Future[Response] {.async.} =
params: JsonNode): Future[Response] {.async.} =
## Remotely calls the specified RPC method.
let id = self.getNextId()
var value = $rpcCallNode(name, params, id) & "\c\l"
Expand All @@ -29,7 +29,7 @@ method call*(self: RpcSocketClient, name: string,

let res = await self.transport.write(value)
# TODO: Add actions when not full packet was send, e.g. disconnect peer.
doAssert(res == len(value))
doAssert(res == len(value))

result = await newFut

Expand Down
2 changes: 1 addition & 1 deletion json_rpc/clients/websocketclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ proc newRpcWebSocketClient*: RpcWebSocketClient =
result.initRpcClient()

method call*(self: RpcWebSocketClient, name: string,
params: JsonNode): Future[Response] {.async.} =
params: JsonNode): Future[Response] {.async.} =
## Remotely calls the specified RPC method.
let id = self.getNextId()
var value = $rpcCallNode(name, params, id) & "\c\l"
Expand Down
49 changes: 19 additions & 30 deletions json_rpc/router.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ const
paramsField = "params"
jsonRpcField = "jsonrpc"
idField = "id"
resultField = "result"
errorField = "error"
codeField = "code"
messageField = "message"
dataField = "data"
messageTerminator = "\c\l"

JSON_PARSE_ERROR* = -32700
Expand Down Expand Up @@ -103,15 +98,15 @@ proc checkJsonState*(line: string,

proc wrapReply*(id: JsonNode, value, error: StringOfJson): StringOfJson =
return StringOfJson(
"""{"jsonRpcField":"2.0","idField":$1,"resultField":$2,"errorField":$3}""" % [
"""{"jsonrpc":"2.0","id":$1,"result":$2,"error":$3}""" % [
$id, string(value), string(error)
])

proc wrapError*(code: int, msg: string, id: JsonNode,
data: JsonNode = newJNull()): StringOfJson {.gcsafe.} =
# Create standardised error json
result = StringOfJson(
"""{"codeField":$1,"idField":$2,"messageField":$3,"dataField":$4}""" % [
"""{"code":$1,"id":$2,"message":$3,"data":$4}""" % [
$code, $id, escapeJson(msg), $data
])
debug "Error generated", error = result, id = id
Expand Down Expand Up @@ -224,34 +219,28 @@ macro rpc*(server: RpcRouter, path: string, body: untyped): untyped =
setup = jsonToNim(parameters, paramsIdent)
procBody = if body.kind == nnkStmtList: body else: body.body

if parameters.hasReturnType:
let ReturnType = parameters[0]
let ReturnType = if parameters.hasReturnType: parameters[0]
else: ident "JsonNode"

# delegate async proc allows return and setting of result as native type
# delegate async proc allows return and setting of result as native type
result.add quote do:
proc `doMain`(`paramsIdent`: JsonNode): Future[`ReturnType`] {.async.} =
`setup`
`procBody`

if ReturnType == ident"JsonNode":
# `JsonNode` results don't need conversion
result.add quote do:
proc `doMain`(`paramsIdent`: JsonNode): Future[`ReturnType`] {.async.} =
`setup`
`procBody`

if ReturnType == ident"JsonNode":
# `JsonNode` results don't need conversion
result.add quote do:
proc `procName`(`paramsIdent`: JsonNode): Future[StringOfJson] {.async, gcsafe.} =
return StringOfJson($(await `doMain`(`paramsIdent`)))
elif ReturnType == ident"StringOfJson":
result.add quote do:
proc `procName`(`paramsIdent`: JsonNode): Future[StringOfJson] {.async, gcsafe.} =
return await `doMain`(`paramsIdent`)
else:
result.add quote do:
proc `procName`(`paramsIdent`: JsonNode): Future[StringOfJson] {.async, gcsafe.} =
return StringOfJson($(%(await `doMain`(`paramsIdent`))))
proc `procName`(`paramsIdent`: JsonNode): Future[StringOfJson] {.async, gcsafe.} =
return StringOfJson($(await `doMain`(`paramsIdent`)))
elif ReturnType == ident"StringOfJson":
result.add quote do:
proc `procName`(`paramsIdent`: JsonNode): Future[StringOfJson] {.async, gcsafe.} =
return await `doMain`(`paramsIdent`)
else:
# no return types, inline contents
result.add quote do:
proc `procName`(`paramsIdent`: JsonNode): Future[StringOfJson] {.async, gcsafe.} =
`setup`
`procBody`
return StringOfJson($(%(await `doMain`(`paramsIdent`))))

result.add quote do:
`server`.register(`path`, `procName`)
Expand Down
12 changes: 12 additions & 0 deletions tests/helpers.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import
json, ../json_rpc/router

template `==`*(a, b: distinct (string|StringOfJson)): bool =
string(a) == string(b)

template `==`*(a: StringOfJson, b: JsonNode): bool =
parseJson(string a) == b

template `==`*(a: JsonNode, b: StringOfJson): bool =
a == parseJson(string b)

9 changes: 5 additions & 4 deletions tests/testethcalls.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest, json, tables
import ../json_rpc/[rpcclient, rpcserver]
import stint, ethtypes, ethprocs, stintjson, chronicles
import
unittest, json, tables,
stint, ethtypes, ethprocs, stintjson, chronicles,
../json_rpc/[rpcclient, rpcserver], ./helpers

from os import getCurrentDir, DirSep
from strutils import rsplit
Expand All @@ -26,7 +27,7 @@ server.rpc("rpc.testreturnuint256") do() -> UInt256:
let r: UInt256 = "0x1234567890abcdef".parse(UInt256, 16)
return r

proc testLocalCalls: Future[seq[JsonNode]] =
proc testLocalCalls: Future[seq[StringOfJson]] =
## Call RPCs created with `rpc` locally.
## This simply demonstrates async calls of the procs generated by the `rpc` macro.
var
Expand Down
4 changes: 2 additions & 2 deletions tests/testrpcmacro.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest, json, chronicles, options
import ../json_rpc/rpcserver
import ../json_rpc/rpcserver, ./helpers

type
# some nested types to check object parsing
Expand Down Expand Up @@ -137,7 +137,7 @@ suite "Server types":

test "Simple paths":
let r = waitFor rpcSimplePath(%[])
check r == %1
check r == "1"

test "Different param types":
let
Expand Down
6 changes: 4 additions & 2 deletions tests/testserverclient.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest, json, chronicles
import ../json_rpc/[rpcclient, rpcserver]
import
unittest, json, chronicles,
../json_rpc/[rpcclient, rpcserver], ./helpers

var srv = newRpcSocketServer(["localhost:8545"])
var client = newRpcSocketClient()
Expand All @@ -19,3 +20,4 @@ suite "Server/Client RPC":

srv.stop()
waitFor srv.closeWait()

0 comments on commit f53e0b9

Please sign in to comment.