2020-05-08 20:58:23 +00:00
|
|
|
{.used.}
|
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
import unittest2, options, sets, sequtils, bearssl
|
2020-06-03 02:21:11 +00:00
|
|
|
import stew/byteutils
|
2020-07-01 06:25:09 +00:00
|
|
|
import ../../libp2p/[peerid,
|
2019-12-06 02:16:18 +00:00
|
|
|
crypto/crypto,
|
|
|
|
protocols/pubsub/mcache,
|
|
|
|
protocols/pubsub/rpc/message,
|
|
|
|
protocols/pubsub/rpc/messages]
|
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
var rng = newRng()
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc randomPeerId(): PeerId =
|
|
|
|
PeerId.init(PrivateKey.random(ECDSA, rng[]).get()).get()
|
2020-07-07 11:14:11 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
suite "MCache":
|
|
|
|
test "put/get":
|
2020-09-04 06:10:32 +00:00
|
|
|
var mCache = MCache.init(3, 5)
|
2021-12-16 10:05:20 +00:00
|
|
|
var msg = Message(fromPeer: randomPeerId(), seqno: "12345".toBytes())
|
2020-06-28 15:56:38 +00:00
|
|
|
let msgId = defaultMsgIdProvider(msg)
|
|
|
|
mCache.put(msgId, msg)
|
|
|
|
check mCache.get(msgId).isSome and mCache.get(msgId).get() == msg
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
test "window":
|
2020-09-04 06:10:32 +00:00
|
|
|
var mCache = MCache.init(3, 5)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2021-12-16 10:05:20 +00:00
|
|
|
var msg = Message(fromPeer: randomPeerId(),
|
2020-06-03 02:21:11 +00:00
|
|
|
seqno: "12345".toBytes(),
|
2019-12-06 02:16:18 +00:00
|
|
|
topicIDs: @["foo"])
|
2020-06-28 15:56:38 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
for i in 0..<5:
|
2021-12-16 10:05:20 +00:00
|
|
|
var msg = Message(fromPeer: randomPeerId(),
|
2020-06-03 02:21:11 +00:00
|
|
|
seqno: "12345".toBytes(),
|
2019-12-06 02:16:18 +00:00
|
|
|
topicIDs: @["bar"])
|
2020-06-28 15:56:38 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var mids = mCache.window("foo")
|
|
|
|
check mids.len == 3
|
|
|
|
|
|
|
|
var id = toSeq(mids)[0]
|
|
|
|
check mCache.get(id).get().topicIDs[0] == "foo"
|
|
|
|
|
|
|
|
test "shift - shift 1 window at a time":
|
2020-09-04 06:10:32 +00:00
|
|
|
var mCache = MCache.init(1, 5)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2021-12-16 10:05:20 +00:00
|
|
|
var msg = Message(fromPeer: randomPeerId(),
|
2020-06-03 02:21:11 +00:00
|
|
|
seqno: "12345".toBytes(),
|
2019-12-06 02:16:18 +00:00
|
|
|
topicIDs: @["foo"])
|
2020-06-28 15:56:38 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
mCache.shift()
|
|
|
|
check mCache.window("foo").len == 0
|
|
|
|
|
|
|
|
for i in 0..<3:
|
2021-12-16 10:05:20 +00:00
|
|
|
var msg = Message(fromPeer: randomPeerId(),
|
2020-06-03 02:21:11 +00:00
|
|
|
seqno: "12345".toBytes(),
|
2019-12-06 02:16:18 +00:00
|
|
|
topicIDs: @["bar"])
|
2020-06-28 15:56:38 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
mCache.shift()
|
|
|
|
check mCache.window("bar").len == 0
|
|
|
|
|
|
|
|
for i in 0..<3:
|
2021-12-16 10:05:20 +00:00
|
|
|
var msg = Message(fromPeer: randomPeerId(),
|
2020-06-03 02:21:11 +00:00
|
|
|
seqno: "12345".toBytes(),
|
2019-12-06 02:16:18 +00:00
|
|
|
topicIDs: @["baz"])
|
2020-06-28 15:56:38 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
mCache.shift()
|
|
|
|
check mCache.window("baz").len == 0
|
|
|
|
|
|
|
|
test "shift - 2 windows at a time":
|
2020-09-04 06:10:32 +00:00
|
|
|
var mCache = MCache.init(1, 5)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2021-12-16 10:05:20 +00:00
|
|
|
var msg = Message(fromPeer: randomPeerId(),
|
2020-06-03 02:21:11 +00:00
|
|
|
seqno: "12345".toBytes(),
|
2019-12-06 02:16:18 +00:00
|
|
|
topicIDs: @["foo"])
|
2020-06-28 15:56:38 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2021-12-16 10:05:20 +00:00
|
|
|
var msg = Message(fromPeer: randomPeerId(),
|
2020-06-03 02:21:11 +00:00
|
|
|
seqno: "12345".toBytes(),
|
2019-12-06 02:16:18 +00:00
|
|
|
topicIDs: @["bar"])
|
2020-06-28 15:56:38 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2021-12-16 10:05:20 +00:00
|
|
|
var msg = Message(fromPeer: randomPeerId(),
|
2020-06-03 02:21:11 +00:00
|
|
|
seqno: "12345".toBytes(),
|
2019-12-06 02:16:18 +00:00
|
|
|
topicIDs: @["baz"])
|
2020-06-28 15:56:38 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
mCache.shift()
|
|
|
|
check mCache.window("foo").len == 0
|
|
|
|
|
|
|
|
mCache.shift()
|
|
|
|
check mCache.window("bar").len == 0
|