mock rpc http server for subscriptions -- needs refactor

This commit is contained in:
Eric 2025-07-03 09:56:07 +10:00
parent f480431ccc
commit 7034bceaaf
No known key found for this signature in database
3 changed files with 21 additions and 21 deletions

View File

@ -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]

View File

@ -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

View File

@ -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()