11from pyper import task
22import pytest
33
4+ class TestError (Exception ): ...
45
56def f1 (data ):
67 return data
@@ -9,7 +10,10 @@ def f2(data):
910 yield data
1011
1112def f3 (data ):
12- raise RuntimeError
13+ raise TestError
14+
15+ def f4 (data ):
16+ return [data ]
1317
1418async def af1 (data ):
1519 return data
@@ -18,7 +22,7 @@ async def af2(data):
1822 yield data
1923
2024async def af3 (data ):
21- raise RuntimeError
25+ raise TestError
2226
2327async def af4 (data ):
2428 async for row in data :
@@ -31,24 +35,29 @@ async def consumer(data):
3135 return total
3236
3337@pytest .mark .asyncio
34- async def test_pipeline ():
35- p = task (f1 ) | task (f2 )
36- assert p (1 ).__next__ () == 1
38+ async def test_aiterable_branched_pipeline ():
39+ p = task (af1 ) | task (f2 , branch = True )
40+ assert await p (1 ).__anext__ () == 1
41+
42+ @pytest .mark .asyncio
43+ async def test_iterable_branched_pipeline ():
44+ p = task (af1 ) | task (f4 , branch = True )
45+ assert await p (1 ).__anext__ () == 1
3746
3847@pytest .mark .asyncio
3948async def test_joined_pipeline ():
40- p = task (af1 ) | task (af2 ) | task (af4 , join = True )
49+ p = task (af1 ) | task (af2 , branch = True ) | task (af4 , branch = True , join = True )
4150 assert await p (1 ).__anext__ () == 1
4251
4352@pytest .mark .asyncio
4453async def test_consumer ():
45- p = task (af1 ) | task (af2 ) > consumer
54+ p = task (af1 ) | task (af2 , branch = True ) > consumer
4655 assert await p (1 ) == 1
4756
4857@pytest .mark .asyncio
49- async def test_invalid_first_stage_concurrency ():
58+ async def test_invalid_first_stage_workers ():
5059 try :
51- p = task (af1 , concurrency = 2 ) | task (af2 ) > consumer
60+ p = task (af1 , workers = 2 ) | task (af2 , branch = True ) > consumer
5261 await p (1 )
5362 except Exception as e :
5463 assert isinstance (e , RuntimeError )
@@ -58,35 +67,48 @@ async def test_invalid_first_stage_concurrency():
5867@pytest .mark .asyncio
5968async def test_invalid_first_stage_join ():
6069 try :
61- p = task (af1 , join = True ) | task (af2 ) > consumer
70+ p = task (af1 , join = True ) | task (af2 , branch = True ) > consumer
6271 await p (1 )
6372 except Exception as e :
6473 assert isinstance (e , RuntimeError )
6574 else :
6675 raise AssertionError
67-
76+
6877@pytest .mark .asyncio
69- async def test_error_handling ():
78+ async def test_invalid_branch_result ():
7079 try :
71- p = task (af1 ) | task ( af2 ) | task ( af3 ) > consumer
80+ p = task (af1 , branch = True ) > consumer
7281 await p (1 )
7382 except Exception as e :
74- assert isinstance (e , RuntimeError )
83+ assert isinstance (e , TypeError )
7584 else :
7685 raise AssertionError
77-
78- @pytest .mark .asyncio
79- async def test_unified_pipeline ():
80- p = task (af1 ) | task (f1 ) | task (af2 ) | task (f2 ) > consumer
81- assert await p (1 ) == 1
8286
83- @pytest .mark .asyncio
84- async def test_error_handling_in_daemon ():
87+ async def _try_catch_error (pipeline ):
8588 try :
86- p = task (af1 ) | task ( af2 ) | task ( f3 , daemon = True ) > consumer
89+ p = task (af1 ) | pipeline > consumer
8790 await p (1 )
8891 except Exception as e :
89- assert isinstance (e , RuntimeError )
92+ return isinstance (e , TestError )
9093 else :
91- raise AssertionError
92-
94+ return False
95+
96+ @pytest .mark .asyncio
97+ async def test_async_error_handling ():
98+ p = task (af3 )
99+ assert await _try_catch_error (p )
100+
101+ @pytest .mark .asyncio
102+ async def test_threaded_error_handling ():
103+ p = task (f3 , workers = 2 )
104+ assert await _try_catch_error (p )
105+
106+ @pytest .mark .asyncio
107+ async def test_multiprocessed_error_handling ():
108+ p = task (f3 , workers = 2 , multiprocess = True )
109+ assert await _try_catch_error (p )
110+
111+ @pytest .mark .asyncio
112+ async def test_unified_pipeline ():
113+ p = task (af1 ) | task (f1 ) | task (f2 , branch = True , multiprocess = True ) > consumer
114+ assert await p (1 ) == 1
0 commit comments