Skip to content

Commit 024a1d7

Browse files
committed
add units tests for async_.Routine
1 parent 30ad503 commit 024a1d7

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/enapter/async_/routine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self, task_group: asyncio.TaskGroup | None = None) -> None:
1212

1313
@abc.abstractmethod
1414
async def _run(self) -> None:
15-
pass
15+
pass # pragma: no cover
1616

1717
async def __aenter__(self) -> Self:
1818
await self.start()

tests/unit/test_async.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import asyncio
2+
13
import pytest
24

35
import enapter
@@ -10,11 +12,78 @@ async def test_aclose(self) -> None:
1012
async def agen():
1113
yield 1
1214
yield 2
13-
yield 3
15+
yield 3 # pragma: no cover
1416

1517
async with agen() as g:
1618
assert await g.__anext__() == 1
1719
assert await g.__anext__() == 2
1820

1921
with pytest.raises(StopAsyncIteration):
2022
await g.__anext__()
23+
24+
25+
class TestRoutine:
26+
27+
async def test_run_not_implemented(self):
28+
class R(enapter.async_.Routine):
29+
pass
30+
31+
with pytest.raises(TypeError):
32+
R()
33+
34+
async def test_start_twice(self) -> None:
35+
started = asyncio.Event()
36+
37+
class R(enapter.async_.Routine):
38+
async def _run(self):
39+
started.set()
40+
await asyncio.Event().wait()
41+
42+
async with R() as r:
43+
await started.wait()
44+
with pytest.raises(RuntimeError):
45+
await r.start()
46+
47+
async def test_stop_without_start(self) -> None:
48+
class R(enapter.async_.Routine):
49+
async def _run(self): # pragma: no cover
50+
await asyncio.Event().wait()
51+
52+
r = R()
53+
with pytest.raises(RuntimeError):
54+
await r.stop()
55+
56+
async def test_context_manager(self):
57+
started = asyncio.Event()
58+
done = asyncio.Event()
59+
60+
class R(enapter.async_.Routine):
61+
async def _run(self):
62+
started.set()
63+
try:
64+
await asyncio.Event().wait()
65+
finally:
66+
done.set()
67+
68+
async with R():
69+
await started.wait()
70+
71+
assert done.is_set()
72+
73+
async def test_task_group(self):
74+
started = asyncio.Event()
75+
done = asyncio.Event()
76+
77+
class R(enapter.async_.Routine):
78+
async def _run(self):
79+
started.set()
80+
try:
81+
await asyncio.Event().wait()
82+
finally:
83+
done.set()
84+
85+
async with asyncio.TaskGroup() as tg:
86+
async with R(task_group=tg):
87+
await started.wait()
88+
89+
assert done.is_set()

0 commit comments

Comments
 (0)