From f791e78cda1facfc3ab7196b9ec3eb760339e6c0 Mon Sep 17 00:00:00 2001 From: Alejandro Cabeza Romero Date: Wed, 14 Aug 2024 17:39:32 +0200 Subject: [PATCH] Implement rendezvous frequent sub/unsub tests. --- tests/testdiscovery.nim | 41 ++++++++++++++++++++++++++++++++++++-- tests/utils/assertions.nim | 2 +- tests/utils/futures.nim | 1 + 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 tests/utils/futures.nim diff --git a/tests/testdiscovery.nim b/tests/testdiscovery.nim index 8f62cc3ac..9f2fd9279 100644 --- a/tests/testdiscovery.nim +++ b/tests/testdiscovery.nim @@ -18,7 +18,7 @@ import discovery/discoverymngr, discovery/rendezvousinterface, ] -import ./helpers, ./asyncunit, ./utils/async, ./utils/assertions +import ./helpers, ./asyncunit, ./utils/[async, assertions, futures] proc createSwitch(rdv: RendezVous = RendezVous.new()): Switch = SwitchBuilder @@ -95,7 +95,7 @@ suite "Discovery": res2.assertIsErr() - asyncTest "Frequent sub/desub": + asyncTest "Frequent sub/unsub": for i in 0 ..< 10: dmB.advertise(rdvNamespace) let @@ -106,8 +106,45 @@ suite "Discovery": check res1.value{PeerId}.get() == clientB.peerInfo.peerId await rdvB.unsubscribe(namespace) + await sleepAsync(TIMEOUT_EXTENDED) var query2 = dmA.request(rdvNamespace) res2 = await query2.getPeer().waitForResult(1.seconds) res2.assertIsErr() + + asyncTest "Frequent sub/unsub with multiple clients": + let + rdvC = RendezVous.new() + clientC = createSwitch(rdvC) + dmC = DiscoveryManager() + + dmC.add(RendezVousInterface.new(rdvC)) + await clientC.start() + + await clientC.connect(remoteNode.peerInfo.peerId, remoteNode.peerInfo.addrs) + + for i in 0 ..< 10: + dmB.advertise(rdvNamespace) + dmC.advertise(rdvNamespace) + let peerIds = + @[clientB.peerInfo.peerId, clientC.peerInfo.peerId] + # peerIds = @[clientB.peerInfo.peerId] + + let + query1 = dmA.request(rdvNamespace) + res1 = await query1.getPeer().waitForResult(1.seconds) + + res1.assertIsOk() + check res1.value{PeerId}.get() in peerIds + + await rdvB.unsubscribe(namespace) + await rdvC.unsubscribe(namespace) + await sleepAsync(TIMEOUT_EXTENDED) + var + query2 = dmA.request(rdvNamespace) + res2 = await query2.getPeer().waitForResult(1.seconds) + + res2.assertIsErr() + + await clientC.stop() diff --git a/tests/utils/assertions.nim b/tests/utils/assertions.nim index 86e97fea2..b09f1971e 100644 --- a/tests/utils/assertions.nim +++ b/tests/utils/assertions.nim @@ -4,7 +4,7 @@ proc assertIsOk*[T, E](res: Result[T, E]) = assert res.isOk, res.error proc assertIsErr*[T, E](res: Result[T, E], error: Option[E] = E.none()) = - assert res.isErr, "Result was \"Err\" but expected \"Ok\"" + assert res.isErr, "Result was \"Ok\" but expected \"Err\"" if error.isSome(): assert res.error == error.get(), "Result was \"" & res.error & "\" but expected \"" & error.get() & "\"" diff --git a/tests/utils/futures.nim b/tests/utils/futures.nim new file mode 100644 index 000000000..43f276e6a --- /dev/null +++ b/tests/utils/futures.nim @@ -0,0 +1 @@ +const TIMEOUT_EXTENDED* = 1500.milliseconds