-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
527 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,153 +1,198 @@ | ||
{.used.} | ||
|
||
import | ||
std/sets, | ||
stew/[results, byteutils], | ||
testutils/unittests, | ||
chronicles | ||
testutils/unittests | ||
import | ||
../../waku/waku_core, | ||
../../waku/waku_api/message_cache, | ||
./testlib/common, | ||
./testlib/wakucore | ||
|
||
|
||
type TestMessageCache = MessageCache[(PubsubTopic, ContentTopic)] | ||
|
||
suite "MessageCache": | ||
test "subscribe to topic": | ||
setup: | ||
## Given | ||
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic")) | ||
let cache = TestMessageCache.init() | ||
let testPubsubTopic = DefaultPubsubTopic | ||
let testContentTopic = DefaultContentTopic | ||
let cache = MessageCache.init(3) | ||
|
||
test "subscribe to topic": | ||
## When | ||
cache.subscribe(testTopic) | ||
cache.pubsubSubscribe(testPubsubTopic) | ||
cache.contentSubscribe(testContentTopic) | ||
|
||
## Then | ||
check: | ||
cache.isSubscribed(testTopic) | ||
|
||
cache.isPubsubSubscribed(testPubsubTopic) | ||
cache.isContentSubscribed(testContentTopic) | ||
|
||
test "unsubscribe from topic": | ||
## Given | ||
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic")) | ||
let cache = TestMessageCache.init() | ||
|
||
# Init cache content | ||
cache.subscribe(testTopic) | ||
cache.pubsubSubscribe(testPubsubTopic) | ||
cache.contentSubscribe(testContentTopic) | ||
|
||
## When | ||
cache.unsubscribe(testTopic) | ||
cache.pubsubUnsubscribe(testPubsubTopic) | ||
cache.contentUnsubscribe(testContentTopic) | ||
|
||
## Then | ||
check: | ||
not cache.isSubscribed(testTopic) | ||
|
||
not cache.isPubsubSubscribed(testPubsubTopic) | ||
not cache.isContentSubscribed(testContentTopic) | ||
|
||
test "get messages of a subscribed topic": | ||
## Given | ||
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic")) | ||
let testMessage = fakeWakuMessage() | ||
let cache = TestMessageCache.init() | ||
|
||
# Init cache content | ||
cache.subscribe(testTopic) | ||
cache.addMessage(testTopic, testMessage) | ||
cache.pubsubSubscribe(testPubsubTopic) | ||
cache.addMessage(testPubsubTopic, testMessage) | ||
|
||
## When | ||
let res = cache.getMessages(testTopic) | ||
let res = cache.getMessages(testPubsubTopic) | ||
|
||
## Then | ||
check: | ||
res.isOk() | ||
res.get() == @[testMessage] | ||
|
||
|
||
test "get messages with clean flag shoud clear the messages cache": | ||
## Given | ||
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic")) | ||
let testMessage = fakeWakuMessage() | ||
let cache = TestMessageCache.init() | ||
|
||
# Init cache content | ||
cache.subscribe(testTopic) | ||
cache.addMessage(testTopic, testMessage) | ||
cache.pubsubSubscribe(testPubsubTopic) | ||
cache.addMessage(testPubsubTopic, testMessage) | ||
|
||
## When | ||
var res = cache.getMessages(testTopic, clear=true) | ||
var res = cache.getMessages(testPubsubTopic, clear=true) | ||
require(res.isOk()) | ||
|
||
res = cache.getMessages(testTopic) | ||
res = cache.getMessages(testPubsubTopic) | ||
|
||
## Then | ||
check: | ||
res.isOk() | ||
res.get().len == 0 | ||
|
||
|
||
test "get messages of a non-subscribed topic": | ||
## Given | ||
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic")) | ||
let cache = TestMessageCache.init() | ||
|
||
## When | ||
let res = cache.getMessages(testTopic) | ||
cache.pubsubSubscribe(PubsubTopic("dummyPubsub")) | ||
let res = cache.getMessages(testPubsubTopic) | ||
|
||
## Then | ||
check: | ||
res.isErr() | ||
res.error() == "Not subscribed to topic" | ||
|
||
res.error() == "not subscribed to this pubsub topic" | ||
|
||
test "add messages to subscribed topic": | ||
## Given | ||
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic")) | ||
let testMessage = fakeWakuMessage() | ||
let cache = TestMessageCache.init() | ||
|
||
cache.subscribe(testTopic) | ||
cache.pubsubSubscribe(testPubsubTopic) | ||
|
||
## When | ||
cache.addMessage(testTopic, testMessage) | ||
cache.addMessage(testPubsubTopic, testMessage) | ||
|
||
## Then | ||
let messages = cache.getMessages(testTopic).tryGet() | ||
let messages = cache.getMessages(testPubsubTopic).tryGet() | ||
check: | ||
messages == @[testMessage] | ||
|
||
|
||
test "add messages to non-subscribed topic": | ||
## Given | ||
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic")) | ||
let testMessage = fakeWakuMessage() | ||
let cache = TestMessageCache.init() | ||
|
||
## When | ||
cache.addMessage(testTopic, testMessage) | ||
cache.addMessage(testPubsubTopic, testMessage) | ||
|
||
## Then | ||
let res = cache.getMessages(testTopic) | ||
let res = cache.getMessages(testPubsubTopic) | ||
check: | ||
res.isErr() | ||
res.error() == "Not subscribed to topic" | ||
|
||
res.error() == "not subscribed to any pubsub topics" | ||
|
||
test "add messages beyond the capacity": | ||
## Given | ||
let testTopic = (PubsubTopic("test-pubsub-topic"), ContentTopic("test-content-topic")) | ||
let testMessages = @[ | ||
let testSet = toHashSet([ | ||
fakeWakuMessage(toBytes("MSG-1")), | ||
fakeWakuMessage(toBytes("MSG-2")), | ||
fakeWakuMessage(toBytes("MSG-3")) | ||
] | ||
fakeWakuMessage(toBytes("MSG-3")), | ||
fakeWakuMessage(toBytes("MSG-4")), | ||
]) | ||
|
||
let cache = TestMessageCache.init(capacity = 2) | ||
cache.subscribe(testTopic) | ||
cache.pubsubSubscribe(testPubsubTopic) | ||
|
||
## When | ||
for msg in testMessages: | ||
cache.addMessage(testTopic, msg) | ||
for msg in testSet: | ||
cache.addMessage(testPubsubTopic, msg) | ||
|
||
## Then | ||
let messages = cache.getMessages(testTopic).tryGet() | ||
let messages = cache.getMessages(testPubsubTopic).tryGet() | ||
let messageSet = toHashSet(messages) | ||
|
||
check: | ||
messageSet.len == 3 | ||
messageSet < testSet | ||
|
||
test "get messages on pubsub via content topics": | ||
cache.pubsubSubscribe(testPubsubTopic) | ||
|
||
let fakeMessage = fakeWakuMessage() | ||
|
||
cache.addMessage(testPubsubTopic, fakeMessage) | ||
|
||
let getRes = cache.getAutoMessages(DefaultContentTopic) | ||
|
||
check: | ||
getRes.isOk | ||
getRes.get() == @[fakeMessage] | ||
|
||
test "add same message twice": | ||
cache.pubsubSubscribe(testPubsubTopic) | ||
|
||
let fakeMessage = fakeWakuMessage() | ||
|
||
cache.addMessage(testPubsubTopic, fakeMessage) | ||
cache.addMessage(testPubsubTopic, fakeMessage) | ||
|
||
check: | ||
cache.messagesCount() == 1 | ||
|
||
test "unsubscribing remove messages": | ||
let topic0 = "PubsubTopic0" | ||
let topic1 = "PubsubTopic1" | ||
let topic2 = "PubsubTopic2" | ||
|
||
let fakeMessage0 = fakeWakuMessage(toBytes("MSG-0")) | ||
let fakeMessage1 = fakeWakuMessage(toBytes("MSG-1")) | ||
let fakeMessage2 = fakeWakuMessage(toBytes("MSG-2")) | ||
|
||
cache.pubsubSubscribe(topic0) | ||
cache.pubsubSubscribe(topic1) | ||
cache.pubsubSubscribe(topic2) | ||
cache.contentSubscribe("ContentTopic0") | ||
|
||
cache.addMessage(topic0, fakeMessage0) | ||
cache.addMessage(topic1, fakeMessage1) | ||
cache.addMessage(topic2, fakeMessage2) | ||
|
||
cache.pubsubUnsubscribe(topic1) | ||
|
||
# at this point, fakeMessage1 is only ref by DefaultContentTopic | ||
|
||
let res = cache.getAutoMessages(DefaultContentTopic) | ||
|
||
check: | ||
res.isOk() | ||
res.get().len == 3 | ||
cache.isPubsubSubscribed(topic0) == true | ||
cache.isPubsubSubscribed(topic1) == false | ||
cache.isPubsubSubscribed(topic2) == true | ||
|
||
cache.contentUnsubscribe(DefaultContentTopic) | ||
|
||
# msg1 was delete because no refs | ||
|
||
check: | ||
messages == testMessages[1..2] | ||
cache.messagesCount() == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.