mirror of
https://github.com/status-im/nim-libp2p.git
synced 2025-01-11 21:44:24 +00:00
5819c6a9a7
* mcache fixes * remove timed cache - the window shifting already removes old messages * ref -> object * avoid unnecessary allocations with `[]` operator * simplify init * fix several gossipsub/floodsub issues * floodsub, gossipsub: don't rebroadcast messages that fail validation (!) * floodsub, gossipsub: don't crash when unsubscribing from unknown topics (!) * gossipsub: don't send message to peers that are not interested in the topic, when messages don't share topic list * floodsub: don't repeat all messages for each message when rebroadcasting * floodsub: allow sending empty data * floodsub: fix inefficient unsubscribe * sync floodsub/gossipsub logging * gossipsub: include incoming messages in mcache (!) * gossipsub: don't rebroadcast already-seen messages (!) * pubsubpeer: remove incoming/outgoing seen caches - these are already handled in gossipsub, floodsub and will cause trouble when peers try to resubscribe / regraft topics (because control messages will have same digest) * timedcache: reimplement without timers (fixes timer leaks and extreme inefficiency due to per-message closures, futures etc) * timedcache: ref -> obj
35 lines
644 B
Nim
35 lines
644 B
Nim
{.used.}
|
|
|
|
import std/unittest
|
|
import chronos/timer
|
|
import ../../libp2p/protocols/pubsub/timedcache
|
|
|
|
suite "TimedCache":
|
|
test "put/get":
|
|
var cache = TimedCache[int].init(5.seconds)
|
|
|
|
let now = Moment.now()
|
|
check:
|
|
not cache.put(1, now)
|
|
not cache.put(2, now + 3.seconds)
|
|
|
|
check:
|
|
1 in cache
|
|
2 in cache
|
|
|
|
check: not cache.put(3, now + 6.seconds) # expires 1
|
|
|
|
check:
|
|
1 notin cache
|
|
2 in cache
|
|
3 in cache
|
|
|
|
check:
|
|
cache.put(2, now + 7.seconds) # refreshes 2
|
|
not cache.put(4, now + 12.seconds) # expires 3
|
|
|
|
check:
|
|
2 in cache
|
|
3 notin cache
|
|
4 in cache
|