Skip to content

Commit

Permalink
Fix a test that was leaving unattended tasks after the test
Browse files Browse the repository at this point in the history
The request was cancelled fast enough, and the response writer was closed. But the response handler continued running and writing into that closed transport — long enough to be noticed by the cleanup fixture.

Signed-off-by: Sergey Vasilyev <[email protected]>
  • Loading branch information
nolar committed Oct 2, 2022
1 parent b7f8f0b commit 7c96127
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions tests/apis/test_api_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,17 @@ async def serve_slowly():
async def test_stopper_in_streams(
resp_mocker, aresponses, hostname, method, delay, expected, settings, logger):

async def stream_slowly(request: aiohttp.ClientRequest):
async def stream_slowly(request: aiohttp.web.Request) -> aiohttp.web.StreamResponse:
response = aiohttp.web.StreamResponse()
await response.prepare(request)
await asyncio.sleep(0.05)
await response.write(b'{"fake": "result1"}\n')
await asyncio.sleep(0.15)
await response.write(b'{"fake": "result2"}\n')
await response.write_eof()
try:
await asyncio.sleep(0.05)
await response.write(b'{"fake": "result1"}\n')
await asyncio.sleep(0.15)
await response.write(b'{"fake": "result2"}\n')
await response.write_eof()
except ConnectionError:
pass
return response

aresponses.add(hostname, '/url', method, stream_slowly)
Expand All @@ -256,3 +259,5 @@ async def stream_slowly(request: aiohttp.ClientRequest):
items.append(item)

assert items == expected

await asyncio.sleep(0.2) # give the response some time to be cancelled and its tasks closed

0 comments on commit 7c96127

Please sign in to comment.