2021-06-02 07:53:34 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2022-11-04 09:52:08 +00:00
|
|
|
std/options,
|
2021-06-02 07:53:34 +00:00
|
|
|
stew/shims/net as stewNet,
|
2023-02-13 10:43:49 +00:00
|
|
|
testutils/unittests,
|
|
|
|
chronos,
|
2022-11-04 09:52:08 +00:00
|
|
|
chronicles,
|
2021-06-02 07:53:34 +00:00
|
|
|
libp2p/switch,
|
2021-06-15 08:55:47 +00:00
|
|
|
libp2p/protocols/ping,
|
2023-02-13 10:43:49 +00:00
|
|
|
libp2p/stream/bufferstream,
|
2022-11-04 09:52:08 +00:00
|
|
|
libp2p/stream/connection,
|
|
|
|
libp2p/crypto/crypto
|
2024-07-05 22:03:38 +00:00
|
|
|
import waku/waku_core, waku/waku_node, ./testlib/wakucore, ./testlib/wakunode
|
2021-06-02 07:53:34 +00:00
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
suite "Waku Keepalive":
|
2021-06-15 08:55:47 +00:00
|
|
|
asyncTest "handle ping keepalives":
|
2021-06-02 07:53:34 +00:00
|
|
|
let
|
2023-02-13 10:43:49 +00:00
|
|
|
nodeKey1 = generateSecp256k1Key()
|
2023-12-14 06:16:39 +00:00
|
|
|
node1 = newTestWakuNode(nodeKey1, parseIpAddress("0.0.0.0"), Port(0))
|
2023-02-13 10:43:49 +00:00
|
|
|
nodeKey2 = generateSecp256k1Key()
|
2023-12-14 06:16:39 +00:00
|
|
|
node2 = newTestWakuNode(nodeKey2, parseIpAddress("0.0.0.0"), Port(0))
|
2021-06-02 07:53:34 +00:00
|
|
|
|
2021-06-15 08:55:47 +00:00
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
proc pingHandler(peerId: PeerID) {.async, gcsafe.} =
|
2021-06-15 08:55:47 +00:00
|
|
|
debug "Ping received"
|
|
|
|
|
|
|
|
check:
|
2021-10-06 12:29:08 +00:00
|
|
|
peerId == node1.switch.peerInfo.peerId
|
2021-06-15 08:55:47 +00:00
|
|
|
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
2021-06-02 07:53:34 +00:00
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay()
|
|
|
|
await node1.mountLibp2pPing()
|
2021-06-02 07:53:34 +00:00
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay()
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
let pingProto = Ping.new(handler = pingHandler)
|
|
|
|
await pingProto.start()
|
|
|
|
node2.switch.mount(pingProto)
|
2021-06-02 07:53:34 +00:00
|
|
|
|
2022-01-10 15:07:35 +00:00
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
2021-06-02 07:53:34 +00:00
|
|
|
|
|
|
|
node1.startKeepalive()
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await completionFut.withTimeout(5.seconds)) == true
|
|
|
|
|
2022-05-20 10:57:19 +00:00
|
|
|
await node2.stop()
|
|
|
|
await node1.stop()
|