Skip to content

Commit 9728021

Browse files
committed
Add more async tests
1 parent 0b8bb4e commit 9728021

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

tests/suite/test_async.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ async def foo():
9494
coro.throw(ValueError('boom'))
9595
self.assertEqual(str(cm.exception), 'boom')
9696

97+
def test_lazy_evaluation(self):
98+
"""The body of an async def must not run until the coroutine is first driven."""
99+
log = []
100+
101+
async def noop():
102+
return
103+
104+
async def lazy():
105+
log.append('before')
106+
await noop()
107+
log.append('after')
108+
return 'done'
109+
110+
coro = lazy()
111+
self.assertEqual(log, []) # constructing the coroutine runs nothing
112+
self.assertEqual(run_coro(coro), 'done')
113+
self.assertEqual(log, ['before', 'after'])
114+
97115

98116
class AwaitTest(unittest.TestCase):
99117
"""Tests for await expression."""
@@ -139,6 +157,20 @@ async def test():
139157

140158
self.assertEqual(run_coro(test()), 'done')
141159

160+
def test_exception_across_await(self):
161+
"""An exception raised in an awaited coroutine propagates and is catchable."""
162+
async def fails():
163+
raise ValueError('boom')
164+
165+
async def test():
166+
try:
167+
await fails()
168+
except ValueError as e:
169+
return 'caught: ' + str(e)
170+
return 'not caught'
171+
172+
self.assertEqual(run_coro(test()), 'caught: boom')
173+
142174

143175
class AsyncWithTest(unittest.TestCase):
144176
"""Tests for async with statement."""

0 commit comments

Comments
 (0)