2020-05-08 20:58:23 +00:00
|
|
|
{.used.}
|
|
|
|
|
2024-05-08 12:33:26 +00:00
|
|
|
import unittest2, sequtils
|
2020-06-03 02:21:11 +00:00
|
|
|
import stew/byteutils
|
2020-07-01 06:25:09 +00:00
|
|
|
import
|
|
|
|
../../libp2p/
|
2024-05-08 12:33:26 +00:00
|
|
|
[peerid, crypto/crypto, protocols/pubsub/mcache, protocols/pubsub/rpc/message]
|
2019-12-06 02:16:18 +00:00
|
|
|
|
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
|
|
|
|
2022-02-24 16:32:20 +00:00
|
|
|
const MsgIdGenSuccess = "msg id generation success"
|
2022-02-21 15:04:17 +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())
|
2022-02-24 16:32:20 +00:00
|
|
|
let msgId = defaultMsgIdProvider(msg).expect(MsgIdGenSuccess)
|
2020-06-28 15:56:38 +00:00
|
|
|
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:
|
2024-03-25 11:06:34 +00:00
|
|
|
var msg =
|
|
|
|
Message(fromPeer: randomPeerId(), seqno: "12345".toBytes(), topic: "foo")
|
2022-02-24 16:32:20 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenSuccess), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
for i in 0 ..< 5:
|
2024-03-25 11:06:34 +00:00
|
|
|
var msg =
|
|
|
|
Message(fromPeer: randomPeerId(), seqno: "12345".toBytes(), topic: "bar")
|
2022-02-24 16:32:20 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenSuccess), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var mids = mCache.window("foo")
|
|
|
|
check mids.len == 3
|
|
|
|
|
|
|
|
var id = toSeq(mids)[0]
|
2024-03-25 11:06:34 +00:00
|
|
|
check mCache.get(id).get().topic == "foo"
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
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:
|
2024-03-25 11:06:34 +00:00
|
|
|
var msg =
|
|
|
|
Message(fromPeer: randomPeerId(), seqno: "12345".toBytes(), topic: "foo")
|
2022-02-24 16:32:20 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenSuccess), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
mCache.shift()
|
|
|
|
check mCache.window("foo").len == 0
|
|
|
|
|
|
|
|
for i in 0 ..< 3:
|
2024-03-25 11:06:34 +00:00
|
|
|
var msg =
|
|
|
|
Message(fromPeer: randomPeerId(), seqno: "12345".toBytes(), topic: "bar")
|
2022-02-24 16:32:20 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenSuccess), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
mCache.shift()
|
|
|
|
check mCache.window("bar").len == 0
|
|
|
|
|
|
|
|
for i in 0 ..< 3:
|
2024-03-25 11:06:34 +00:00
|
|
|
var msg =
|
|
|
|
Message(fromPeer: randomPeerId(), seqno: "12345".toBytes(), topic: "baz")
|
2022-02-24 16:32:20 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenSuccess), 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:
|
2024-03-25 11:06:34 +00:00
|
|
|
var msg =
|
|
|
|
Message(fromPeer: randomPeerId(), seqno: "12345".toBytes(), topic: "foo")
|
2022-02-24 16:32:20 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenSuccess), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
for i in 0 ..< 3:
|
2024-03-25 11:06:34 +00:00
|
|
|
var msg =
|
|
|
|
Message(fromPeer: randomPeerId(), seqno: "12345".toBytes(), topic: "bar")
|
2022-02-24 16:32:20 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenSuccess), msg)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
for i in 0 ..< 3:
|
2024-03-25 11:06:34 +00:00
|
|
|
var msg =
|
|
|
|
Message(fromPeer: randomPeerId(), seqno: "12345".toBytes(), topic: "baz")
|
2022-02-24 16:32:20 +00:00
|
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenSuccess), 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
|