mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-11 15:40:54 +00:00
Added test for peer-store cleanup task
This commit is contained in:
@ -120,3 +120,30 @@ async def test_addr_stream_yields_new_addrs():
|
|||||||
nursery.cancel_scope.cancel()
|
nursery.cancel_scope.cancel()
|
||||||
|
|
||||||
assert collected == [addr1, addr2]
|
assert collected == [addr1, addr2]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.trio
|
||||||
|
async def test_cleanup_task_remove_expired_data():
|
||||||
|
store = PeerStore()
|
||||||
|
peer_id = ID(b"peer123")
|
||||||
|
addr = Multiaddr("/ip4/127.0.0.1/tcp/4040")
|
||||||
|
|
||||||
|
# Insert addrs with short TTL (0.01s)
|
||||||
|
store.add_addr(peer_id, addr, 1)
|
||||||
|
|
||||||
|
assert store.addrs(peer_id) == [addr]
|
||||||
|
assert peer_id in store.peer_data_map
|
||||||
|
|
||||||
|
# Start cleanup task in a nursery
|
||||||
|
async with trio.open_nursery() as nursery:
|
||||||
|
# Run the cleanup task with a short interval so it runs soon
|
||||||
|
nursery.start_soon(store.start_cleanup_task, 1)
|
||||||
|
|
||||||
|
# Sleep long enough for TTL to expire and cleanup to run
|
||||||
|
await trio.sleep(3)
|
||||||
|
|
||||||
|
# Cancel the nursery to stop background tasks
|
||||||
|
nursery.cancel_scope.cancel()
|
||||||
|
|
||||||
|
# Confirm the peer data is gone from the peer_data_map
|
||||||
|
assert peer_id not in store.peer_data_map
|
||||||
|
|||||||
Reference in New Issue
Block a user