Skip to content

Commit

Permalink
test(taskgroup): Add error handling example
Browse files Browse the repository at this point in the history
  • Loading branch information
achimnol committed Dec 14, 2020
1 parent b1a7b34 commit dd855f5
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/test_taskgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,38 @@ async def do_cancel():
assert t2.cancelled()
assert t3.done()
assert results == ['a', 'a']


@pytest.mark.asyncio
async def test_taskgroup_error():
with VirtualClock().patch_loop():

async def do_job(delay, result):
await asyncio.sleep(delay)
if result == 'x':
raise ZeroDivisionError('oops')
else:
return 99

with pytest.raises(TaskGroupError) as e:
async with TaskGroup() as tg:
t1 = tg.create_task(do_job(0.3, 'a'))
t2 = tg.create_task(do_job(0.5, 'x'))
t3 = tg.create_task(do_job(0.7, 'a'))

assert len(e.value.__errors__) == 1
assert type(e.value.__errors__[0]).__name__ == 'ZeroDivisionError'

assert t1.done()
assert await t1 == 99
assert t1.result() == 99
assert t1.exception() is None

assert t2.done()
with pytest.raises(ZeroDivisionError):
await t2
with pytest.raises(ZeroDivisionError):
t2.result()
assert type(t2.exception()).__name__ == 'ZeroDivisionError'

assert t3.cancelled()

0 comments on commit dd855f5

Please sign in to comment.