diff options
author | Yury Selivanov <yury@magic.io> | 2016-11-04 18:29:28 (GMT) |
---|---|---|
committer | Yury Selivanov <yury@magic.io> | 2016-11-04 18:29:28 (GMT) |
commit | 600a349781bfa0a8239e1cb95fac29c7c4a3302e (patch) | |
tree | 79f35114de7101a97fe85f80cd02cbd866d4abd9 /Lib/test/test_asyncio | |
parent | 1ea023e523f484955fb81b59a72c586845d8d97f (diff) | |
download | cpython-600a349781bfa0a8239e1cb95fac29c7c4a3302e.zip cpython-600a349781bfa0a8239e1cb95fac29c7c4a3302e.tar.gz cpython-600a349781bfa0a8239e1cb95fac29c7c4a3302e.tar.bz2 |
Issue #28613: Fix get_event_loop() to return the current loop
when called from coroutines or callbacks.
Diffstat (limited to 'Lib/test/test_asyncio')
-rw-r--r-- | Lib/test/test_asyncio/test_base_events.py | 20 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_events.py | 23 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_futures.py | 3 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_locks.py | 4 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_pep492.py | 1 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_proactor_events.py | 3 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_queues.py | 1 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_selector_events.py | 5 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_sslproto.py | 1 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_streams.py | 1 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_subprocess.py | 3 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_tasks.py | 5 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_unix_events.py | 5 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_windows_events.py | 1 |
14 files changed, 76 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py index 3913125..cdbd587 100644 --- a/Lib/test/test_asyncio/test_base_events.py +++ b/Lib/test/test_asyncio/test_base_events.py @@ -154,6 +154,7 @@ class BaseEventTests(test_utils.TestCase): class BaseEventLoopTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = base_events.BaseEventLoop() self.loop._selector = mock.Mock() self.loop._selector.select.return_value = () @@ -976,6 +977,7 @@ class MyDatagramProto(asyncio.DatagramProtocol): class BaseEventLoopWithSelectorTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = asyncio.new_event_loop() self.set_event_loop(self.loop) @@ -1692,5 +1694,23 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase): "took .* seconds$") +class RunningLoopTests(unittest.TestCase): + + def test_running_loop_within_a_loop(self): + @asyncio.coroutine + def runner(loop): + loop.run_forever() + + loop = asyncio.new_event_loop() + outer_loop = asyncio.new_event_loop() + try: + with self.assertRaisesRegex(RuntimeError, + 'while another loop is running'): + outer_loop.run_until_complete(runner(loop)) + finally: + loop.close() + outer_loop.close() + + if __name__ == '__main__': unittest.main() diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index d8946e3..4c18300 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -2233,6 +2233,7 @@ def noop(*args, **kwargs): class HandleTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = mock.Mock() self.loop.get_debug.return_value = True @@ -2411,6 +2412,7 @@ class HandleTests(test_utils.TestCase): class TimerTests(unittest.TestCase): def setUp(self): + super().setUp() self.loop = mock.Mock() def test_hash(self): @@ -2719,6 +2721,27 @@ class PolicyTests(unittest.TestCase): self.assertIs(policy, asyncio.get_event_loop_policy()) self.assertIsNot(policy, old_policy) + def test_get_event_loop_returns_running_loop(self): + class Policy(asyncio.DefaultEventLoopPolicy): + def get_event_loop(self): + raise NotImplementedError + + loop = None + + old_policy = asyncio.get_event_loop_policy() + try: + asyncio.set_event_loop_policy(Policy()) + loop = asyncio.new_event_loop() + + async def func(): + self.assertIs(asyncio.get_event_loop(), loop) + + loop.run_until_complete(func()) + finally: + asyncio.set_event_loop_policy(old_policy) + if loop is not None: + loop.close() + if __name__ == '__main__': unittest.main() diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index d20eb68..153b8ed 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -79,6 +79,7 @@ class DuckFuture: class DuckTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() self.addCleanup(self.loop.close) @@ -96,6 +97,7 @@ class DuckTests(test_utils.TestCase): class FutureTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() self.addCleanup(self.loop.close) @@ -468,6 +470,7 @@ class FutureTests(test_utils.TestCase): class FutureDoneCallbackTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() def run_briefly(self): diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index e557212..152948c 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -19,6 +19,7 @@ RGX_REPR = re.compile(STR_RGX_REPR) class LockTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() def test_ctor_loop(self): @@ -235,6 +236,7 @@ class LockTests(test_utils.TestCase): class EventTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() def test_ctor_loop(self): @@ -364,6 +366,7 @@ class EventTests(test_utils.TestCase): class ConditionTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() def test_ctor_loop(self): @@ -699,6 +702,7 @@ class ConditionTests(test_utils.TestCase): class SemaphoreTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() def test_ctor_loop(self): diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py index 29aba81..d5b8522 100644 --- a/Lib/test/test_asyncio/test_pep492.py +++ b/Lib/test/test_asyncio/test_pep492.py @@ -17,6 +17,7 @@ from asyncio import test_utils class BaseTest(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = asyncio.BaseEventLoop() self.loop._process_events = mock.Mock() self.loop._selector = mock.Mock() diff --git a/Lib/test/test_asyncio/test_proactor_events.py b/Lib/test/test_asyncio/test_proactor_events.py index 5a92b1e..4dfc612 100644 --- a/Lib/test/test_asyncio/test_proactor_events.py +++ b/Lib/test/test_asyncio/test_proactor_events.py @@ -24,6 +24,7 @@ def close_transport(transport): class ProactorSocketTransportTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() self.addCleanup(self.loop.close) self.proactor = mock.Mock() @@ -436,6 +437,8 @@ class ProactorSocketTransportTests(test_utils.TestCase): class BaseProactorEventLoopTests(test_utils.TestCase): def setUp(self): + super().setUp() + self.sock = test_utils.mock_nonblocking_socket() self.proactor = mock.Mock() diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py index 591a9bb..fe5a6db 100644 --- a/Lib/test/test_asyncio/test_queues.py +++ b/Lib/test/test_asyncio/test_queues.py @@ -10,6 +10,7 @@ from asyncio import test_utils class _QueueTestBase(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() diff --git a/Lib/test/test_asyncio/test_selector_events.py b/Lib/test/test_asyncio/test_selector_events.py index 07de640..6bf7862 100644 --- a/Lib/test/test_asyncio/test_selector_events.py +++ b/Lib/test/test_asyncio/test_selector_events.py @@ -51,6 +51,7 @@ def close_transport(transport): class BaseSelectorEventLoopTests(test_utils.TestCase): def setUp(self): + super().setUp() self.selector = mock.Mock() self.selector.select.return_value = [] self.loop = TestBaseSelectorEventLoop(self.selector) @@ -698,6 +699,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase): class SelectorTransportTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() self.protocol = test_utils.make_test_protocol(asyncio.Protocol) self.sock = mock.Mock(socket.socket) @@ -793,6 +795,7 @@ class SelectorTransportTests(test_utils.TestCase): class SelectorSocketTransportTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() self.protocol = test_utils.make_test_protocol(asyncio.Protocol) self.sock = mock.Mock(socket.socket) @@ -1141,6 +1144,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): class SelectorSslTransportTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() self.protocol = test_utils.make_test_protocol(asyncio.Protocol) self.sock = mock.Mock(socket.socket) @@ -1501,6 +1505,7 @@ class SelectorSslWithoutSslTransportTests(unittest.TestCase): class SelectorDatagramTransportTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() self.protocol = test_utils.make_test_protocol(asyncio.DatagramProtocol) self.sock = mock.Mock(spec_set=socket.socket) diff --git a/Lib/test/test_asyncio/test_sslproto.py b/Lib/test/test_asyncio/test_sslproto.py index 7dfa6c2..0ca6d1b 100644 --- a/Lib/test/test_asyncio/test_sslproto.py +++ b/Lib/test/test_asyncio/test_sslproto.py @@ -18,6 +18,7 @@ from asyncio import test_utils class SslProtoHandshakeTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = asyncio.new_event_loop() self.set_event_loop(self.loop) diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index 35557c3..b47433a 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -22,6 +22,7 @@ class StreamReaderTests(test_utils.TestCase): DATA = b'line1\nline2\nline3\n' def setUp(self): + super().setUp() self.loop = asyncio.new_event_loop() self.set_event_loop(self.loop) diff --git a/Lib/test/test_asyncio/test_subprocess.py b/Lib/test/test_asyncio/test_subprocess.py index 1531023..bba688b 100644 --- a/Lib/test/test_asyncio/test_subprocess.py +++ b/Lib/test/test_asyncio/test_subprocess.py @@ -35,6 +35,7 @@ class TestSubprocessTransport(base_subprocess.BaseSubprocessTransport): class SubprocessTransportTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() self.set_event_loop(self.loop) @@ -466,6 +467,7 @@ if sys.platform != 'win32': Watcher = None def setUp(self): + super().setUp() policy = asyncio.get_event_loop_policy() self.loop = policy.new_event_loop() self.set_event_loop(self.loop) @@ -490,6 +492,7 @@ else: class SubprocessProactorTests(SubprocessMixin, test_utils.TestCase): def setUp(self): + super().setUp() self.loop = asyncio.ProactorEventLoop() self.set_event_loop(self.loop) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 1ceb9b2..22accf5 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -75,6 +75,7 @@ class Dummy: class TaskTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() def test_other_loop_future(self): @@ -1933,6 +1934,7 @@ class TaskTests(test_utils.TestCase): class GatherTestsBase: def setUp(self): + super().setUp() self.one_loop = self.new_test_loop() self.other_loop = self.new_test_loop() self.set_event_loop(self.one_loop, cleanup=False) @@ -2216,6 +2218,7 @@ class RunCoroutineThreadsafeTests(test_utils.TestCase): """Test case for asyncio.run_coroutine_threadsafe.""" def setUp(self): + super().setUp() self.loop = asyncio.new_event_loop() self.set_event_loop(self.loop) # Will cleanup properly @@ -2306,12 +2309,14 @@ class RunCoroutineThreadsafeTests(test_utils.TestCase): class SleepTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) def tearDown(self): self.loop.close() self.loop = None + super().tearDown() def test_sleep_zero(self): result = 0 diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index ce897ed..83a035e 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -40,6 +40,7 @@ def close_pipe_transport(transport): class SelectorEventLoopSignalTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = asyncio.SelectorEventLoop() self.set_event_loop(self.loop) @@ -234,6 +235,7 @@ class SelectorEventLoopSignalTests(test_utils.TestCase): class SelectorEventLoopUnixSocketTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = asyncio.SelectorEventLoop() self.set_event_loop(self.loop) @@ -338,6 +340,7 @@ class SelectorEventLoopUnixSocketTests(test_utils.TestCase): class UnixReadPipeTransportTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() self.protocol = test_utils.make_test_protocol(asyncio.Protocol) self.pipe = mock.Mock(spec_set=io.RawIOBase) @@ -487,6 +490,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase): class UnixWritePipeTransportTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = self.new_test_loop() self.protocol = test_utils.make_test_protocol(asyncio.BaseProtocol) self.pipe = mock.Mock(spec_set=io.RawIOBase) @@ -805,6 +809,7 @@ class ChildWatcherTestsMixin: ignore_warnings = mock.patch.object(log.logger, "warning") def setUp(self): + super().setUp() self.loop = self.new_test_loop() self.running = False self.zombies = {} diff --git a/Lib/test/test_asyncio/test_windows_events.py b/Lib/test/test_asyncio/test_windows_events.py index 7fcf402..1afcae1 100644 --- a/Lib/test/test_asyncio/test_windows_events.py +++ b/Lib/test/test_asyncio/test_windows_events.py @@ -31,6 +31,7 @@ class UpperProto(asyncio.Protocol): class ProactorTests(test_utils.TestCase): def setUp(self): + super().setUp() self.loop = asyncio.ProactorEventLoop() self.set_event_loop(self.loop) |