Skip to content

Commit

Permalink
Let the OS choose the port for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jan 7, 2024
1 parent 31b7417 commit 3a15a9f
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 46 deletions.
5 changes: 5 additions & 0 deletions json_rpc/clients/socketclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ proc connect*(client: RpcSocketClient, address: string, port: Port) {.async.} =
client.address = addresses[0]
client.loop = processData(client)

proc connect*(client: RpcSocketClient, address: TransportAddress) {.async.} =
client.transport = await connect(address)
client.address = address
client.loop = processData(client)

method close*(client: RpcSocketClient) {.async.} =
await client.loop.cancelAndWait()
if not client.transport.isNil:
Expand Down
3 changes: 3 additions & 0 deletions json_rpc/rpcproxy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,7 @@ proc stop*(proxy: RpcProxy) {.async.} =
proc closeWait*(proxy: RpcProxy) {.async.} =
await proxy.rpcHttpServer.closeWait()

func localAddress*(proxy: RpcProxy): seq[TransportAddress] =
proxy.rpcHttpServer.localAddress()

{.pop.}
4 changes: 4 additions & 0 deletions json_rpc/servers/httpserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,7 @@ proc closeWait*(server: RpcHttpServer) {.async.} =
## Cleanup resources of RPC server.
for item in server.httpServers:
await item.closeWait()

proc localAddress*(server: RpcHttpServer): seq[TransportAddress] =
for item in server.httpServers:
result.add item.instance.localAddress()
4 changes: 4 additions & 0 deletions json_rpc/servers/socketserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,7 @@ proc closeWait*(server: RpcSocketServer) {.async.} =
## Cleanup resources of RPC server.
for item in server.servers:
await item.closeWait()

proc localAddress*(server: RpcSocketServer): seq[TransportAddress] =
for x in server.servers:
result.add x.localAddress
3 changes: 3 additions & 0 deletions json_rpc/servers/websocketserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,6 @@ proc close*(server: RpcWebSocketServer) =
proc closeWait*(server: RpcWebSocketServer) {.async.} =
## Cleanup resources of RPC server.
await server.server.closeWait()

proc localAddress*(server: RpcWebSocketServer): TransportAddress =
server.server.localAddress()
4 changes: 2 additions & 2 deletions tests/test_callsigs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ proc installHandlers(s: RpcServer) =
return "meow"

suite "test callsigs":
var server = newRpcSocketServer(["127.0.0.1:8545"])
var server = newRpcSocketServer(["127.0.0.1:0"])
server.installHandlers()
var client = newRpcSocketClient()

server.start()
waitFor client.connect("127.0.0.1", Port(8545))
waitFor client.connect(server.localAddress()[0])

test "callsigs from file":
let res = waitFor client.shh_uninstallFilter(123)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_router_rpc.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# json-rpc
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import
unittest2,
../json_rpc/router,
Expand Down
4 changes: 2 additions & 2 deletions tests/testethcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ from strutils import rsplit
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]

var
server = newRpcSocketServer("localhost", Port(8545))
server = newRpcSocketServer("127.0.0.1", Port(0))
client = newRpcSocketClient()

## Generate Ethereum server RPCs
Expand Down Expand Up @@ -66,7 +66,7 @@ proc testSigCalls: Future[seq[string]] =
return all(version, sha3)

server.start()
waitFor client.connect("localhost", Port(8545))
waitFor client.connect(server.localAddress()[0])


suite "Local calls":
Expand Down
16 changes: 8 additions & 8 deletions tests/testhook.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import

const
serverHost = "127.0.0.1"
serverPort = 8547
serverPort = 0 # let the OS choose the port
serverAddress = serverHost & ":" & $serverPort

proc setupServer*(srv: RpcServer) =
Expand All @@ -37,14 +37,14 @@ suite "HTTP server hook test":

test "no auth token":
let client = newRpcHttpClient()
waitFor client.connect(serverHost, Port(serverPort), false)
waitFor client.connect("http://" & $srv.localAddress()[0])
expect ErrorResponse:
let r = waitFor client.call("testHook", %[%"abc"])
discard r

test "good auth token":
let client = newRpcHttpClient(getHeaders = authHeaders)
waitFor client.connect(serverHost, Port(serverPort), false)
waitFor client.connect("http://" & $srv.localAddress()[0])
let r = waitFor client.call("testHook", %[%"abc"])
check r.string == "\"Hello abc\""

Expand Down Expand Up @@ -72,8 +72,8 @@ suite "Websocket server hook test":
return true

let srv = newRpcWebSocketServer(
"127.0.0.1",
Port(8545),
serverHost,
Port(serverPort),
authHooks = @[WsAuthHook(mockAuth)]
)
srv.setupServer()
Expand All @@ -82,13 +82,13 @@ suite "Websocket server hook test":

test "no auth token":
try:
waitFor client.connect("ws://127.0.0.1:8545/")
waitFor client.connect("ws://" & $srv.localAddress())
check false
except CatchableError as e:
check e.msg == "Server did not reply with a websocket upgrade: Header code: 403 Header reason: Forbidden Address: 127.0.0.1:8545"
check e.msg == "Server did not reply with a websocket upgrade: Header code: 403 Header reason: Forbidden Address: " & $srv.localAddress()

test "good auth token":
waitFor client.connect("ws://127.0.0.1:8545/", hooks = @[hook])
waitFor client.connect("ws://" & $srv.localAddress(), hooks = @[hook])
let r = waitFor client.call("testHook", %[%"abc"])
check r.string == "\"Hello abc\""

Expand Down
20 changes: 10 additions & 10 deletions tests/testhttp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ import ../json_rpc/[rpcserver, rpcclient]

const TestsCount = 100

proc simpleTest(address: string, port: Port): Future[bool] {.async.} =
proc simpleTest(address: string): Future[bool] {.async.} =
var client = newRpcHttpClient()
await client.connect(address, port, secure = false)
await client.connect("http://" & address)
var r = await client.call("noParamsProc", %[])
if r.string == "\"Hello world\"":
result = true

proc continuousTest(address: string, port: Port): Future[int] {.async.} =
proc continuousTest(address: string): Future[int] {.async.} =
var client = newRpcHttpClient()
result = 0
for i in 0..<TestsCount:
await client.connect(address, port, secure = false)
await client.connect("http://" & address)
var r = await client.call("myProc", %[%"abc", %[1, 2, 3, i]])
if r.string == "\"Hello abc data: [1, 2, 3, " & $i & "]\"":
result += 1
await client.close()

proc invalidTest(address: string, port: Port): Future[bool] {.async.} =
proc invalidTest(address: string): Future[bool] {.async.} =
var client = newRpcHttpClient()
await client.connect(address, port, secure = false)
await client.connect("http://" & address)
var invalidA, invalidB: bool
try:
var r = await client.call("invalidProcA", %[])
Expand All @@ -46,7 +46,7 @@ proc invalidTest(address: string, port: Port): Future[bool] {.async.} =
if invalidA and invalidB:
result = true

var httpsrv = newRpcHttpServer(["127.0.0.1:8545"])
var httpsrv = newRpcHttpServer(["127.0.0.1:0"])

# Create RPC on server
httpsrv.rpc("myProc") do(input: string, data: array[0..3, int]):
Expand All @@ -58,11 +58,11 @@ httpsrv.start()

suite "JSON-RPC test suite":
test "Simple RPC call":
check waitFor(simpleTest("127.0.0.1", Port(8545))) == true
check waitFor(simpleTest($httpsrv.localAddress()[0])) == true
test "Continuous RPC calls (" & $TestsCount & " messages)":
check waitFor(continuousTest("127.0.0.1", Port(8545))) == TestsCount
check waitFor(continuousTest($httpsrv.localAddress()[0])) == TestsCount
test "Invalid RPC calls":
check waitFor(invalidTest("127.0.0.1", Port(8545))) == true
check waitFor(invalidTest($httpsrv.localAddress()[0])) == true

waitFor httpsrv.stop()
waitFor httpsrv.closeWait()
20 changes: 10 additions & 10 deletions tests/testhttps.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,26 @@ N8r5CwGcIX/XPC3lKazzbZ8baA==
-----END CERTIFICATE-----
"""

proc simpleTest(address: string, port: Port): Future[bool] {.async.} =
proc simpleTest(address: string): Future[bool] {.async.} =
var client = newRpcHttpClient(secure=true)
await client.connect(address, port, secure=true)
await client.connect("https://" & address)
var r = await client.call("noParamsProc", %[])
if r.string == "\"Hello world\"":
result = true

proc continuousTest(address: string, port: Port): Future[int] {.async.} =
proc continuousTest(address: string): Future[int] {.async.} =
var client = newRpcHttpClient(secure=true)
result = 0
for i in 0..<TestsCount:
await client.connect(address, port, secure=true)
await client.connect("https://" & address)
var r = await client.call("myProc", %[%"abc", %[1, 2, 3, i]])
if r.string == "\"Hello abc data: [1, 2, 3, " & $i & "]\"":
result += 1
await client.close()

proc invalidTest(address: string, port: Port): Future[bool] {.async.} =
proc invalidTest(address: string): Future[bool] {.async.} =
var client = newRpcHttpClient(secure=true)
await client.connect(address, port, secure=true)
await client.connect("https://" & address)
var invalidA, invalidB: bool
try:
var r = await client.call("invalidProcA", %[])
Expand All @@ -111,7 +111,7 @@ let secureKey = TLSPrivateKey.init(HttpsSelfSignedRsaKey)
let secureCert = TLSCertificate.init(HttpsSelfSignedRsaCert)
var secureHttpSrv = RpcHttpServer.new()

secureHttpSrv.addSecureHttpServer("127.0.0.1:8545", secureKey, secureCert)
secureHttpSrv.addSecureHttpServer("127.0.0.1:0", secureKey, secureCert)

# Create RPC on server
secureHttpSrv.rpc("myProc") do(input: string, data: array[0..3, int]):
Expand All @@ -123,11 +123,11 @@ secureHttpSrv.start()

suite "JSON-RPC test suite":
test "Simple RPC call":
check waitFor(simpleTest("127.0.0.1", Port(8545))) == true
check waitFor(simpleTest($secureHttpsrv.localAddress()[0])) == true
test "Continuous RPC calls (" & $TestsCount & " messages)":
check waitFor(continuousTest("127.0.0.1", Port(8545))) == TestsCount
check waitFor(continuousTest($secureHttpsrv.localAddress()[0])) == TestsCount
test "Invalid RPC calls":
check waitFor(invalidTest("127.0.0.1", Port(8545))) == true
check waitFor(invalidTest($secureHttpsrv.localAddress()[0])) == true

waitFor secureHttpSrv.stop()
waitFor secureHttpSrv.closeWait()
13 changes: 6 additions & 7 deletions tests/testproxy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import
unittest2, chronicles,
../json_rpc/[rpcclient, rpcserver, rpcproxy]

let srvAddress = initTAddress("127.0.0.1", Port(8545))
let proxySrvAddress = "127.0.0.1:8546"
let proxySrvAddressForClient = "http://"&proxySrvAddress
let srvAddress = initTAddress("127.0.0.1", Port(0))
let proxySrvAddress = "127.0.0.1:0"

template registerMethods(srv: RpcServer, proxy: RpcProxy) =
srv.rpc("myProc") do(input: string, data: array[0..3, int]):
Expand All @@ -27,14 +26,14 @@ template registerMethods(srv: RpcServer, proxy: RpcProxy) =

suite "Proxy RPC through http":
var srv = newRpcHttpServer([srvAddress])
var proxy = RpcProxy.new([proxySrvAddress], getHttpClientConfig("http://127.0.0.1:8545"))
var proxy = RpcProxy.new([proxySrvAddress], getHttpClientConfig("http://" & $srv.localAddress()[0]))
var client = newRpcHttpClient()

registerMethods(srv, proxy)

srv.start()
waitFor proxy.start()
waitFor client.connect(proxySrvAddressForClient)
waitFor client.connect("http://" & $proxy.localAddress()[0])

test "Successful RPC call thorugh proxy":
let r = waitFor client.call("myProc", %[%"abc", %[1, 2, 3, 4]])
Expand All @@ -56,14 +55,14 @@ suite "Proxy RPC through http":

suite "Proxy RPC through websockets":
var srv = newRpcWebSocketServer(srvAddress)
var proxy = RpcProxy.new([proxySrvAddress], getWebSocketClientConfig("ws://127.0.0.1:8545"))
var proxy = RpcProxy.new([proxySrvAddress], getWebSocketClientConfig("ws://" & $srv.localAddress()))
var client = newRpcHttpClient()

registerMethods(srv, proxy)

srv.start()
waitFor proxy.start()
waitFor client.connect(proxySrvAddressForClient)
waitFor client.connect("http://" & $proxy.localAddress()[0])

test "Successful RPC call thorugh proxy":
let r = waitFor client.call("myProc", %[%"abc", %[1, 2, 3, 4]])
Expand Down
2 changes: 1 addition & 1 deletion tests/testrpcmacro.nim
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ let
},
"c": %1.0}

var s = newRpcSocketServer(["127.0.0.1:8545"])
var s = newRpcSocketServer(["127.0.0.1:0"])

# RPC definitions
s.rpc("rpc.simplePath"):
Expand Down
12 changes: 6 additions & 6 deletions tests/testserverclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ proc setupServer*(srv: RpcServer) =
raise (ref InvalidRequest)(code: -32001, msg: "Unknown payload")

suite "Socket Server/Client RPC":
var srv = newRpcSocketServer(["127.0.0.1:8545"])
var srv = newRpcSocketServer(["127.0.0.1:0"])
var client = newRpcSocketClient()

srv.setupServer()
srv.start()
waitFor client.connect("127.0.0.1", Port(8545))
waitFor client.connect(srv.localAddress()[0])

test "Successful RPC call":
let r = waitFor client.call("myProc", %[%"abc", %[1, 2, 3, 4]])
Expand All @@ -53,12 +53,12 @@ suite "Socket Server/Client RPC":
waitFor srv.closeWait()

suite "Websocket Server/Client RPC":
var srv = newRpcWebSocketServer("127.0.0.1", Port(8545))
var srv = newRpcWebSocketServer("127.0.0.1", Port(0))
var client = newRpcWebSocketClient()

srv.setupServer()
srv.start()
waitFor client.connect("ws://127.0.0.1:8545/")
waitFor client.connect("ws://" & $srv.localAddress())

test "Successful RPC call":
let r = waitFor client.call("myProc", %[%"abc", %[1, 2, 3, 4]])
Expand All @@ -83,13 +83,13 @@ suite "Websocket Server/Client RPC":
waitFor srv.closeWait()

suite "Websocket Server/Client RPC with Compression":
var srv = newRpcWebSocketServer("127.0.0.1", Port(8545),
var srv = newRpcWebSocketServer("127.0.0.1", Port(0),
compression = true)
var client = newRpcWebSocketClient()

srv.setupServer()
srv.start()
waitFor client.connect("ws://127.0.0.1:8545/",
waitFor client.connect("ws://" & $srv.localAddress(),
compression = true)

test "Successful RPC call":
Expand Down

0 comments on commit 3a15a9f

Please sign in to comment.