Files
nim-libp2p/tests/discovery/testrendezvousinterface.nim
2025-08-28 09:35:04 +01:00

68 lines
2.0 KiB
Nim

{.used.}
# Nim-Libp2p
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import chronos
import ../../libp2p/[protocols/rendezvous, switch, builders]
import ../../libp2p/discovery/[rendezvousinterface, discoverymngr]
import ../helpers
import ./utils
type
MockRendezVous = ref object of RendezVous
numAdvertiseNs1: int
numAdvertiseNs2: int
MockErrorRendezVous = ref object of MockRendezVous
method advertise*(
self: MockRendezVous, namespace: string, ttl: Duration
) {.async: (raises: [CancelledError, AdvertiseError]).} =
if namespace == "ns1":
self.numAdvertiseNs1 += 1
elif namespace == "ns2":
self.numAdvertiseNs2 += 1
# Forward the call to the actual implementation
await procCall RendezVous(self).advertise(namespace, ttl)
method advertise*(
self: MockErrorRendezVous, namespace: string, ttl: Duration
) {.async: (raises: [CancelledError, AdvertiseError]).} =
await procCall MockRendezVous(self).advertise(namespace, ttl)
raise newException(AdvertiseError, "MockErrorRendezVous.advertise")
suite "RendezVous Interface":
teardown:
checkTrackers()
proc baseTimeToAdvertiseTest(rdv: MockRendezVous) {.async.} =
let
tta = 100.milliseconds
ttl = 2.hours
client = createSwitch(rdv)
dm = DiscoveryManager()
await client.start()
dm.add(RendezVousInterface.new(rdv = rdv, tta = tta, ttl = ttl))
dm.advertise(RdvNamespace("ns1"))
dm.advertise(RdvNamespace("ns2"))
checkUntilTimeout:
rdv.numAdvertiseNs1 >= 5
checkUntilTimeout:
rdv.numAdvertiseNs2 >= 5
await client.stop()
asyncTest "Check timeToAdvertise interval":
await baseTimeToAdvertiseTest(MockRendezVous.new(newRng()))
asyncTest "Check timeToAdvertise interval when there is an error":
await baseTimeToAdvertiseTest(MockErrorRendezVous.new(newRng()))