From 7034bceaaf33a388d54b06278bed25a171b0286c Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Thu, 3 Jul 2025 09:56:07 +1000 Subject: [PATCH] mock rpc http server for subscriptions -- needs refactor --- .../jsonrpc/mocks/mockRpcHttpServer.nim | 38 +++++++++---------- .../mocks/mockRpcHttpServerSubscriptions.nim | 2 +- .../jsonrpc/testJsonRpcSubscriptions.nim | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/testmodule/providers/jsonrpc/mocks/mockRpcHttpServer.nim b/testmodule/providers/jsonrpc/mocks/mockRpcHttpServer.nim index 3c0258b..f710011 100644 --- a/testmodule/providers/jsonrpc/mocks/mockRpcHttpServer.nim +++ b/testmodule/providers/jsonrpc/mocks/mockRpcHttpServer.nim @@ -1,31 +1,31 @@ -import ../../../examples -import ../../../../ethers/provider -import ../../../../ethers/providers/jsonrpc/conversions - -import std/sequtils -import pkg/stew/byteutils import pkg/json_rpc/rpcserver except `%`, `%*` -import pkg/json_rpc/errors +import pkg/json_rpc/servers/httpserver +import ./mockRpcServer {.push raises: [].} -type MockRpcHttpServer* = ref object of RootObj - srv: RpcHttpServer +type MockRpcHttpServer* = ref object of MockRpcServer proc new*(_: type MockRpcHttpServer): MockRpcHttpServer {.raises: [JsonRpcError].} = - let srv = newRpcHttpServer(["127.0.0.1:0"]) + let srv = newRpcHttpServer(initTAddress("127.0.0.1:0")) MockRpcHttpServer(srv: srv) - -template registerRpcMethod*(server: MockRpcHttpServer, path: string, body: untyped): untyped = +template registerRpcMethod*( + server: MockRpcHttpServer, path: string, body: untyped +): untyped = server.srv.router.rpc(path, body) -method start*(server: MockRpcHttpServer) {.gcsafe, base.} = - server.srv.start() +method start*(server: MockRpcHttpServer) {.gcsafe, raises: [JsonRpcError].} = + RpcHttpServer(server.srv).start() -proc stop*(server: MockRpcHttpServer) {.async.} = - await server.srv.stop() - await server.srv.closeWait() +method stop*(server: MockRpcHttpServer) {.async: (raises: []).} = + try: + await RpcHttpServer(server.srv).stop() + await RpcHttpServer(server.srv).closeWait() + except CatchableError: + # stop and closeWait don't actually raise but they're not annotated with + # raises: [] + discard -proc localAddress*(server: MockRpcHttpServer): seq[TransportAddress] = - return server.srv.localAddress() +method localAddress*(server: MockRpcHttpServer): TransportAddress = + return RpcHttpServer(server.srv).localAddress()[0] diff --git a/testmodule/providers/jsonrpc/mocks/mockRpcHttpServerSubscriptions.nim b/testmodule/providers/jsonrpc/mocks/mockRpcHttpServerSubscriptions.nim index 3b9edac..0817907 100644 --- a/testmodule/providers/jsonrpc/mocks/mockRpcHttpServerSubscriptions.nim +++ b/testmodule/providers/jsonrpc/mocks/mockRpcHttpServerSubscriptions.nim @@ -23,7 +23,7 @@ proc new*(_: type MockRpcHttpServerSubscriptions): MockRpcHttpServerSubscription proc invalidateFilter*(server: MockRpcHttpServerSubscriptions, jsonId: JsonNode) = server.filters.keepItIf it != jsonId.getStr -method start*(server: MockRpcHttpServerSubscriptions) = +method start*(server: MockRpcHttpServerSubscriptions) {.raises: [JsonRpcError].} = server.registerRpcMethod("eth_newFilter") do(filter: EventFilter) -> string: let filterId = "0x" & (array[16, byte].example).toHex server.filters.add filterId diff --git a/testmodule/providers/jsonrpc/testJsonRpcSubscriptions.nim b/testmodule/providers/jsonrpc/testJsonRpcSubscriptions.nim index fdbaa5d..df863fc 100644 --- a/testmodule/providers/jsonrpc/testJsonRpcSubscriptions.nim +++ b/testmodule/providers/jsonrpc/testJsonRpcSubscriptions.nim @@ -119,7 +119,7 @@ suite "HTTP polling subscriptions - mock tests": proc startServer() {.async.} = mockServer = MockRpcHttpServerSubscriptions.new() mockServer.start() - await client.connect("http://" & $MockRpcHttpServer(mockServer).localAddress()[0]) + await client.connect("http://" & $mockServer.localAddress) proc stopServer() {.async.} = await MockRpcHttpServer(mockServer).stop()