2020-05-08 20:58:23 +00:00
|
|
|
{.used.}
|
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
import unittest, 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()
|
|
|
|
|
|
|
|
proc randomPeerID(): PeerID =
|
|
|
|
PeerID.init(PrivateKey.random(ECDSA, rng[]).get()).get()
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
suite "MCache":
|
|
|
|
test "put/get":
|
|
|
|
var mCache = newMCache(3, 5)
|
2020-07-07 11:14:11 +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":
|
|
|
|
var mCache = newMCache(3, 5)
|
|
|
|
|
|
|
|
for i in 0..<3:
|
2020-07-07 11:14:11 +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:
|
2020-07-07 11:14:11 +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":
|
|
|
|
var mCache = newMCache(1, 5)
|
|
|
|
|
|
|
|
for i in 0..<3:
|
2020-07-07 11:14:11 +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:
|
2020-07-07 11:14:11 +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:
|
2020-07-07 11:14:11 +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":
|
|
|
|
var mCache = newMCache(1, 5)
|
|
|
|
|
|
|
|
for i in 0..<3:
|
2020-07-07 11:14:11 +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:
|
2020-07-07 11:14:11 +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:
|
2020-07-07 11:14:11 +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
|