add tests for self trigger

This commit is contained in:
Dmitriy Ryajov 2019-10-03 16:35:29 -06:00
parent 1d4b51413e
commit b281f46ee5
1 changed files with 74 additions and 38 deletions

View File

@ -32,7 +32,8 @@ proc createMplex(conn: Connection): Muxer =
result = newMplex(conn) result = newMplex(conn)
proc createNode(privKey: Option[PrivateKey] = none(PrivateKey), proc createNode(privKey: Option[PrivateKey] = none(PrivateKey),
address: string = "/ip4/127.0.0.1/tcp/0"): Switch = address: string = "/ip4/127.0.0.1/tcp/0",
triggerSelf: bool = false): Switch =
var peerInfo: PeerInfo var peerInfo: PeerInfo
var seckey = privKey var seckey = privKey
if privKey.isNone: if privKey.isNone:
@ -46,7 +47,7 @@ proc createNode(privKey: Option[PrivateKey] = none(PrivateKey),
let muxers = [(MplexCodec, mplexProvider)].toTable() let muxers = [(MplexCodec, mplexProvider)].toTable()
let identify = newIdentify(peerInfo) let identify = newIdentify(peerInfo)
let secureManagers = [(SecioCodec, Secure(newSecio(seckey.get())))].toTable() let secureManagers = [(SecioCodec, Secure(newSecio(seckey.get())))].toTable()
let pubSub = some(PubSub(newFloodSub(peerInfo))) let pubSub = some(PubSub(newPubSub(FloodSub, peerInfo, triggerSelf)))
result = newSwitch(peerInfo, result = newSwitch(peerInfo,
transports, transports,
identify, identify,
@ -66,62 +67,65 @@ proc subscribeNodes*(nodes: seq[Switch]) {.async.} =
await allFutures(pending) await allFutures(pending)
suite "PubSub": suite "PubSub":
test "FloodSub basic publish/subscribe A -> B": # test "FloodSub basic publish/subscribe A -> B":
proc testBasicPubSub(): Future[bool] {.async.} = # proc testBasicPubSub(): Future[bool] {.async.} =
var passed: bool # var passed: bool
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = # proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
check topic == "foobar" # check topic == "foobar"
passed = true # passed = true
var nodes = generateNodes(2) # var nodes = generateNodes(2)
var wait = await nodes[1].start() # var wait = await nodes[1].start()
await nodes[0].subscribeToPeer(nodes[1].peerInfo) # await nodes[0].subscribeToPeer(nodes[1].peerInfo)
await nodes[1].subscribe("foobar", handler) # await nodes[1].subscribe("foobar", handler)
await sleepAsync(100.millis) # await sleepAsync(100.millis)
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!")) # await nodes[0].publish("foobar", cast[seq[byte]]("Hello!"))
await sleepAsync(100.millis) # await sleepAsync(100.millis)
await nodes[1].stop() # await nodes[1].stop()
await allFutures(wait) # await allFutures(wait)
result = passed # result = passed
check: # check:
waitFor(testBasicPubSub()) == true # waitFor(testBasicPubSub()) == true
test "FloodSub basic publish/subscribe B -> A": # test "FloodSub basic publish/subscribe B -> A":
proc testBasicPubSub(): Future[bool] {.async.} = # proc testBasicPubSub(): Future[bool] {.async.} =
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = # proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
check topic == "foobar" # check topic == "foobar"
var nodes = generateNodes(2) # var nodes = generateNodes(2)
var wait = await nodes[1].start() # var wait = await nodes[1].start()
await nodes[0].subscribeToPeer(nodes[1].peerInfo) # await nodes[0].subscribeToPeer(nodes[1].peerInfo)
await nodes[0].subscribe("foobar", handler) # await nodes[0].subscribe("foobar", handler)
await sleepAsync(10.millis) # await sleepAsync(10.millis)
await nodes[1].publish("foobar", cast[seq[byte]]("Hello!")) # await nodes[1].publish("foobar", cast[seq[byte]]("Hello!"))
await sleepAsync(10.millis) # await sleepAsync(10.millis)
await nodes[1].stop() # await nodes[1].stop()
await allFutures(wait) # await allFutures(wait)
result = true # result = true
check: # check:
waitFor(testBasicPubSub()) == true # waitFor(testBasicPubSub()) == true
test "FloodSub multiple peers": test "FloodSub multiple peers, no self trigger":
proc testBasicFloodSub(): Future[bool] {.async.} = proc testBasicFloodSub(): Future[bool] {.async.} =
var passed: int var passed: int
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
check topic == "foobar" check topic == "foobar"
passed.inc() passed.inc()
var nodes: seq[Switch] = generateNodes(10) var nodes: seq[Switch] = newSeq[Switch]()
for i in 0..<10:
nodes.add(createNode())
var awaitters: seq[Future[void]] var awaitters: seq[Future[void]]
for node in nodes: for node in nodes:
awaitters.add(await node.start()) awaitters.add(await node.start())
@ -142,3 +146,35 @@ suite "PubSub":
check: check:
waitFor(testBasicFloodSub()) == true waitFor(testBasicFloodSub()) == true
test "FloodSub multiple peers, with self trigger":
proc testBasicFloodSub(): Future[bool] {.async.} =
var passed: int
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
check topic == "foobar"
passed.inc()
var nodes: seq[Switch] = newSeq[Switch]()
for i in 0..<10:
nodes.add(createNode(none(PrivateKey), "/ip4/127.0.0.1/tcp/0", true))
var awaitters: seq[Future[void]]
for node in nodes:
awaitters.add(await node.start())
await node.subscribe("foobar", handler)
await sleepAsync(10.millis)
await subscribeNodes(nodes)
await sleepAsync(50.millis)
for node in nodes:
await node.publish("foobar", cast[seq[byte]]("Hello!"))
await sleepAsync(100.millis)
await allFutures(nodes.mapIt(it.stop()))
await allFutures(awaitters)
result = passed == 20
check:
waitFor(testBasicFloodSub()) == true