-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtest_async.py
114 lines (92 loc) · 2.61 KB
/
test_async.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from pyper import task
import pytest
class TestError(Exception): ...
def f1(data):
return data
def f2(data):
yield data
def f3(data):
raise TestError
def f4(data):
return [data]
async def af1(data):
return data
async def af2(data):
yield data
async def af3(data):
raise TestError
async def af4(data):
async for row in data:
yield row
async def consumer(data):
total = 0
async for i in data:
total += i
return total
@pytest.mark.asyncio
async def test_aiterable_branched_pipeline():
p = task(af1) | task(f2, branch=True)
assert await p(1).__anext__() == 1
@pytest.mark.asyncio
async def test_iterable_branched_pipeline():
p = task(af1) | task(f4, branch=True)
assert await p(1).__anext__() == 1
@pytest.mark.asyncio
async def test_joined_pipeline():
p = task(af1) | task(af2, branch=True) | task(af4, branch=True, join=True)
assert await p(1).__anext__() == 1
@pytest.mark.asyncio
async def test_consumer():
p = task(af1) | task(af2, branch=True) > consumer
assert await p(1) == 1
@pytest.mark.asyncio
async def test_invalid_first_stage_workers():
try:
p = task(af1, workers=2) | task(af2, branch=True) > consumer
await p(1)
except Exception as e:
assert isinstance(e, RuntimeError)
else:
raise AssertionError
@pytest.mark.asyncio
async def test_invalid_first_stage_join():
try:
p = task(af1, join=True) | task(af2, branch=True) > consumer
await p(1)
except Exception as e:
assert isinstance(e, RuntimeError)
else:
raise AssertionError
@pytest.mark.asyncio
async def test_invalid_branch_result():
try:
p = task(af1, branch=True) > consumer
await p(1)
except Exception as e:
assert isinstance(e, TypeError)
else:
raise AssertionError
async def _try_catch_error(pipeline):
try:
p = task(af1) | pipeline > consumer
await p(1)
except Exception as e:
return isinstance(e, TestError)
else:
return False
@pytest.mark.asyncio
async def test_async_error_handling():
p = task(af3)
assert await _try_catch_error(p)
@pytest.mark.asyncio
async def test_threaded_error_handling():
p = task(f3, workers=2)
assert await _try_catch_error(p)
@pytest.mark.asyncio
async def test_multiprocessed_error_handling():
p = task(f3, workers=2, multiprocess=True)
assert await _try_catch_error(p)
@pytest.mark.asyncio
async def test_unified_pipeline():
p = task(af1) | task(f1) | task(f2, branch=True, multiprocess=True) > consumer
assert await p(1) == 1