Skip to content

Commit

Permalink
Provide stack for suspended coro in test timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
fjetter committed Oct 21, 2021
1 parent 3afc670 commit aa2384f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions distributed/tests/test_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,21 @@ async def ping_pong():
assert await write_queue.get() == (b.address, {"op": "ping", "reply": True})
write_event.set()
assert await fut == "pong"


def test_provide_stack_on_timeout():
sleep_time = 30

async def inner_test(c, s, a, b):
await asyncio.sleep(sleep_time)

test = gen_cluster(client=True, timeout=0.5)(inner_test)

start = time()
with pytest.raises(asyncio.TimeoutError) as exc:
test()
end = time()
assert "inner_test" in str(exc)
assert "await asyncio.sleep(sleep_time)" in str(exc)
# ensure the task was properly
assert end - start < sleep_time / 2
14 changes: 13 additions & 1 deletion distributed/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,10 +965,22 @@ async def coro():
args = [c] + args
try:
future = func(*args, *outer_args, **kwargs)
future = asyncio.wait_for(future, timeout)
task = asyncio.create_task(future)

future = asyncio.wait_for(asyncio.shield(task), timeout)
result = await future
if s.validate:
s.validate_state()
except asyncio.TimeoutError:
assert task
buffer = io.StringIO()
# This stack indicates where the coro/test is
# suspended
task.print_stack(file=buffer)
task.cancel()
while not task.cancelled():
await asyncio.sleep(0.01)
raise TimeoutError(f"Test timeout.\n{buffer.getvalue()}")
finally:
if client and c.status not in ("closing", "closed"):
await c._close(fast=s.status == Status.closed)
Expand Down

0 comments on commit aa2384f

Please sign in to comment.