-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ludovic Chenut <[email protected]>
- Loading branch information
1 parent
56599f5
commit 61929ae
Showing
5 changed files
with
88 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{.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 sequtils, strutils | ||
import chronos | ||
import ../libp2p/[protocols/rendezvous, | ||
switch, | ||
builders,] | ||
import ../libp2p/discovery/[rendezvousinterface, discoverymngr] | ||
import ./helpers | ||
|
||
proc createSwitch(rdv: RendezVous = RendezVous.new()): Switch = | ||
SwitchBuilder.new() | ||
.withRng(newRng()) | ||
.withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) | ||
.withTcpTransport() | ||
.withMplex() | ||
.withNoise() | ||
.withRendezVous(rdv) | ||
.build() | ||
|
||
type | ||
MockRendezVous = ref object of RendezVous | ||
numAdvertiseNs1: int | ||
numAdvertiseNs2: int | ||
|
||
MockErrorRendezVous = ref object of MockRendezVous | ||
|
||
method advertise*(self: MockRendezVous, namespace: string, ttl: Duration) {.async.} = | ||
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.} = | ||
await procCall MockRendezVous(self).advertise(namespace, ttl) | ||
raise newException(CatchableError, "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")) | ||
|
||
checkExpiring: rdv.numAdvertiseNs1 >= 5 | ||
checkExpiring: 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())) |