27
27
28
28
import asyncio
29
29
from asyncio import coroutines
30
+ from asyncio import events
30
31
from asyncio import proactor_events
31
32
from asyncio import selector_events
32
33
from test .test_asyncio import utils as test_utils
@@ -2145,23 +2146,6 @@ def tearDown(self):
2145
2146
asyncio .set_child_watcher (None )
2146
2147
super ().tearDown ()
2147
2148
2148
- def test_get_event_loop_new_process (self ):
2149
- # Issue bpo-32126: The multiprocessing module used by
2150
- # ProcessPoolExecutor is not functional when the
2151
- # multiprocessing.synchronize module cannot be imported.
2152
- support .import_module ('multiprocessing.synchronize' )
2153
- async def main ():
2154
- pool = concurrent .futures .ProcessPoolExecutor ()
2155
- result = await self .loop .run_in_executor (
2156
- pool , _test_get_event_loop_new_process__sub_proc )
2157
- pool .shutdown ()
2158
- return result
2159
-
2160
- self .unpatch_get_running_loop ()
2161
-
2162
- self .assertEqual (
2163
- self .loop .run_until_complete (main ()),
2164
- 'hello' )
2165
2149
2166
2150
if hasattr (selectors , 'KqueueSelector' ):
2167
2151
class KqueueEventLoopTests (UnixEventLoopTestsMixin ,
@@ -2722,17 +2706,95 @@ def test_set_event_loop_policy(self):
2722
2706
self .assertIs (policy , asyncio .get_event_loop_policy ())
2723
2707
self .assertIsNot (policy , old_policy )
2724
2708
2709
+
2710
+ class GetEventLoopTestsMixin :
2711
+
2712
+ _get_running_loop_impl = None
2713
+ _set_running_loop_impl = None
2714
+ get_running_loop_impl = None
2715
+ get_event_loop_impl = None
2716
+
2717
+ def setUp (self ):
2718
+ self ._get_running_loop_saved = events ._get_running_loop
2719
+ self ._set_running_loop_saved = events ._set_running_loop
2720
+ self .get_running_loop_saved = events .get_running_loop
2721
+ self .get_event_loop_saved = events .get_event_loop
2722
+
2723
+ events ._get_running_loop = type (self )._get_running_loop_impl
2724
+ events ._set_running_loop = type (self )._set_running_loop_impl
2725
+ events .get_running_loop = type (self ).get_running_loop_impl
2726
+ events .get_event_loop = type (self ).get_event_loop_impl
2727
+
2728
+ asyncio ._get_running_loop = type (self )._get_running_loop_impl
2729
+ asyncio ._set_running_loop = type (self )._set_running_loop_impl
2730
+ asyncio .get_running_loop = type (self ).get_running_loop_impl
2731
+ asyncio .get_event_loop = type (self ).get_event_loop_impl
2732
+
2733
+ super ().setUp ()
2734
+
2735
+ self .loop = asyncio .new_event_loop ()
2736
+ asyncio .set_event_loop (self .loop )
2737
+
2738
+ watcher = asyncio .SafeChildWatcher ()
2739
+ watcher .attach_loop (self .loop )
2740
+ asyncio .set_child_watcher (watcher )
2741
+
2742
+ def tearDown (self ):
2743
+ try :
2744
+ asyncio .set_child_watcher (None )
2745
+ super ().tearDown ()
2746
+ finally :
2747
+ self .loop .close ()
2748
+ asyncio .set_event_loop (None )
2749
+
2750
+ events ._get_running_loop = self ._get_running_loop_saved
2751
+ events ._set_running_loop = self ._set_running_loop_saved
2752
+ events .get_running_loop = self .get_running_loop_saved
2753
+ events .get_event_loop = self .get_event_loop_saved
2754
+
2755
+ asyncio ._get_running_loop = self ._get_running_loop_saved
2756
+ asyncio ._set_running_loop = self ._set_running_loop_saved
2757
+ asyncio .get_running_loop = self .get_running_loop_saved
2758
+ asyncio .get_event_loop = self .get_event_loop_saved
2759
+
2760
+ if sys .platform != 'win32' :
2761
+
2762
+ def test_get_event_loop_new_process (self ):
2763
+ # Issue bpo-32126: The multiprocessing module used by
2764
+ # ProcessPoolExecutor is not functional when the
2765
+ # multiprocessing.synchronize module cannot be imported.
2766
+ support .import_module ('multiprocessing.synchronize' )
2767
+
2768
+ async def main ():
2769
+ pool = concurrent .futures .ProcessPoolExecutor ()
2770
+ result = await self .loop .run_in_executor (
2771
+ pool , _test_get_event_loop_new_process__sub_proc )
2772
+ pool .shutdown ()
2773
+ return result
2774
+
2775
+ self .assertEqual (
2776
+ self .loop .run_until_complete (main ()),
2777
+ 'hello' )
2778
+
2725
2779
def test_get_event_loop_returns_running_loop (self ):
2780
+ class TestError (Exception ):
2781
+ pass
2782
+
2726
2783
class Policy (asyncio .DefaultEventLoopPolicy ):
2727
2784
def get_event_loop (self ):
2728
- raise NotImplementedError
2729
-
2730
- loop = None
2785
+ raise TestError
2731
2786
2732
2787
old_policy = asyncio .get_event_loop_policy ()
2733
2788
try :
2734
2789
asyncio .set_event_loop_policy (Policy ())
2735
2790
loop = asyncio .new_event_loop ()
2791
+
2792
+ with self .assertRaises (TestError ):
2793
+ asyncio .get_event_loop ()
2794
+ asyncio .set_event_loop (None )
2795
+ with self .assertRaises (TestError ):
2796
+ asyncio .get_event_loop ()
2797
+
2736
2798
with self .assertRaisesRegex (RuntimeError , 'no running' ):
2737
2799
self .assertIs (asyncio .get_running_loop (), None )
2738
2800
self .assertIs (asyncio ._get_running_loop (), None )
@@ -2743,6 +2805,15 @@ async def func():
2743
2805
self .assertIs (asyncio ._get_running_loop (), loop )
2744
2806
2745
2807
loop .run_until_complete (func ())
2808
+
2809
+ asyncio .set_event_loop (loop )
2810
+ with self .assertRaises (TestError ):
2811
+ asyncio .get_event_loop ()
2812
+
2813
+ asyncio .set_event_loop (None )
2814
+ with self .assertRaises (TestError ):
2815
+ asyncio .get_event_loop ()
2816
+
2746
2817
finally :
2747
2818
asyncio .set_event_loop_policy (old_policy )
2748
2819
if loop is not None :
@@ -2754,5 +2825,27 @@ async def func():
2754
2825
self .assertIs (asyncio ._get_running_loop (), None )
2755
2826
2756
2827
2828
+ class TestPyGetEventLoop (GetEventLoopTestsMixin , unittest .TestCase ):
2829
+
2830
+ _get_running_loop_impl = events ._py__get_running_loop
2831
+ _set_running_loop_impl = events ._py__set_running_loop
2832
+ get_running_loop_impl = events ._py_get_running_loop
2833
+ get_event_loop_impl = events ._py_get_event_loop
2834
+
2835
+
2836
+ try :
2837
+ import _asyncio # NoQA
2838
+ except ImportError :
2839
+ pass
2840
+ else :
2841
+
2842
+ class TestCGetEventLoop (GetEventLoopTestsMixin , unittest .TestCase ):
2843
+
2844
+ _get_running_loop_impl = events ._c__get_running_loop
2845
+ _set_running_loop_impl = events ._c__set_running_loop
2846
+ get_running_loop_impl = events ._c_get_running_loop
2847
+ get_event_loop_impl = events ._c_get_event_loop
2848
+
2849
+
2757
2850
if __name__ == '__main__' :
2758
2851
unittest .main ()
0 commit comments