Implement rendezvous frequent sub/unsub tests.

This commit is contained in:
Alejandro Cabeza Romero 2024-08-14 17:39:32 +02:00
parent 788e156800
commit f791e78cda
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
3 changed files with 41 additions and 3 deletions

View File

@ -18,7 +18,7 @@ import
discovery/discoverymngr, discovery/discoverymngr,
discovery/rendezvousinterface, discovery/rendezvousinterface,
] ]
import ./helpers, ./asyncunit, ./utils/async, ./utils/assertions import ./helpers, ./asyncunit, ./utils/[async, assertions, futures]
proc createSwitch(rdv: RendezVous = RendezVous.new()): Switch = proc createSwitch(rdv: RendezVous = RendezVous.new()): Switch =
SwitchBuilder SwitchBuilder
@ -95,7 +95,7 @@ suite "Discovery":
res2.assertIsErr() res2.assertIsErr()
asyncTest "Frequent sub/desub": asyncTest "Frequent sub/unsub":
for i in 0 ..< 10: for i in 0 ..< 10:
dmB.advertise(rdvNamespace) dmB.advertise(rdvNamespace)
let let
@ -106,8 +106,45 @@ suite "Discovery":
check res1.value{PeerId}.get() == clientB.peerInfo.peerId check res1.value{PeerId}.get() == clientB.peerInfo.peerId
await rdvB.unsubscribe(namespace) await rdvB.unsubscribe(namespace)
await sleepAsync(TIMEOUT_EXTENDED)
var var
query2 = dmA.request(rdvNamespace) query2 = dmA.request(rdvNamespace)
res2 = await query2.getPeer().waitForResult(1.seconds) res2 = await query2.getPeer().waitForResult(1.seconds)
res2.assertIsErr() 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()

View File

@ -4,7 +4,7 @@ proc assertIsOk*[T, E](res: Result[T, E]) =
assert res.isOk, res.error assert res.isOk, res.error
proc assertIsErr*[T, E](res: Result[T, E], error: Option[E] = E.none()) = 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(): if error.isSome():
assert res.error == error.get(), assert res.error == error.get(),
"Result was \"" & res.error & "\" but expected \"" & error.get() & "\"" "Result was \"" & res.error & "\" but expected \"" & error.get() & "\""

1
tests/utils/futures.nim Normal file
View File

@ -0,0 +1 @@
const TIMEOUT_EXTENDED* = 1500.milliseconds