Skip to content

Commit 943a511

Browse files
committed
test for memory leaks in p2p service children
1 parent 18505f7 commit 943a511

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/p2p/test_service.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,60 @@ async def test_daemon_exit_causes_parent_cancellation():
3939
assert not service.is_running
4040

4141
await asyncio.wait_for(service.events.cleaned_up.wait(), timeout=1)
42+
43+
44+
@pytest.mark.asyncio
45+
async def test_service_tasks_do_not_leak_memory():
46+
service = WaitService()
47+
asyncio.ensure_future(service.run())
48+
49+
end = asyncio.Event()
50+
51+
async def run_until_end():
52+
await end.wait()
53+
54+
service.run_task(run_until_end())
55+
56+
# inspect internals to determine if memory is leaking
57+
58+
# confirm that task is tracked:
59+
assert len(service._tasks) == 1
60+
61+
end.set()
62+
# allow the coro to exit
63+
await asyncio.sleep(0)
64+
65+
# confirm that task is no longer tracked:
66+
assert len(service._tasks) == 0
67+
68+
# test cleanup
69+
await service.cancel()
70+
71+
72+
@pytest.mark.asyncio
73+
async def test_service_children_do_not_leak_memory():
74+
parent = WaitService()
75+
child = WaitService()
76+
asyncio.ensure_future(parent.run())
77+
78+
parent.run_child_service(child)
79+
80+
# inspect internals to determine if memory is leaking
81+
82+
# confirm that child service is tracked:
83+
assert len(parent._child_services) == 1
84+
85+
# give child a chance to start
86+
await asyncio.sleep(0)
87+
88+
# ... and then end it
89+
await child.cancel()
90+
91+
# remove the final reference to the child service
92+
del child
93+
94+
# confirm that child service is no longer tracked:
95+
assert len(parent._child_services) == 0
96+
97+
# test cleanup
98+
await parent.cancel()

0 commit comments

Comments
 (0)