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