]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Fix tests: IOLoop.current() now behaves the same way on any thread
authorThomas Kluyver <thomas@kluyver.me.uk>
Fri, 10 Feb 2023 14:39:37 +0000 (14:39 +0000)
committerThomas Kluyver <thomas@kluyver.me.uk>
Fri, 10 Feb 2023 14:39:37 +0000 (14:39 +0000)
tornado/test/asyncio_test.py

index 6ad1dcfbbe93971acd0ef1d708bff35e48ace0b0..0b18ee5f961d4a269d4822e1ad211b904da9e903 100644 (file)
@@ -183,25 +183,37 @@ class AnyThreadEventLoopPolicyTest(unittest.TestCase):
         future = self.executor.submit(get_and_close_event_loop)
         return future.result()
 
-    def run_policy_test(self, accessor, expected_type):
+    def test_asyncio_accessor(self):
         with warnings.catch_warnings():
             warnings.simplefilter("ignore", DeprecationWarning)
             # With the default policy, non-main threads don't get an event
             # loop.
             self.assertRaises(
-                (RuntimeError, AssertionError), self.executor.submit(accessor).result
+                RuntimeError, self.executor.submit(asyncio.get_event_loop).result
             )
             # Set the policy and we can get a loop.
             asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
             self.assertIsInstance(
-                self.executor.submit(accessor).result(), expected_type
+                self.executor.submit(asyncio.get_event_loop).result(), asyncio.AbstractEventLoop
             )
             # Clean up to silence leak warnings. Always use asyncio since
             # IOLoop doesn't (currently) close the underlying loop.
             self.executor.submit(lambda: asyncio.get_event_loop().close()).result()  # type: ignore
 
-    def test_asyncio_accessor(self):
-        self.run_policy_test(asyncio.get_event_loop, asyncio.AbstractEventLoop)
-
     def test_tornado_accessor(self):
-        self.run_policy_test(IOLoop.current, IOLoop)
+        # Tornado's IOLoop.current() API can create a loop for any thread,
+        # regardless of this event loop policy.
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore", DeprecationWarning)
+            self.assertIsInstance(
+                self.executor.submit(IOLoop.current).result(), IOLoop
+            )
+            # Clean up to silence leak warnings. Always use asyncio since
+            # IOLoop doesn't (currently) close the underlying loop.
+            self.executor.submit(lambda: asyncio.get_event_loop().close()).result()  # type: ignore
+
+            asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
+            self.assertIsInstance(
+                self.executor.submit(IOLoop.current).result(), IOLoop
+            )
+            self.executor.submit(lambda: asyncio.get_event_loop().close()).result()  # type: ignore