Skip to content

Commit 81e90bf

Browse files
committed
Add tests for async generators.
1 parent 770ce09 commit 81e90bf

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

Lib/test/test_free_threading/test_generators.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,44 @@
66
from test.support import threading_helper
77
from test.support import import_helper
88
from threading import Thread
9+
import types
910

1011
@threading_helper.requires_working_threading()
1112
@support.requires_resource("cpu")
12-
class TestGen(TestCase):
13+
class GeneratorFreeThreadingTests(TestCase):
1314
def infinite_generator(self):
1415
def gen():
1516
while True:
1617
yield
1718

1819
return gen()
1920

20-
def infinite_coroutine(self):
21+
def infinite_coroutine(self, from_gen: bool = False):
2122
asyncio = import_helper.import_module("asyncio")
2223

23-
async def coro():
24+
if from_gen:
25+
@types.coroutine
26+
def coro():
27+
while True:
28+
yield from asyncio.sleep(0)
29+
else:
30+
async def coro():
31+
while True:
32+
await asyncio.sleep(0)
33+
34+
return coro()
35+
36+
def infinite_asyncgen(self):
37+
asyncio = import_helper.import_module("asyncio")
38+
39+
async def async_gen():
2440
while True:
2541
await asyncio.sleep(0)
42+
yield 42
2643

27-
return coro()
44+
return async_gen()
2845

29-
def _stress_genlike(self, gen, *funcs):
46+
def _stress_object(self, gen, *funcs):
3047
threads = []
3148

3249
for func in funcs:
@@ -47,10 +64,13 @@ def with_iterations(gen):
4764
thread.join()
4865

4966
def stress_generator(self, *funcs):
50-
self._stress_genlike(self.infinite_generator(), *funcs)
67+
self._stress_object(self.infinite_generator(), *funcs)
68+
69+
def stress_coroutine(self, *funcs, from_gen: bool = False):
70+
self._stress_object(self.infinite_coroutine(from_gen=from_gen), *funcs)
5171

52-
def stress_coroutine(self, *funcs):
53-
self._stress_genlike(self.infinite_coroutine(), *funcs)
72+
def stress_asyncgen(self, *funcs):
73+
self._stress_object(self.infinite_asyncgen(), *funcs)
5474

5575
def test_generator_send(self):
5676
self.stress_generator(lambda gen: next(gen))
@@ -81,5 +101,14 @@ def test_coroutine_attributes(self):
81101
lambda coro: coro.cr_suspended,
82102
)
83103

104+
def test_generator_coroutine(self):
105+
self.stress_coroutine(lambda coro: next(coro), from_gen=True)
106+
107+
def test_async_gen(self):
108+
self.stress_asyncgen(lambda ag: next(ag), lambda ag: ag.send(None))
109+
self.stress_asyncgen(lambda ag: next(ag), lambda ag: ag.throw(RuntimeError))
110+
self.stress_asyncgen(lambda ag: next(ag), lambda ag: ag.close())
111+
self.stress_asyncgen(lambda ag: ag.throw(RuntimeError), lambda ag: ag.close())
112+
84113
if __name__ == "__main__":
85114
unittest.main()

0 commit comments

Comments
 (0)