Skip to content

Commit 09e151a

Browse files
committed
Added test for peer-store cleanup task
1 parent 2d335d4 commit 09e151a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/core/peer/test_peerstore.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,30 @@ async def consume_addrs():
120120
nursery.cancel_scope.cancel()
121121

122122
assert collected == [addr1, addr2]
123+
124+
125+
@pytest.mark.trio
126+
async def test_cleanup_task_remove_expired_data():
127+
store = PeerStore()
128+
peer_id = ID(b"peer123")
129+
addr = Multiaddr("/ip4/127.0.0.1/tcp/4040")
130+
131+
# Insert addrs with short TTL (0.01s)
132+
store.add_addr(peer_id, addr, 1)
133+
134+
assert store.addrs(peer_id) == [addr]
135+
assert peer_id in store.peer_data_map
136+
137+
# Start cleanup task in a nursery
138+
async with trio.open_nursery() as nursery:
139+
# Run the cleanup task with a short interval so it runs soon
140+
nursery.start_soon(store.start_cleanup_task, 1)
141+
142+
# Sleep long enough for TTL to expire and cleanup to run
143+
await trio.sleep(3)
144+
145+
# Cancel the nursery to stop background tasks
146+
nursery.cancel_scope.cancel()
147+
148+
# Confirm the peer data is gone from the peer_data_map
149+
assert peer_id not in store.peer_data_map

0 commit comments

Comments
 (0)