From 3a15a9fde81852ea6d716c21323c8546e822a7ed Mon Sep 17 00:00:00 2001 From: jangko Date: Sun, 7 Jan 2024 14:40:59 +0700 Subject: [PATCH] Let the OS choose the port for tests --- json_rpc/clients/socketclient.nim | 5 +++++ json_rpc/rpcproxy.nim | 3 +++ json_rpc/servers/httpserver.nim | 4 ++++ json_rpc/servers/socketserver.nim | 4 ++++ json_rpc/servers/websocketserver.nim | 3 +++ tests/test_callsigs.nim | 4 ++-- tests/test_router_rpc.nim | 9 +++++++++ tests/testethcalls.nim | 4 ++-- tests/testhook.nim | 16 ++++++++-------- tests/testhttp.nim | 20 ++++++++++---------- tests/testhttps.nim | 20 ++++++++++---------- tests/testproxy.nim | 13 ++++++------- tests/testrpcmacro.nim | 2 +- tests/testserverclient.nim | 12 ++++++------ 14 files changed, 73 insertions(+), 46 deletions(-) diff --git a/json_rpc/clients/socketclient.nim b/json_rpc/clients/socketclient.nim index dee5a45..6d2c34a 100644 --- a/json_rpc/clients/socketclient.nim +++ b/json_rpc/clients/socketclient.nim @@ -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: diff --git a/json_rpc/rpcproxy.nim b/json_rpc/rpcproxy.nim index c0df44d..8d4c1b8 100644 --- a/json_rpc/rpcproxy.nim +++ b/json_rpc/rpcproxy.nim @@ -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.} diff --git a/json_rpc/servers/httpserver.nim b/json_rpc/servers/httpserver.nim index 548cb27..1c4fe57 100644 --- a/json_rpc/servers/httpserver.nim +++ b/json_rpc/servers/httpserver.nim @@ -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() diff --git a/json_rpc/servers/socketserver.nim b/json_rpc/servers/socketserver.nim index 676a765..2c4aaf4 100644 --- a/json_rpc/servers/socketserver.nim +++ b/json_rpc/servers/socketserver.nim @@ -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 diff --git a/json_rpc/servers/websocketserver.nim b/json_rpc/servers/websocketserver.nim index 91e22a0..4882cc5 100644 --- a/json_rpc/servers/websocketserver.nim +++ b/json_rpc/servers/websocketserver.nim @@ -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() diff --git a/tests/test_callsigs.nim b/tests/test_callsigs.nim index 0a0a5f6..6a6c565 100644 --- a/tests/test_callsigs.nim +++ b/tests/test_callsigs.nim @@ -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) diff --git a/tests/test_router_rpc.nim b/tests/test_router_rpc.nim index 0846efe..a65b6a3 100644 --- a/tests/test_router_rpc.nim +++ b/tests/test_router_rpc.nim @@ -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, diff --git a/tests/testethcalls.nim b/tests/testethcalls.nim index 5d37f85..3621217 100644 --- a/tests/testethcalls.nim +++ b/tests/testethcalls.nim @@ -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 @@ -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": diff --git a/tests/testhook.nim b/tests/testhook.nim index 1502ab7..99b2337 100644 --- a/tests/testhook.nim +++ b/tests/testhook.nim @@ -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) = @@ -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\"" @@ -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() @@ -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\"" diff --git a/tests/testhttp.nim b/tests/testhttp.nim index b91af3e..23b29b5 100644 --- a/tests/testhttp.nim +++ b/tests/testhttp.nim @@ -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..