2019-05-07 11:44:13 +08:00
|
|
|
from libp2p.pubsub.mcache import MessageCache
|
|
|
|
|
|
|
|
|
|
|
|
class Msg:
|
2019-08-08 14:22:06 +08:00
|
|
|
__slots__ = ["topicIDs", "seqno", "from_id"]
|
|
|
|
|
2019-05-07 11:44:13 +08:00
|
|
|
def __init__(self, topicIDs, seqno, from_id):
|
|
|
|
self.topicIDs = topicIDs
|
2019-07-29 12:09:35 +08:00
|
|
|
self.seqno = seqno
|
2019-05-07 11:44:13 +08:00
|
|
|
self.from_id = from_id
|
|
|
|
|
2019-07-29 12:09:35 +08:00
|
|
|
|
2019-12-06 17:06:37 +08:00
|
|
|
def test_mcache():
|
2019-05-07 11:44:13 +08:00
|
|
|
# Ported from:
|
2019-07-28 18:07:48 +08:00
|
|
|
# https://github.com/libp2p/go-libp2p-pubsub/blob/51b7501433411b5096cac2b4994a36a68515fc03/mcache_test.go
|
2019-05-07 11:44:13 +08:00
|
|
|
mcache = MessageCache(3, 5)
|
|
|
|
msgs = []
|
|
|
|
|
|
|
|
for i in range(60):
|
|
|
|
msgs.append(Msg(["test"], i, "test"))
|
|
|
|
|
|
|
|
for i in range(10):
|
|
|
|
mcache.put(msgs[i])
|
|
|
|
|
|
|
|
for i in range(10):
|
|
|
|
msg = msgs[i]
|
|
|
|
mid = (msg.seqno, msg.from_id)
|
|
|
|
get_msg = mcache.get(mid)
|
|
|
|
|
|
|
|
# successful read
|
|
|
|
assert get_msg == msg
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
gids = mcache.window("test")
|
2019-05-07 11:44:13 +08:00
|
|
|
|
|
|
|
assert len(gids) == 10
|
|
|
|
|
|
|
|
for i in range(10):
|
|
|
|
msg = msgs[i]
|
|
|
|
mid = (msg.seqno, msg.from_id)
|
|
|
|
|
|
|
|
assert mid == gids[i]
|
|
|
|
|
|
|
|
mcache.shift()
|
|
|
|
|
|
|
|
for i in range(10, 20):
|
|
|
|
mcache.put(msgs[i])
|
|
|
|
|
|
|
|
for i in range(20):
|
|
|
|
msg = msgs[i]
|
|
|
|
mid = (msg.seqno, msg.from_id)
|
|
|
|
get_msg = mcache.get(mid)
|
|
|
|
|
|
|
|
assert get_msg == msg
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
gids = mcache.window("test")
|
2019-05-07 11:44:13 +08:00
|
|
|
|
|
|
|
assert len(gids) == 20
|
|
|
|
|
|
|
|
for i in range(10):
|
|
|
|
msg = msgs[i]
|
|
|
|
mid = (msg.seqno, msg.from_id)
|
|
|
|
|
|
|
|
assert mid == gids[10 + i]
|
|
|
|
|
|
|
|
for i in range(10, 20):
|
|
|
|
msg = msgs[i]
|
|
|
|
mid = (msg.seqno, msg.from_id)
|
|
|
|
|
|
|
|
assert mid == gids[i - 10]
|
|
|
|
|
|
|
|
mcache.shift()
|
|
|
|
|
|
|
|
for i in range(20, 30):
|
|
|
|
mcache.put(msgs[i])
|
|
|
|
|
|
|
|
mcache.shift()
|
|
|
|
|
|
|
|
for i in range(30, 40):
|
|
|
|
mcache.put(msgs[i])
|
|
|
|
|
|
|
|
mcache.shift()
|
|
|
|
|
|
|
|
for i in range(40, 50):
|
|
|
|
mcache.put(msgs[i])
|
|
|
|
|
|
|
|
mcache.shift()
|
|
|
|
|
|
|
|
for i in range(50, 60):
|
|
|
|
mcache.put(msgs[i])
|
|
|
|
|
|
|
|
assert len(mcache.msgs) == 50
|
|
|
|
|
|
|
|
for i in range(10):
|
|
|
|
msg = msgs[i]
|
|
|
|
mid = (msg.seqno, msg.from_id)
|
|
|
|
get_msg = mcache.get(mid)
|
|
|
|
|
|
|
|
# Should be evicted from cache
|
|
|
|
assert not get_msg
|
|
|
|
|
|
|
|
for i in range(10, 60):
|
|
|
|
msg = msgs[i]
|
|
|
|
mid = (msg.seqno, msg.from_id)
|
|
|
|
get_msg = mcache.get(mid)
|
|
|
|
|
|
|
|
assert get_msg == msg
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
gids = mcache.window("test")
|
2019-05-07 11:44:13 +08:00
|
|
|
|
|
|
|
assert len(gids) == 30
|
|
|
|
|
|
|
|
for i in range(10):
|
|
|
|
msg = msgs[50 + i]
|
|
|
|
mid = (msg.seqno, msg.from_id)
|
|
|
|
|
|
|
|
assert mid == gids[i]
|
|
|
|
|
|
|
|
for i in range(10, 20):
|
|
|
|
msg = msgs[30 + i]
|
|
|
|
mid = (msg.seqno, msg.from_id)
|
|
|
|
|
|
|
|
assert mid == gids[i]
|
|
|
|
|
|
|
|
for i in range(20, 30):
|
|
|
|
msg = msgs[10 + i]
|
|
|
|
mid = (msg.seqno, msg.from_id)
|
|
|
|
|
|
|
|
assert mid == gids[i]
|