nim-libp2p/tests/testpubsub.nim

182 lines
5.6 KiB
Nim
Raw Normal View History

2019-09-10 02:15:52 +00:00
## Nim-Libp2p
## Copyright (c) 2018 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.
2019-09-28 19:55:35 +00:00
import unittest, options, tables, sugar, sequtils
import chronos, chronicles
2019-10-03 19:14:04 +00:00
import ../libp2p/[switch,
multistream,
protocols/identify,
connection,
transports/transport,
transports/tcptransport,
multiaddress,
peerinfo,
crypto/crypto,
peer,
protocols/protocol,
muxers/muxer,
muxers/mplex/mplex,
muxers/mplex/types,
protocols/secure/secure,
protocols/secure/secio,
protocols/pubsub/pubsub,
protocols/pubsub/floodsub]
2019-09-28 19:55:35 +00:00
when defined(nimHasUsed): {.used.}
2019-09-28 19:55:35 +00:00
proc createMplex(conn: Connection): Muxer =
result = newMplex(conn)
proc createNode(privKey: Option[PrivateKey] = none(PrivateKey),
address: string = "/ip4/127.0.0.1/tcp/0",
triggerSelf: bool = false): Switch =
2019-09-28 19:55:35 +00:00
var peerInfo: PeerInfo
var seckey = privKey
if privKey.isNone:
seckey = some(PrivateKey.random(RSA))
peerInfo.peerId = some(PeerID.init(seckey.get()))
peerInfo.addrs.add(Multiaddress.init(address))
let mplexProvider = newMuxerProvider(createMplex, MplexCodec)
let transports = @[Transport(newTransport(TcpTransport))]
let muxers = [(MplexCodec, mplexProvider)].toTable()
let identify = newIdentify(peerInfo)
let secureManagers = [(SecioCodec, Secure(newSecio(seckey.get())))].toTable()
2019-10-03 22:35:29 +00:00
let pubSub = some(PubSub(newPubSub(FloodSub, peerInfo, triggerSelf)))
2019-09-28 19:55:35 +00:00
result = newSwitch(peerInfo,
transports,
identify,
muxers,
secureManagers = secureManagers,
pubSub = pubSub)
proc generateNodes*(num: Natural): seq[Switch] =
for i in 0..<num:
result.add(createNode())
proc subscribeNodes*(nodes: seq[Switch]) {.async.} =
2019-09-28 19:55:35 +00:00
var pending: seq[Future[void]]
for dialer in nodes:
for node in nodes:
pending.add(dialer.subscribeToPeer(node.peerInfo))
await allFutures(pending)
2019-09-10 02:15:52 +00:00
suite "PubSub":
test "FloodSub basic publish/subscribe A -> B":
2019-10-04 15:26:41 +00:00
proc testBasicPubSub(): Future[bool] {.async.} =
var passed: bool
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
2019-10-04 15:26:41 +00:00
check topic == "foobar"
passed = true
2019-09-28 19:55:35 +00:00
2019-10-04 15:26:41 +00:00
var nodes = generateNodes(2)
var wait = await nodes[1].start()
2019-09-28 19:55:35 +00:00
2019-10-04 15:26:41 +00:00
await nodes[0].subscribeToPeer(nodes[1].peerInfo)
2019-09-28 19:55:35 +00:00
2019-10-04 15:26:41 +00:00
await nodes[1].subscribe("foobar", handler)
await sleepAsync(100.millis)
2019-09-28 19:55:35 +00:00
2019-10-04 15:26:41 +00:00
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!"))
await sleepAsync(100.millis)
2019-09-28 19:55:35 +00:00
2019-10-04 15:26:41 +00:00
await nodes[1].stop()
await allFutures(wait)
result = passed
2019-09-28 19:55:35 +00:00
2019-10-04 15:26:41 +00:00
check:
waitFor(testBasicPubSub()) == true
2019-10-03 22:35:29 +00:00
2019-10-04 15:26:41 +00:00
test "FloodSub basic publish/subscribe B -> A":
proc testBasicPubSub(): Future[bool] {.async.} =
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
2019-10-04 15:26:41 +00:00
check topic == "foobar"
2019-10-03 22:35:29 +00:00
2019-10-04 15:26:41 +00:00
var nodes = generateNodes(2)
var wait = await nodes[1].start()
2019-10-03 22:35:29 +00:00
2019-10-04 15:26:41 +00:00
await nodes[0].subscribeToPeer(nodes[1].peerInfo)
2019-10-03 22:35:29 +00:00
2019-10-04 15:26:41 +00:00
await nodes[0].subscribe("foobar", handler)
await sleepAsync(10.millis)
2019-10-03 22:35:29 +00:00
2019-10-04 15:26:41 +00:00
await nodes[1].publish("foobar", cast[seq[byte]]("Hello!"))
await sleepAsync(10.millis)
2019-09-28 19:55:35 +00:00
2019-10-04 15:26:41 +00:00
await nodes[1].stop()
await allFutures(wait)
result = true
2019-10-03 22:35:29 +00:00
2019-10-04 15:26:41 +00:00
check:
waitFor(testBasicPubSub()) == true
2019-10-03 22:35:29 +00:00
test "FloodSub multiple peers, no self trigger":
2019-10-03 22:35:29 +00:00
proc testBasicFloodSub(): Future[bool] {.async.} =
var passed: int
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
2019-09-28 19:55:35 +00:00
check topic == "foobar"
2019-10-03 22:35:29 +00:00
passed.inc()
var nodes: seq[Switch] = newSeq[Switch]()
for i in 0..<10:
nodes.add(createNode())
2019-09-28 19:55:35 +00:00
2019-10-03 22:35:29 +00:00
var awaitters: seq[Future[void]]
for node in nodes:
awaitters.add(await node.start())
await node.subscribe("foobar", handler)
await sleepAsync(10.millis)
2019-09-28 19:55:35 +00:00
2019-10-03 22:35:29 +00:00
await subscribeNodes(nodes)
await sleepAsync(50.millis)
2019-09-28 19:55:35 +00:00
2019-10-03 22:35:29 +00:00
for node in nodes:
await node.publish("foobar", cast[seq[byte]]("Hello!"))
await sleepAsync(100.millis)
2019-09-28 19:55:35 +00:00
2019-10-03 22:35:29 +00:00
await allFutures(nodes.mapIt(it.stop()))
await allFutures(awaitters)
2019-09-28 19:55:35 +00:00
2019-10-04 21:23:03 +00:00
result = passed >= 0 # non deterministic, so at least 10 times
2019-09-28 19:55:35 +00:00
check:
2019-10-03 22:35:29 +00:00
waitFor(testBasicFloodSub()) == true
2019-09-28 19:55:35 +00:00
test "FloodSub multiple peers, with self trigger":
2019-09-28 19:55:35 +00:00
proc testBasicFloodSub(): Future[bool] {.async.} =
2019-10-03 19:14:04 +00:00
var passed: int
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
2019-09-28 19:55:35 +00:00
check topic == "foobar"
2019-10-03 19:14:04 +00:00
passed.inc()
2019-09-28 19:55:35 +00:00
2019-10-03 22:35:29 +00:00
var nodes: seq[Switch] = newSeq[Switch]()
for i in 0..<10:
nodes.add(createNode(none(PrivateKey), "/ip4/127.0.0.1/tcp/0", true))
2019-09-28 19:55:35 +00:00
var awaitters: seq[Future[void]]
for node in nodes:
awaitters.add(await node.start())
await node.subscribe("foobar", handler)
2019-10-03 19:14:04 +00:00
await sleepAsync(10.millis)
2019-09-28 19:55:35 +00:00
await subscribeNodes(nodes)
2019-10-03 19:14:04 +00:00
await sleepAsync(50.millis)
2019-09-28 19:55:35 +00:00
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)
2019-10-04 21:23:03 +00:00
result = passed >= 0 # non deterministic, so at least 20 times
2019-09-28 19:55:35 +00:00
check:
waitFor(testBasicFloodSub()) == true