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
|
2023-04-18 13:22:10 +00:00
|
|
|
../../waku/v2/waku_message,
|
2022-10-21 13:01:39 +00:00
|
|
|
../../waku/v2/node/message_cache,
|
2023-02-13 10:43:49 +00:00
|
|
|
./testlib/common,
|
2023-04-05 14:01:51 +00:00
|
|
|
./testlib/wakucore
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
2022-11-09 14:00:11 +00:00
|
|
|
type TestMessageCache = MessageCache[(PubsubTopic, ContentTopic)]
|
2022-08-30 13:57:45 +00:00
|
|
|
|
|
|
|
suite "MessageCache":
|
2023-02-13 10:43:49 +00:00
|
|
|
test "subscribe to topic":
|
2022-06-10 11:30:51 +00:00
|
|
|
## Given
|
2022-11-09 14:00:11 +00:00
|
|
|
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic"))
|
2022-08-30 13:57:45 +00:00
|
|
|
let cache = TestMessageCache.init()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
## When
|
|
|
|
cache.subscribe(testTopic)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
cache.isSubscribed(testTopic)
|
|
|
|
|
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
test "unsubscribe from topic":
|
2022-06-10 11:30:51 +00:00
|
|
|
## Given
|
2022-11-09 14:00:11 +00:00
|
|
|
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic"))
|
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)
|
|
|
|
|
|
|
|
## When
|
|
|
|
cache.unsubscribe(testTopic)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
not cache.isSubscribed(testTopic)
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
test "get messages of a subscribed topic":
|
|
|
|
## Given
|
2022-11-09 14:00:11 +00:00
|
|
|
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic"))
|
2022-06-10 11:30:51 +00:00
|
|
|
let testMessage = fakeWakuMessage()
|
2023-02-13 10:43:49 +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-11-09 14:00:11 +00:00
|
|
|
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic"))
|
2022-06-10 11:30:51 +00:00
|
|
|
let testMessage = fakeWakuMessage()
|
2023-02-13 10:43:49 +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
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
test "get messages of a non-subscribed topic":
|
|
|
|
## Given
|
2022-11-09 14:00:11 +00:00
|
|
|
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic"))
|
2022-08-30 13:57:45 +00:00
|
|
|
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-11-09 14:00:11 +00:00
|
|
|
let testTopic = (PubsubTopic("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)
|
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
## When
|
2022-06-10 11:30:51 +00:00
|
|
|
cache.addMessage(testTopic, testMessage)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
let messages = cache.getMessages(testTopic).tryGet()
|
|
|
|
check:
|
|
|
|
messages == @[testMessage]
|
|
|
|
|
|
|
|
|
|
|
|
test "add messages to non-subscribed topic":
|
|
|
|
## Given
|
2022-11-09 14:00:11 +00:00
|
|
|
let testTopic = (PubsubTopic("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
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
## When
|
2022-06-10 11:30:51 +00:00
|
|
|
cache.addMessage(testTopic, testMessage)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
let res = cache.getMessages(testTopic)
|
|
|
|
check:
|
|
|
|
res.isErr()
|
|
|
|
res.error() == "Not subscribed to topic"
|
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2022-06-10 11:30:51 +00:00
|
|
|
test "add messages beyond the capacity":
|
|
|
|
## Given
|
2022-11-09 14:00:11 +00:00
|
|
|
let testTopic = (PubsubTopic("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)
|
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
## When
|
2022-06-10 11:30:51 +00:00
|
|
|
for msg in testMessages:
|
|
|
|
cache.addMessage(testTopic, msg)
|
|
|
|
|
|
|
|
## Then
|
2023-02-13 10:43:49 +00:00
|
|
|
let messages = cache.getMessages(testTopic).tryGet()
|
2022-06-10 11:30:51 +00:00
|
|
|
check:
|
|
|
|
messages == testMessages[1..2]
|