-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_async.py
More file actions
67 lines (51 loc) · 1.73 KB
/
test_async.py
File metadata and controls
67 lines (51 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import asyncio
import traceback
import pytest
import outcome
from outcome import AlreadyUsedError, Error, Value
pytestmark = pytest.mark.asyncio
async def test_acapture():
async def add(x, y):
await asyncio.sleep(0)
return x + y
v = await outcome.acapture(add, 3, y=4)
assert v == Value(7)
assert v.peek() == 7
async def raise_ValueError(x):
await asyncio.sleep(0)
raise ValueError(x)
e = await outcome.acapture(raise_ValueError, 9)
assert type(e.error) is ValueError
assert e.error.args == (9,)
with pytest.raises(ValueError) as exc_info:
e.peek()
with pytest.raises(ValueError) as exc_info2:
e.unwrap()
assert exc_info.value is not exc_info2.value
async def test_asend():
async def my_agen_func():
assert (yield 1) == "value"
with pytest.raises(KeyError):
yield 2
yield 3
my_agen = my_agen_func().__aiter__()
v = Value("value")
e = Error(KeyError())
assert (await my_agen.asend(None)) == 1
assert (await v.asend(my_agen)) == 2
with pytest.raises(AlreadyUsedError):
await v.asend(my_agen)
assert (await e.asend(my_agen)) == 3
with pytest.raises(AlreadyUsedError):
await e.asend(my_agen)
with pytest.raises(StopAsyncIteration):
await my_agen.asend(None)
async def test_traceback_frame_removal():
async def raise_ValueError(x):
raise ValueError(x)
e = await outcome.acapture(raise_ValueError, 'abc')
with pytest.raises(ValueError) as exc_info:
e.unwrap()
frames = traceback.extract_tb(exc_info.value.__traceback__)
functions = [function for _, _, function, _ in frames]
assert functions[-2:] == ['unwrap', 'raise_ValueError']