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/rpcserver except `%`, `%*`
import pkg/json_rpc/errors import pkg/json_rpc/servers/httpserver
import ./mockRpcServer
{.push raises: [].} {.push raises: [].}
type MockRpcHttpServer* = ref object of RootObj type MockRpcHttpServer* = ref object of MockRpcServer
srv: RpcHttpServer
proc new*(_: type MockRpcHttpServer): MockRpcHttpServer {.raises: [JsonRpcError].} = 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) MockRpcHttpServer(srv: srv)
template registerRpcMethod*(
template registerRpcMethod*(server: MockRpcHttpServer, path: string, body: untyped): untyped = server: MockRpcHttpServer, path: string, body: untyped
): untyped =
server.srv.router.rpc(path, body) server.srv.router.rpc(path, body)
method start*(server: MockRpcHttpServer) {.gcsafe, base.} = method start*(server: MockRpcHttpServer) {.gcsafe, raises: [JsonRpcError].} =
server.srv.start() RpcHttpServer(server.srv).start()
proc stop*(server: MockRpcHttpServer) {.async.} = method stop*(server: MockRpcHttpServer) {.async: (raises: []).} =
await server.srv.stop() try:
await server.srv.closeWait() 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] = method localAddress*(server: MockRpcHttpServer): TransportAddress =
return server.srv.localAddress() return RpcHttpServer(server.srv).localAddress()[0]

View File

@ -23,7 +23,7 @@ proc new*(_: type MockRpcHttpServerSubscriptions): MockRpcHttpServerSubscription
proc invalidateFilter*(server: MockRpcHttpServerSubscriptions, jsonId: JsonNode) = proc invalidateFilter*(server: MockRpcHttpServerSubscriptions, jsonId: JsonNode) =
server.filters.keepItIf it != jsonId.getStr server.filters.keepItIf it != jsonId.getStr
method start*(server: MockRpcHttpServerSubscriptions) = method start*(server: MockRpcHttpServerSubscriptions) {.raises: [JsonRpcError].} =
server.registerRpcMethod("eth_newFilter") do(filter: EventFilter) -> string: server.registerRpcMethod("eth_newFilter") do(filter: EventFilter) -> string:
let filterId = "0x" & (array[16, byte].example).toHex let filterId = "0x" & (array[16, byte].example).toHex
server.filters.add filterId server.filters.add filterId

View File

@ -119,7 +119,7 @@ suite "HTTP polling subscriptions - mock tests":
proc startServer() {.async.} = proc startServer() {.async.} =
mockServer = MockRpcHttpServerSubscriptions.new() mockServer = MockRpcHttpServerSubscriptions.new()
mockServer.start() mockServer.start()
await client.connect("http://" & $MockRpcHttpServer(mockServer).localAddress()[0]) await client.connect("http://" & $mockServer.localAddress)
proc stopServer() {.async.} = proc stopServer() {.async.} =
await MockRpcHttpServer(mockServer).stop() await MockRpcHttpServer(mockServer).stop()