Skip to content

Commit

Permalink
Fix rendezvous frequent sub/unsub tests and implement frequent sub/un…
Browse files Browse the repository at this point in the history
…sub from multiple clients.
  • Loading branch information
AlejandroCabeza committed Aug 14, 2024
1 parent 5063cab commit 1852506
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
41 changes: 39 additions & 2 deletions tests/testdiscovery.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
2 changes: 1 addition & 1 deletion tests/utils/assertions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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() & "\""
1 change: 1 addition & 0 deletions tests/utils/futures.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const TIMEOUT_EXTENDED* = 1500.milliseconds

0 comments on commit 1852506

Please sign in to comment.