nim-ethers/testmodule/providers/jsonrpc/testWsResubscription.nim
Arnaud af3d7379c8
chore: add ws resubscription for hardhat workaround (#112)
* Move logFilters to JsonRpcSubscriptions

* Add resubscribe flag

* Add documentation for the resubscribe symbol

* Rename the symbol for better clarity

* Provide better message

* Add nimbledeps to git ignore

* Update wording

* Update wording

* Remove the ws_resubscribe flag from the config

* Handle the concurrency issues when updating the logFilters and add tests

* Update log filters comment

* Add lock when subscribing to blocks

* Remove useless private access

* Fix wording

* Fix try except format

* Restore privateAccess because logEvents moved to JsonRpcSubscriptions

* Use seconds instead of milliseconds

* Remove extra dot in test label

* Restore new lines

* Pass the resubscribe internal in new function and remove unneeded try except

* Remove ws_resubscribe default value making testing easier

* Remove unneeded condition

* Add new line

* Fix nim syntax

* Update symbol description

* Log warning when the resubscription interval is more than 300 seconds

* Catch errors in close method

* Redefine raises for async pragma in close methods

* Provide better error message
2025-04-10 10:48:41 +02:00

58 lines
2.0 KiB
Nim

import std/os
import std/importutils
import pkg/asynctest
import pkg/json_rpc/rpcclient
import ethers/provider
import ethers/providers/jsonrpc/subscriptions
import ../../examples
import ./rpc_mock
suite "Websocket re-subscriptions":
privateAccess(JsonRpcSubscriptions)
var subscriptions: JsonRpcSubscriptions
var client: RpcWebSocketClient
var resubscribeInterval: int
setup:
resubscribeInterval = 3
client = newRpcWebSocketClient()
await client.connect("ws://" & getEnv("ETHERS_TEST_PROVIDER", "localhost:8545"))
subscriptions = JsonRpcSubscriptions.new(client, resubscribeInterval = resubscribeInterval)
subscriptions.start()
teardown:
await subscriptions.close()
await client.close()
test "unsubscribing from a log filter while subscriptions are being resubscribed does not cause a concurrency error":
let filter = EventFilter(address: Address.example, topics: @[array[32, byte].example])
let emptyHandler = proc(log: ?!Log) = discard
for i in 1..10:
discard await subscriptions.subscribeLogs(filter, emptyHandler)
# Wait until the re-subscription starts
await sleepAsync(resubscribeInterval.seconds)
# Attempt to modify callbacks while its being iterated
discard await subscriptions.subscribeLogs(filter, emptyHandler)
test "resubscribe events take effect with new subscription IDs in the log filters":
let filter = EventFilter(address: Address.example, topics: @[array[32, byte].example])
let emptyHandler = proc(log: ?!Log) = discard
let id = await subscriptions.subscribeLogs(filter, emptyHandler)
check id in subscriptions.logFilters
check subscriptions.logFilters.len == 1
# Make sure the subscription is done
await sleepAsync((resubscribeInterval + 1).seconds)
# The previous subscription should not be in the log filters
check id notin subscriptions.logFilters
# There is still one subscription which is the new one
check subscriptions.logFilters.len == 1