mirror of
https://github.com/status-im/nim-ethers.git
synced 2025-02-17 09:36:34 +00:00
add test that simulates POST failure
This commit is contained in:
parent
a64235dfee
commit
39d03f642c
@ -3,41 +3,45 @@ import ../../../ethers/provider
|
|||||||
import ../../../ethers/providers/jsonrpc/conversions
|
import ../../../ethers/providers/jsonrpc/conversions
|
||||||
|
|
||||||
import std/tables
|
import std/tables
|
||||||
import std/sequtils
|
|
||||||
import pkg/stew/byteutils
|
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/errors
|
||||||
|
|
||||||
type MockRpcHttpServer* = ref object
|
type MockRpcHttpServer* = ref object
|
||||||
filters*: seq[string]
|
filterChanges*: Table[string, seq[Log]]
|
||||||
srv: RpcHttpServer
|
srv: RpcHttpServer
|
||||||
|
|
||||||
proc new*(_: type MockRpcHttpServer): MockRpcHttpServer =
|
proc new*(_: type MockRpcHttpServer): MockRpcHttpServer =
|
||||||
let srv = newRpcHttpServer(["127.0.0.1:0"])
|
let srv = newRpcHttpServer(["127.0.0.1:0"])
|
||||||
MockRpcHttpServer(filters: @[], srv: srv)
|
MockRpcHttpServer(filterChanges: initTable[string, seq[Log]](), srv: srv)
|
||||||
|
|
||||||
proc invalidateFilter*(server: MockRpcHttpServer, jsonId: JsonNode) =
|
proc invalidateFilter*(server: MockRpcHttpServer, jsonId: JsonNode) =
|
||||||
server.filters.keepItIf it != jsonId.getStr
|
trace "invalidating filter", id = jsonId.getStr
|
||||||
|
server.filterChanges.del jsonId.getStr
|
||||||
|
|
||||||
|
proc addFilterChange*(server: MockRpcHttpServer, jsonId: JsonNode, change: Log) =
|
||||||
|
server.filterChanges[jsonId.getStr].add change
|
||||||
|
|
||||||
proc start*(server: MockRpcHttpServer) =
|
proc start*(server: MockRpcHttpServer) =
|
||||||
server.srv.router.rpc("eth_newFilter") do(filter: EventFilter) -> string:
|
server.srv.router.rpc("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.filterChanges[filterId] = @[]
|
||||||
return filterId
|
return filterId
|
||||||
|
|
||||||
server.srv.router.rpc("eth_newBlockFilter") do() -> string:
|
server.srv.router.rpc("eth_newBlockFilter") do() -> string:
|
||||||
let filterId = "0x" & (array[16, byte].example).toHex
|
let filterId = "0x" & (array[16, byte].example).toHex
|
||||||
server.filters.add filterId
|
server.filterChanges[filterId] = @[]
|
||||||
return filterId
|
return filterId
|
||||||
|
|
||||||
server.srv.router.rpc("eth_getFilterChanges") do(id: string) -> seq[string]:
|
server.srv.router.rpc("eth_getFilterChanges") do(id: string) -> seq[Log]:
|
||||||
if id notin server.filters:
|
if id notin server.filterChanges:
|
||||||
|
trace "Raising filter not found error", id
|
||||||
raise (ref ApplicationError)(code: -32000, msg: "filter not found")
|
raise (ref ApplicationError)(code: -32000, msg: "filter not found")
|
||||||
|
|
||||||
return @[]
|
return server.filterChanges[id]
|
||||||
|
|
||||||
server.srv.router.rpc("eth_uninstallFilter") do(id: string) -> bool:
|
server.srv.router.rpc("eth_uninstallFilter") do(id: string) -> bool:
|
||||||
if id notin server.filters:
|
if id notin server.filterChanges:
|
||||||
raise (ref ApplicationError)(code: -32000, msg: "filter not found")
|
raise (ref ApplicationError)(code: -32000, msg: "filter not found")
|
||||||
|
|
||||||
server.invalidateFilter(%id)
|
server.invalidateFilter(%id)
|
||||||
|
@ -2,10 +2,9 @@ import pkg/serde
|
|||||||
import std/os
|
import std/os
|
||||||
import std/sequtils
|
import std/sequtils
|
||||||
import std/importutils
|
import std/importutils
|
||||||
import pkg/asynctest
|
import pkg/asynctest/chronos/unittest
|
||||||
import pkg/serde
|
import pkg/json_rpc/rpcclient except `%`, `%*`, toJson
|
||||||
import pkg/json_rpc/rpcclient
|
import pkg/json_rpc/rpcserver except `%`, `%*`, toJson
|
||||||
import pkg/json_rpc/rpcserver
|
|
||||||
import ethers/provider
|
import ethers/provider
|
||||||
import ethers/providers/jsonrpc/subscriptions
|
import ethers/providers/jsonrpc/subscriptions
|
||||||
|
|
||||||
@ -103,6 +102,7 @@ suite "HTTP polling subscriptions - filter not found":
|
|||||||
var subscriptions: PollingSubscriptions
|
var subscriptions: PollingSubscriptions
|
||||||
var client: RpcHttpClient
|
var client: RpcHttpClient
|
||||||
var mockServer: MockRpcHttpServer
|
var mockServer: MockRpcHttpServer
|
||||||
|
var url: string
|
||||||
|
|
||||||
privateAccess(PollingSubscriptions)
|
privateAccess(PollingSubscriptions)
|
||||||
|
|
||||||
@ -111,7 +111,8 @@ suite "HTTP polling subscriptions - filter not found":
|
|||||||
mockServer.start()
|
mockServer.start()
|
||||||
|
|
||||||
client = newRpcHttpClient()
|
client = newRpcHttpClient()
|
||||||
await client.connect("http://" & $mockServer.localAddress()[0])
|
url = "http://" & $mockServer.localAddress()[0]
|
||||||
|
await client.connect(url)
|
||||||
|
|
||||||
subscriptions = PollingSubscriptions(
|
subscriptions = PollingSubscriptions(
|
||||||
JsonRpcSubscriptions.new(
|
JsonRpcSubscriptions.new(
|
||||||
@ -124,6 +125,30 @@ suite "HTTP polling subscriptions - filter not found":
|
|||||||
await client.close()
|
await client.close()
|
||||||
await mockServer.stop()
|
await mockServer.stop()
|
||||||
|
|
||||||
|
test "polling loop is kept alive after disconnection":
|
||||||
|
let log = Log(blockNumber: 999999.u256)
|
||||||
|
|
||||||
|
var latestBlock = 0.u256
|
||||||
|
proc callback(log: Log) =
|
||||||
|
trace "GOT LOG IN CALLBACK", number = log.blockNumber
|
||||||
|
latestBlock = log.blockNumber
|
||||||
|
|
||||||
|
let filter = EventFilter(address: Address.example, topics: @[array[32, byte].example])
|
||||||
|
let emptyHandler = proc(log: Log) = discard
|
||||||
|
|
||||||
|
let id = await subscriptions.subscribeLogs(filter, callback)
|
||||||
|
# simulate failed requests by connecting to an invalid port
|
||||||
|
await client.connect("http://127.0.0.1:1")
|
||||||
|
await sleepAsync(5.millis) # wait for polling loop
|
||||||
|
trace "STARTING MOCK SERVER"
|
||||||
|
await client.connect(url)
|
||||||
|
trace "ADDING FILTER CHANGE"
|
||||||
|
mockServer.addFilterChange(id, log)
|
||||||
|
|
||||||
|
check eventually(latestBlock == log.blockNumber, timeout=200)
|
||||||
|
|
||||||
|
await subscriptions.unsubscribe(id)
|
||||||
|
|
||||||
test "filter not found error recreates log filter":
|
test "filter not found error recreates log filter":
|
||||||
let filter = EventFilter(address: Address.example, topics: @[array[32, byte].example])
|
let filter = EventFilter(address: Address.example, topics: @[array[32, byte].example])
|
||||||
let emptyHandler = proc(log: Log) = discard
|
let emptyHandler = proc(log: Log) = discard
|
||||||
|
Loading…
x
Reference in New Issue
Block a user