2021-04-23 06:56:04 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2022-08-02 23:47:42 +00:00
|
|
|
testutils/unittests,
|
|
|
|
chronos,
|
|
|
|
libp2p/crypto/crypto
|
|
|
|
import
|
2021-04-23 06:56:04 +00:00
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
2022-08-02 23:47:42 +00:00
|
|
|
../../waku/v2/protocol/waku_message,
|
|
|
|
../../waku/v2/protocol/waku_lightpush,
|
2022-10-25 12:55:31 +00:00
|
|
|
../../waku/v2/protocol/waku_lightpush/client,
|
|
|
|
./testlib/common,
|
|
|
|
./testlib/switch
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
proc newTestWakuLightpushNode(switch: Switch, handler: PushMessageHandler): Future[WakuLightPush] {.async.} =
|
|
|
|
let
|
|
|
|
peerManager = PeerManager.new(switch)
|
|
|
|
rng = crypto.newRng()
|
|
|
|
proto = WakuLightPush.new(peerManager, rng, handler)
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
await proto.start()
|
|
|
|
switch.mount(proto)
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
return proto
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
proc newTestWakuLightpushClient(switch: Switch): WakuLightPushClient =
|
|
|
|
let
|
|
|
|
peerManager = PeerManager.new(switch)
|
|
|
|
rng = crypto.newRng()
|
|
|
|
WakuLightPushClient.new(peerManager, rng)
|
2021-04-23 06:56:04 +00:00
|
|
|
|
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
suite "Waku Lightpush":
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
asyncTest "push message to pubsub topic is successful":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
## Given
|
|
|
|
let handlerFuture = newFuture[(string, WakuMessage)]()
|
2022-11-09 08:55:47 +00:00
|
|
|
let handler = proc(peer: PeerId, pubsubTopic: PubsubTopic, message: WakuMessage): Future[WakuLightPushResult[void]] {.async.} =
|
2022-10-25 12:55:31 +00:00
|
|
|
handlerFuture.complete((pubsubTopic, message))
|
|
|
|
return ok()
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
let
|
|
|
|
server = await newTestWakuLightpushNode(serverSwitch, handler)
|
|
|
|
client = newTestWakuLightpushClient(clientSwitch)
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
let serverPeerId = serverSwitch.peerInfo.toRemotePeerInfo()
|
2021-04-23 06:56:04 +00:00
|
|
|
|
|
|
|
let
|
2022-10-25 12:55:31 +00:00
|
|
|
topic = DefaultPubsubTopic
|
|
|
|
message = fakeWakuMessage()
|
|
|
|
|
|
|
|
## When
|
2022-10-28 14:30:01 +00:00
|
|
|
let requestRes = await client.publish(topic, message, peer=serverPeerId)
|
2022-10-25 12:55:31 +00:00
|
|
|
|
|
|
|
require await handlerFuture.withTimeout(100.millis)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
requestRes.isOk()
|
|
|
|
handlerFuture.finished()
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
let (handledMessagePubsubTopic, handledMessage) = handlerFuture.read()
|
|
|
|
check:
|
|
|
|
handledMessagePubsubTopic == topic
|
|
|
|
handledMessage == message
|
|
|
|
|
|
|
|
## Cleanup
|
|
|
|
await allFutures(clientSwitch.stop(), serverSwitch.stop())
|
|
|
|
|
|
|
|
asyncTest "push message to pubsub topic should fail":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-08-02 23:47:42 +00:00
|
|
|
## Given
|
2022-10-25 12:55:31 +00:00
|
|
|
let error = "test_failure"
|
|
|
|
|
|
|
|
let handlerFuture = newFuture[void]()
|
2022-11-09 08:55:47 +00:00
|
|
|
let handler = proc(peer: PeerId, pubsubTopic: PubsubTopic, message: WakuMessage): Future[WakuLightPushResult[void]] {.async.} =
|
2022-10-25 12:55:31 +00:00
|
|
|
handlerFuture.complete()
|
|
|
|
return err(error)
|
|
|
|
|
2022-08-02 23:47:42 +00:00
|
|
|
let
|
2022-10-25 12:55:31 +00:00
|
|
|
server = await newTestWakuLightpushNode(serverSwitch, handler)
|
|
|
|
client = newTestWakuLightpushClient(clientSwitch)
|
|
|
|
|
|
|
|
let serverPeerId = serverSwitch.peerInfo.toRemotePeerInfo()
|
|
|
|
|
|
|
|
let
|
|
|
|
topic = DefaultPubsubTopic
|
|
|
|
message = fakeWakuMessage()
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
## When
|
2022-10-28 14:30:01 +00:00
|
|
|
let requestRes = await client.publish(topic, message, peer=serverPeerId)
|
2022-10-25 12:55:31 +00:00
|
|
|
|
|
|
|
require await handlerFuture.withTimeout(100.millis)
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-08-02 23:47:42 +00:00
|
|
|
## Then
|
2021-04-23 06:56:04 +00:00
|
|
|
check:
|
2022-10-25 12:55:31 +00:00
|
|
|
requestRes.isErr()
|
|
|
|
handlerFuture.finished()
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
let requestError = requestRes.error
|
|
|
|
check:
|
|
|
|
requestError == error
|
|
|
|
|
2022-08-02 23:47:42 +00:00
|
|
|
## Cleanup
|
2022-10-25 12:55:31 +00:00
|
|
|
await allFutures(clientSwitch.stop(), serverSwitch.stop())
|