nim-libp2p/tests/pubsub/testmcache.nim
Dmitriy Ryajov 7b6e1c0688
Gossipsub interop (#189)
* interop fixes

* add custom messageid provider and fix seqno

* use ECDSA for speed

* adding messageid tests

* breakout from publish loop

* addressing review comments

* remove unneded var

* dont stop broadcasting on failed peers
2020-05-27 12:33:49 -06:00

96 lines
3.0 KiB
Nim

{.used.}
import options, sets, sequtils
import unittest
import ../../libp2p/[peer,
crypto/crypto,
protocols/pubsub/mcache,
protocols/pubsub/rpc/message,
protocols/pubsub/rpc/messages]
suite "MCache":
test "put/get":
var mCache = newMCache(3, 5)
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"))
mCache.put(msg)
check mCache.get(msg.msgId).isSome and mCache.get(msg.msgId).get() == msg
test "window":
var mCache = newMCache(3, 5)
for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"),
topicIDs: @["foo"])
mCache.put(msg)
for i in 0..<5:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"),
topicIDs: @["bar"])
mCache.put(msg)
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:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"),
topicIDs: @["foo"])
mCache.put(msg)
mCache.shift()
check mCache.window("foo").len == 0
for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"),
topicIDs: @["bar"])
mCache.put(msg)
mCache.shift()
check mCache.window("bar").len == 0
for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"),
topicIDs: @["baz"])
mCache.put(msg)
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:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"),
topicIDs: @["foo"])
mCache.put(msg)
for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"),
topicIDs: @["bar"])
mCache.put(msg)
for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"),
topicIDs: @["baz"])
mCache.put(msg)
mCache.shift()
check mCache.window("foo").len == 0
mCache.shift()
check mCache.window("bar").len == 0