Skip to content

Commit

Permalink
chore: add unit test for duplicate message push
Browse files Browse the repository at this point in the history
  • Loading branch information
darshankabariya committed Jun 26, 2024
1 parent 99149ea commit 1dfd377
Showing 1 changed file with 145 additions and 0 deletions.
145 changes: 145 additions & 0 deletions tests/wakunode_rest/test_rest_filter.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{.used.}

import
std/[os, times],
stew/byteutils,
stew/shims/net,
testutils/unittests,
Expand Down Expand Up @@ -315,3 +316,147 @@ suite "Waku v2 Rest API - Filter V2":
messages == @[testMessage]

await restFilterTest.shutdown()

asyncTest "duplicate message push to filter subscriber":
# setup filter service and client node
let restFilterTest = await RestFilterTest.init()
let subPeerId = restFilterTest.subscriberNode.peerInfo.toRemotePeerInfo().peerId
restFilterTest.serviceNode.subscribe((kind: PubsubSub, topic: DefaultPubsubTopic))

let requestBody = FilterSubscribeRequest(
requestId: "1001",
contentFilters: @[DefaultContentTopic],
pubsubTopic: some(DefaultPubsubTopic),
)
let response = await restFilterTest.client.filterPostSubscriptions(requestBody)

# subscribe fiter service
let subscribedPeer = restFilterTest.serviceNode.wakuFilter.subscriptions.findSubscribedPeers(
DefaultPubsubTopic, DefaultContentTopic
)

check:
response.status == 200
$response.contentType == $MIMETYPE_JSON
response.data.requestId == "1001"
subscribedPeer.len() == 1

# ping subscriber node
restFilterTest.messageCache.pubsubSubscribe(DefaultPubsubTopic)

let pingResponse = await restFilterTest.client.filterSubscriberPing("1002")

check:
pingResponse.status == 200
pingResponse.data.requestId == "1002"
pingResponse.data.statusDesc == "OK"

# first - message push from service node to subscriber client
let testMessage = WakuMessage(
payload: "TEST-PAYLOAD-MUST-RECEIVE".toBytes(),
contentTopic: DefaultContentTopic,
timestamp: int64(2022),
meta: "test-meta".toBytes(),
)

let postMsgResponse1 = await restFilterTest.clientTwdServiceNode.relayPostMessagesV1(
DefaultPubsubTopic, toRelayWakuMessage(testMessage)
)

# check messages received client side or not
let messages1 = await restFilterTest.client.filterGetMessagesV1(DefaultContentTopic)

check:
postMsgResponse1.status == 200
$postMsgResponse1.contentType == $MIMETYPE_TEXT
postMsgResponse1.data == "OK"
len(messages1.data) == 1

# second - message push from service node to subscriber client
let postMsgResponse2 = await restFilterTest.clientTwdServiceNode.relayPostMessagesV1(
DefaultPubsubTopic, toRelayWakuMessage(testMessage)
)

# check message received client side or not
let messages2 = await restFilterTest.client.filterGetMessagesV1(DefaultContentTopic)

check:
postMsgResponse2.status == 200
$postMsgResponse2.contentType == $MIMETYPE_TEXT
postMsgResponse2.data == "OK"
len(messages2.data) == 0

await restFilterTest.shutdown()

asyncTest "duplicate message push to filter subscriber ( sleep in between )":
# setup filter service and client node
let restFilterTest = await RestFilterTest.init()
let subPeerId = restFilterTest.subscriberNode.peerInfo.toRemotePeerInfo().peerId
restFilterTest.serviceNode.subscribe((kind: PubsubSub, topic: DefaultPubsubTopic))

let requestBody = FilterSubscribeRequest(
requestId: "1001",
contentFilters: @[DefaultContentTopic],
pubsubTopic: some(DefaultPubsubTopic),
)
let response = await restFilterTest.client.filterPostSubscriptions(requestBody)

# subscribe fiter service
let subscribedPeer = restFilterTest.serviceNode.wakuFilter.subscriptions.findSubscribedPeers(
DefaultPubsubTopic, DefaultContentTopic
)

check:
response.status == 200
$response.contentType == $MIMETYPE_JSON
response.data.requestId == "1001"
subscribedPeer.len() == 1

# ping subscriber node
restFilterTest.messageCache.pubsubSubscribe(DefaultPubsubTopic)

let pingResponse = await restFilterTest.client.filterSubscriberPing("1002")

check:
pingResponse.status == 200
pingResponse.data.requestId == "1002"
pingResponse.data.statusDesc == "OK"

# first - message push from service node to subscriber client
let testMessage = WakuMessage(
payload: "TEST-PAYLOAD-MUST-RECEIVE".toBytes(),
contentTopic: DefaultContentTopic,
timestamp: int64(2022),
meta: "test-meta".toBytes(),
)

let postMsgResponse1 = await restFilterTest.clientTwdServiceNode.relayPostMessagesV1(
DefaultPubsubTopic, toRelayWakuMessage(testMessage)
)

# check messages received client side or not
let messages1 = await restFilterTest.client.filterGetMessagesV1(DefaultContentTopic)

check:
postMsgResponse1.status == 200
$postMsgResponse1.contentType == $MIMETYPE_TEXT
postMsgResponse1.data == "OK"
len(messages1.data) == 1

# Pause execution for 2 minutes to test TimeCache functionality of service node
sleep(120000)

# second - message push from service node to subscriber client
let postMsgResponse2 = await restFilterTest.clientTwdServiceNode.relayPostMessagesV1(
DefaultPubsubTopic, toRelayWakuMessage(testMessage)
)

# check message received client side or not
let messages2 = await restFilterTest.client.filterGetMessagesV1(DefaultContentTopic)

check:
postMsgResponse2.status == 200
$postMsgResponse2.contentType == $MIMETYPE_TEXT
postMsgResponse2.data == "OK"
len(messages2.data) == 1
await restFilterTest.shutdown()

0 comments on commit 1dfd377

Please sign in to comment.