2022-06-10 11:30:51 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2022-08-30 13:57:45 +00:00
|
|
|
stew/[results, byteutils],
|
2022-06-10 11:30:51 +00:00
|
|
|
testutils/unittests,
|
2022-08-30 13:57:45 +00:00
|
|
|
chronicles
|
2022-06-10 11:30:51 +00:00
|
|
|
import
|
|
|
|
../../waku/v2/protocol/waku_message,
|
2022-08-30 13:57:45 +00:00
|
|
|
../../waku/v2/node/message_cache
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc fakeWakuMessage(payload = toBytes("TEST"), contentTopic = "test"): WakuMessage =
|
|
|
|
WakuMessage(
|
|
|
|
payload: payload,
|
|
|
|
contentTopic: contentTopic,
|
|
|
|
version: 1,
|
|
|
|
timestamp: 2022
|
|
|
|
)
|
|
|
|
|
2022-08-30 13:57:45 +00:00
|
|
|
type PubsubTopicString = string
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2022-08-30 13:57:45 +00:00
|
|
|
type TestMessageCache = MessageCache[(PubsubTopicString, ContentTopic)]
|
|
|
|
|
|
|
|
|
|
|
|
suite "MessageCache":
|
2022-06-10 11:30:51 +00:00
|
|
|
test "subscribe to topic":
|
|
|
|
## Given
|
2022-08-30 13:57:45 +00:00
|
|
|
let testTopic = ("test-pubsub-topic", ContentTopic("test-content-topic"))
|
|
|
|
let cache = TestMessageCache.init()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
## When
|
|
|
|
cache.subscribe(testTopic)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
cache.isSubscribed(testTopic)
|
|
|
|
|
|
|
|
|
|
|
|
test "unsubscribe from topic":
|
|
|
|
## Given
|
2022-08-30 13:57:45 +00:00
|
|
|
let testTopic = ("test-pubsub-topic", ContentTopic("test-content-topic"))
|
|
|
|
let cache = TestMessageCache.init()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# Init cache content
|
|
|
|
cache.subscribe(testTopic)
|
|
|
|
|
|
|
|
## When
|
|
|
|
cache.unsubscribe(testTopic)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
not cache.isSubscribed(testTopic)
|
|
|
|
|
|
|
|
|
|
|
|
test "get messages of a subscribed topic":
|
|
|
|
## Given
|
2022-08-30 13:57:45 +00:00
|
|
|
let testTopic = ("test-pubsub-topic", ContentTopic("test-content-topic"))
|
2022-06-10 11:30:51 +00:00
|
|
|
let testMessage = fakeWakuMessage()
|
2022-08-30 13:57:45 +00:00
|
|
|
let cache = TestMessageCache.init()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# Init cache content
|
|
|
|
cache.subscribe(testTopic)
|
|
|
|
cache.addMessage(testTopic, testMessage)
|
|
|
|
|
|
|
|
## When
|
|
|
|
let res = cache.getMessages(testTopic)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
res.isOk()
|
|
|
|
res.get() == @[testMessage]
|
|
|
|
|
|
|
|
|
|
|
|
test "get messages with clean flag shoud clear the messages cache":
|
|
|
|
## Given
|
2022-08-30 13:57:45 +00:00
|
|
|
let testTopic = ("test-pubsub-topic", ContentTopic("test-content-topic"))
|
2022-06-10 11:30:51 +00:00
|
|
|
let testMessage = fakeWakuMessage()
|
2022-08-30 13:57:45 +00:00
|
|
|
let cache = TestMessageCache.init()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# Init cache content
|
|
|
|
cache.subscribe(testTopic)
|
|
|
|
cache.addMessage(testTopic, testMessage)
|
|
|
|
|
|
|
|
## When
|
|
|
|
var res = cache.getMessages(testTopic, clear=true)
|
|
|
|
require(res.isOk())
|
|
|
|
|
|
|
|
res = cache.getMessages(testTopic)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
res.isOk()
|
|
|
|
res.get().len == 0
|
|
|
|
|
|
|
|
|
|
|
|
test "get messages of a non-subscribed topic":
|
|
|
|
## Given
|
2022-08-30 13:57:45 +00:00
|
|
|
let testTopic = ("test-pubsub-topic", ContentTopic("test-content-topic"))
|
|
|
|
let cache = TestMessageCache.init()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
## When
|
|
|
|
let res = cache.getMessages(testTopic)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
res.isErr()
|
|
|
|
res.error() == "Not subscribed to topic"
|
|
|
|
|
|
|
|
|
|
|
|
test "add messages to subscribed topic":
|
|
|
|
## Given
|
2022-08-30 13:57:45 +00:00
|
|
|
let testTopic = ("test-pubsub-topic", ContentTopic("test-content-topic"))
|
2022-06-10 11:30:51 +00:00
|
|
|
let testMessage = fakeWakuMessage()
|
2022-08-30 13:57:45 +00:00
|
|
|
let cache = TestMessageCache.init()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
cache.subscribe(testTopic)
|
|
|
|
|
|
|
|
## When
|
|
|
|
cache.addMessage(testTopic, testMessage)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
let messages = cache.getMessages(testTopic).tryGet()
|
|
|
|
check:
|
|
|
|
messages == @[testMessage]
|
|
|
|
|
|
|
|
|
|
|
|
test "add messages to non-subscribed topic":
|
|
|
|
## Given
|
2022-08-30 13:57:45 +00:00
|
|
|
let testTopic = ("test-pubsub-topic", ContentTopic("test-content-topic"))
|
2022-06-10 11:30:51 +00:00
|
|
|
let testMessage = fakeWakuMessage()
|
2022-08-30 13:57:45 +00:00
|
|
|
let cache = TestMessageCache.init()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
## When
|
|
|
|
cache.addMessage(testTopic, testMessage)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
let res = cache.getMessages(testTopic)
|
|
|
|
check:
|
|
|
|
res.isErr()
|
|
|
|
res.error() == "Not subscribed to topic"
|
|
|
|
|
|
|
|
|
|
|
|
test "add messages beyond the capacity":
|
|
|
|
## Given
|
2022-08-30 13:57:45 +00:00
|
|
|
let testTopic = ("test-pubsub-topic", ContentTopic("test-content-topic"))
|
2022-06-10 11:30:51 +00:00
|
|
|
let testMessages = @[
|
|
|
|
fakeWakuMessage(toBytes("MSG-1")),
|
|
|
|
fakeWakuMessage(toBytes("MSG-2")),
|
|
|
|
fakeWakuMessage(toBytes("MSG-3"))
|
|
|
|
]
|
|
|
|
|
2022-08-30 13:57:45 +00:00
|
|
|
let cache = TestMessageCache.init(capacity = 2)
|
2022-06-10 11:30:51 +00:00
|
|
|
cache.subscribe(testTopic)
|
|
|
|
|
|
|
|
## When
|
|
|
|
for msg in testMessages:
|
|
|
|
cache.addMessage(testTopic, msg)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
let messages = cache.getMessages(testTopic).tryGet()
|
|
|
|
check:
|
|
|
|
messages == testMessages[1..2]
|