Skip to content

Commit 77a33bb

Browse files
committed
Add tests for coroutines.
1 parent 7cd787f commit 77a33bb

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

Lib/test/test_free_threading/test_generators.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from test import support
66
from test.support import threading_helper
7+
from test.support import import_helper
78
from threading import Thread
89

910
@threading_helper.requires_working_threading()
@@ -16,9 +17,17 @@ def gen():
1617

1718
return gen()
1819

19-
def stress_generator(self, *funcs):
20+
def infinite_coroutine(self):
21+
asyncio = import_helper.import_module("asyncio")
22+
23+
async def coro():
24+
while True:
25+
await asyncio.sleep(0)
26+
27+
return coro()
28+
29+
def _stress_genlike(self, gen, *funcs):
2030
threads = []
21-
gen = self.infinite_generator()
2231

2332
for func in funcs:
2433
for _ in range(10):
@@ -37,6 +46,12 @@ def with_iterations(gen):
3746
for thread in threads:
3847
thread.join()
3948

49+
def stress_generator(self, *funcs):
50+
self._stress_genlike(self.infinite_generator(), *funcs)
51+
52+
def stress_coroutine(self, *funcs):
53+
self._stress_genlike(self.infinite_coroutine(), *funcs)
54+
4055
def test_generator_send(self):
4156
self.stress_generator(lambda gen: next(gen))
4257

@@ -48,5 +63,23 @@ def test_generator_state(self):
4863
self.stress_generator(lambda gen: next(gen), lambda gen: gen.gi_running)
4964
self.stress_generator(lambda gen: gen.gi_isrunning, lambda gen: gen.close())
5065

66+
def test_coroutine_send(self):
67+
def try_send_non_none(coro):
68+
try:
69+
coro.send(42)
70+
except ValueError:
71+
# Can't send non-None to just started coroutine
72+
coro.send(None)
73+
74+
self.stress_coroutine(lambda coro: next(coro))
75+
self.stress_coroutine(try_send_non_none)
76+
77+
def test_coroutine_attributes(self):
78+
self.stress_coroutine(
79+
lambda coro: next(coro),
80+
lambda coro: coro.cr_frame,
81+
lambda coro: coro.cr_suspended,
82+
)
83+
5184
if __name__ == "__main__":
5285
unittest.main()

0 commit comments

Comments
 (0)