More autonat tests (#833)

This commit is contained in:
diegomrsantos 2022-12-23 20:10:15 +01:00 committed by GitHub
parent 9532bff983
commit 5e3323d43f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 58 additions and 7 deletions

View File

@ -12,19 +12,22 @@ import chronos, metrics
import unittest2 import unittest2
import ../libp2p/[builders, import ../libp2p/[builders,
switch, switch,
services/autonatservice] services/autonatservice,
protocols/connectivity/autonat]
import ./helpers import ./helpers
import stubs/autonatstub import stubs/autonatstub
proc createSwitch(autonatSvc: Service = nil): Switch = proc createSwitch(autonatSvc: Service = nil, withAutonat = true): Switch =
var builder = SwitchBuilder.new() var builder = SwitchBuilder.new()
.withRng(newRng()) .withRng(newRng())
.withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ])
.withTcpTransport() .withTcpTransport()
.withMplex() .withMplex()
.withAutonat()
.withNoise() .withNoise()
if withAutonat:
builder = builder.withAutonat()
if autonatSvc != nil: if autonatSvc != nil:
builder = builder.withServices(@[autonatSvc]) builder = builder.withServices(@[autonatSvc])
@ -67,18 +70,27 @@ suite "Autonat Service":
asyncTest "Peer must be reachable": asyncTest "Peer must be reachable":
let autonatStub = AutonatStub.new(expectedDials = 3) let autonat = Autonat.new(switch = nil)
autonatStub.answer = Reachable
let autonatService = AutonatService.new(autonatStub, newRng(), some(1.seconds)) let autonatService = AutonatService.new(autonat, newRng(), some(1.seconds))
let switch1 = createSwitch(autonatService) let switch1 = createSwitch(autonatService)
let switch2 = createSwitch() let switch2 = createSwitch()
let switch3 = createSwitch() let switch3 = createSwitch()
let switch4 = createSwitch() let switch4 = createSwitch()
let awaiter = newFuture[void]()
proc statusAndConfidenceHandler(networkReachability: NetworkReachability, confidence: Option[float]) {.gcsafe, async.} =
if networkReachability == NetworkReachability.Reachable and confidence.isSome() and confidence.get() >= 0.3:
if not awaiter.finished:
awaiter.complete()
autonat.switch = switch1
check autonatService.networkReachability() == NetworkReachability.Unknown check autonatService.networkReachability() == NetworkReachability.Unknown
autonatService.statusAndConfidenceHandler(statusAndConfidenceHandler)
await switch1.start() await switch1.start()
await switch2.start() await switch2.start()
await switch3.start() await switch3.start()
@ -88,7 +100,7 @@ suite "Autonat Service":
await switch1.connect(switch3.peerInfo.peerId, switch3.peerInfo.addrs) await switch1.connect(switch3.peerInfo.peerId, switch3.peerInfo.addrs)
await switch1.connect(switch4.peerInfo.peerId, switch4.peerInfo.addrs) await switch1.connect(switch4.peerInfo.peerId, switch4.peerInfo.addrs)
await autonatStub.finished await awaiter
check autonatService.networkReachability() == NetworkReachability.Reachable check autonatService.networkReachability() == NetworkReachability.Reachable
check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 0.3 check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 0.3
@ -141,6 +153,45 @@ suite "Autonat Service":
await allFuturesThrowing(switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop()) await allFuturesThrowing(switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop())
asyncTest "Peer must be reachable when one connected peer has autonat disabled":
let autonat = Autonat.new(switch = nil)
let autonatService = AutonatService.new(autonat, newRng(), some(1.seconds), maxQueueSize = 2)
let switch1 = createSwitch(autonatService)
let switch2 = createSwitch(withAutonat = false)
let switch3 = createSwitch()
let switch4 = createSwitch()
let awaiter = newFuture[void]()
proc statusAndConfidenceHandler(networkReachability: NetworkReachability, confidence: Option[float]) {.gcsafe, async.} =
if networkReachability == NetworkReachability.Reachable and confidence.isSome() and confidence.get() == 1:
if not awaiter.finished:
awaiter.complete()
autonat.switch = switch1
check autonatService.networkReachability() == NetworkReachability.Unknown
autonatService.statusAndConfidenceHandler(statusAndConfidenceHandler)
await switch1.start()
await switch2.start()
await switch3.start()
await switch4.start()
await switch1.connect(switch2.peerInfo.peerId, switch2.peerInfo.addrs)
await switch1.connect(switch3.peerInfo.peerId, switch3.peerInfo.addrs)
await switch1.connect(switch4.peerInfo.peerId, switch4.peerInfo.addrs)
await awaiter
check autonatService.networkReachability() == NetworkReachability.Reachable
check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 1
await allFuturesThrowing(
switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop())
asyncTest "Unknown answers must be ignored": asyncTest "Unknown answers must be ignored":
let autonatStub = AutonatStub.new(expectedDials = 6) let autonatStub = AutonatStub.new(expectedDials = 6)