fix(tests): testautorelay (#1121)
This commit is contained in:
parent
96bfefc928
commit
3bf8a2907f
|
@ -22,7 +22,7 @@ type
|
|||
running: bool
|
||||
runner: Future[void]
|
||||
client: RelayClient
|
||||
numRelays: int
|
||||
maxNumRelays: int # maximum number of relays we can reserve at the same time
|
||||
relayPeers: Table[PeerId, Future[void]]
|
||||
relayAddresses: Table[PeerId, seq[MultiAddress]]
|
||||
backingOff: seq[PeerId]
|
||||
|
@ -68,7 +68,7 @@ method setup*(self: AutoRelayService, switch: Switch): Future[bool] {.async.} =
|
|||
if hasBeenSetUp:
|
||||
proc handlePeerJoined(peerId: PeerId, event: PeerEvent) {.async.} =
|
||||
trace "Peer Joined", peerId
|
||||
if self.relayPeers.len < self.numRelays:
|
||||
if self.relayPeers.len < self.maxNumRelays:
|
||||
self.peerAvailable.fire()
|
||||
|
||||
proc handlePeerLeft(peerId: PeerId, event: PeerEvent) {.async.} =
|
||||
|
@ -111,7 +111,7 @@ proc innerRun(self: AutoRelayService, switch: Switch) {.async.} =
|
|||
self.rng.shuffle(connectedPeers)
|
||||
|
||||
for relayPid in connectedPeers:
|
||||
if self.relayPeers.len() >= self.numRelays:
|
||||
if self.relayPeers.len() >= self.maxNumRelays:
|
||||
break
|
||||
self.relayPeers[relayPid] = self.reserveAndUpdate(relayPid, switch)
|
||||
|
||||
|
@ -142,13 +142,13 @@ proc getAddresses*(self: AutoRelayService): seq[MultiAddress] =
|
|||
|
||||
proc new*(
|
||||
T: typedesc[AutoRelayService],
|
||||
numRelays: int,
|
||||
maxNumRelays: int,
|
||||
client: RelayClient,
|
||||
onReservation: OnReservationHandler,
|
||||
rng: ref HmacDrbgContext,
|
||||
): T =
|
||||
T(
|
||||
numRelays: numRelays,
|
||||
maxNumRelays: maxNumRelays,
|
||||
client: client,
|
||||
onReservation: onReservation,
|
||||
peerAvailable: newAsyncEvent(),
|
||||
|
|
|
@ -24,7 +24,7 @@ proc createSwitch(r: Relay, autorelay: Service = nil): Switch =
|
|||
var builder = SwitchBuilder
|
||||
.new()
|
||||
.withRng(newRng())
|
||||
.withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()], false)
|
||||
.withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()])
|
||||
.withTcpTransport()
|
||||
.withMplex()
|
||||
.withNoise()
|
||||
|
@ -33,13 +33,17 @@ proc createSwitch(r: Relay, autorelay: Service = nil): Switch =
|
|||
builder = builder.withServices(@[autorelay])
|
||||
builder.build()
|
||||
|
||||
proc buildRelayMA(switchRelay: Switch, switchClient: Switch): MultiAddress =
|
||||
MultiAddress
|
||||
.init(
|
||||
$switchRelay.peerInfo.addrs[0] & "/p2p/" & $switchRelay.peerInfo.peerId &
|
||||
"/p2p-circuit"
|
||||
)
|
||||
.get()
|
||||
proc buildRelayMA(switchRelay: Switch, switchClient: Switch): seq[MultiAddress] =
|
||||
result = newSeq[MultiAddress]()
|
||||
for i in 0 ..< switchRelay.peerInfo.addrs.len():
|
||||
result.add(
|
||||
MultiAddress
|
||||
.init(
|
||||
$switchRelay.peerInfo.addrs[i] & "/p2p/" & $switchRelay.peerInfo.peerId &
|
||||
"/p2p-circuit"
|
||||
)
|
||||
.get()
|
||||
)
|
||||
|
||||
suite "Autorelay":
|
||||
asyncTeardown:
|
||||
|
@ -57,9 +61,7 @@ suite "Autorelay":
|
|||
let fut = newFuture[void]()
|
||||
proc checkMA(addresses: seq[MultiAddress]) =
|
||||
check:
|
||||
addresses[0] == buildRelayMA(switchRelay, switchClient)
|
||||
check:
|
||||
addresses.len() == 1
|
||||
addresses == buildRelayMA(switchRelay, switchClient)
|
||||
fut.complete()
|
||||
|
||||
autorelay = AutoRelayService.new(3, relayClient, checkMA, newRng())
|
||||
|
@ -69,8 +71,7 @@ suite "Autorelay":
|
|||
await fut.wait(1.seconds)
|
||||
let addresses = autorelay.getAddresses()
|
||||
check:
|
||||
addresses[0] == buildRelayMA(switchRelay, switchClient)
|
||||
addresses.len() == 1
|
||||
addresses == buildRelayMA(switchRelay, switchClient)
|
||||
await allFutures(switchClient.stop(), switchRelay.stop())
|
||||
|
||||
asyncTest "Connect after starting switches":
|
||||
|
@ -79,7 +80,7 @@ suite "Autorelay":
|
|||
let fut = newFuture[void]()
|
||||
proc checkMA(address: seq[MultiAddress]) =
|
||||
check:
|
||||
address[0] == buildRelayMA(switchRelay, switchClient)
|
||||
address == buildRelayMA(switchRelay, switchClient)
|
||||
fut.complete()
|
||||
|
||||
let autorelay = AutoRelayService.new(3, relayClient, checkMA, newRng())
|
||||
|
@ -89,55 +90,73 @@ suite "Autorelay":
|
|||
await switchClient.connect(switchRelay.peerInfo.peerId, switchRelay.peerInfo.addrs)
|
||||
await fut.wait(1.seconds)
|
||||
let addresses = autorelay.getAddresses()
|
||||
|
||||
check:
|
||||
addresses == @[buildRelayMA(switchRelay, switchClient)]
|
||||
addresses.len() == 1
|
||||
addresses[0] in switchClient.peerInfo.addrs
|
||||
addresses == buildRelayMA(switchRelay, switchClient)
|
||||
for address in addresses:
|
||||
check address in switchClient.peerInfo.addrs
|
||||
|
||||
await allFutures(switchClient.stop(), switchRelay.stop())
|
||||
|
||||
check addresses != switchClient.peerInfo.addrs
|
||||
|
||||
asyncTest "Three relays connections":
|
||||
var state = 0
|
||||
type RelayReservationState = enum
|
||||
Relay1Reserved
|
||||
Relay1AndRelay2Reserved
|
||||
# Although switchClient is connected to rel3, rel3 isn't reserved due to the maximum number of relays set to 2.
|
||||
Relay2UnreservedAndRelay1Reserved
|
||||
Relay1AndRelay3Reserved
|
||||
|
||||
var state = Relay1Reserved
|
||||
let
|
||||
rel1 = createSwitch(Relay.new())
|
||||
rel2 = createSwitch(Relay.new())
|
||||
rel3 = createSwitch(Relay.new())
|
||||
fut = newFuture[void]()
|
||||
rel1Checked = newFuture[void]()
|
||||
rel1And2Checked = newFuture[void]()
|
||||
allChecksCompleted = newFuture[void]()
|
||||
relayClient = RelayClient.new()
|
||||
proc checkMA(addresses: seq[MultiAddress]) =
|
||||
if state == 0 or state == 2:
|
||||
check:
|
||||
buildRelayMA(rel1, switchClient) in addresses
|
||||
addresses.len() == 1
|
||||
state += 1
|
||||
elif state == 1:
|
||||
check:
|
||||
buildRelayMA(rel1, switchClient) in addresses
|
||||
buildRelayMA(rel2, switchClient) in addresses
|
||||
addresses.len() == 2
|
||||
state += 1
|
||||
elif state == 3:
|
||||
check:
|
||||
buildRelayMA(rel1, switchClient) in addresses
|
||||
buildRelayMA(rel3, switchClient) in addresses
|
||||
addresses.len() == 2
|
||||
state += 1
|
||||
fut.complete()
|
||||
if state == Relay1Reserved or state == Relay2UnreservedAndRelay1Reserved:
|
||||
let relayMAs = buildRelayMA(rel1, switchClient)
|
||||
for relayMA in relayMAs:
|
||||
check:
|
||||
relayMA in addresses
|
||||
if state == Relay1Reserved:
|
||||
state = Relay1AndRelay2Reserved
|
||||
rel1Checked.complete()
|
||||
elif state == Relay2UnreservedAndRelay1Reserved:
|
||||
state = Relay1AndRelay3Reserved
|
||||
elif state == Relay1AndRelay2Reserved:
|
||||
let relay1MAs = buildRelayMA(rel1, switchClient)
|
||||
for relayMA in relay1MAs:
|
||||
check:
|
||||
relayMA in addresses
|
||||
let relay2MAs = buildRelayMA(rel2, switchClient)
|
||||
for relayMA in relay2MAs:
|
||||
check:
|
||||
relayMA in addresses
|
||||
state = Relay2UnreservedAndRelay1Reserved
|
||||
rel1And2Checked.complete()
|
||||
elif state == Relay1AndRelay3Reserved:
|
||||
let relay1MAs = buildRelayMA(rel1, switchClient)
|
||||
for relayMA in relay1MAs:
|
||||
check:
|
||||
relayMA in addresses
|
||||
let relay3MAs = buildRelayMA(rel3, switchClient)
|
||||
for relayMA in relay3MAs:
|
||||
check:
|
||||
relayMA in addresses
|
||||
allChecksCompleted.complete()
|
||||
|
||||
let autorelay = AutoRelayService.new(2, relayClient, checkMA, newRng())
|
||||
let autorelay =
|
||||
AutoRelayService.new(maxNumRelays = 2, relayClient, checkMA, newRng())
|
||||
switchClient = createSwitch(relayClient, autorelay)
|
||||
await allFutures(switchClient.start(), rel1.start(), rel2.start(), rel3.start())
|
||||
await switchClient.connect(rel1.peerInfo.peerId, rel1.peerInfo.addrs)
|
||||
await sleepAsync(500.millis)
|
||||
await rel1Checked.wait(500.millis)
|
||||
await switchClient.connect(rel2.peerInfo.peerId, rel2.peerInfo.addrs)
|
||||
await switchClient.connect(rel3.peerInfo.peerId, rel3.peerInfo.addrs)
|
||||
await sleepAsync(500.millis)
|
||||
await rel1And2Checked.wait(500.millis)
|
||||
await rel2.stop()
|
||||
await fut.wait(1.seconds)
|
||||
let addresses = autorelay.getAddresses()
|
||||
check:
|
||||
buildRelayMA(rel1, switchClient) in addresses
|
||||
buildRelayMA(rel3, switchClient) in addresses
|
||||
addresses.len() == 2
|
||||
await allChecksCompleted.wait(1.seconds)
|
||||
await allFutures(switchClient.stop(), rel1.stop(), rel3.stop())
|
||||
|
|
Loading…
Reference in New Issue