Added test for peer-store cleanup task

This commit is contained in:
lla-dane
2025-07-19 22:57:00 +05:30
parent 2d335d4394
commit 09e151aafc

View File

@ -120,3 +120,30 @@ async def test_addr_stream_yields_new_addrs():
nursery.cancel_scope.cancel()
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