44
55from test import support
66from test .support import threading_helper
7+ from test .support import import_helper
78from 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+
5184if __name__ == "__main__" :
5285 unittest .main ()
0 commit comments