]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Fix incorrect check in IOLoop(make_current=True).
authorBen Darnell <ben@bendarnell.com>
Thu, 30 Jul 2015 02:49:54 +0000 (22:49 -0400)
committerBen Darnell <ben@bendarnell.com>
Thu, 30 Jul 2015 02:49:54 +0000 (22:49 -0400)
Expand tests.

Fixes #1469.

tornado/ioloop.py
tornado/test/ioloop_test.py

index 67e33b521f41c12e8b14a38f54d1961245cad487..87d4168e49226ea77271c2b58adb3ae075887416 100644 (file)
@@ -249,7 +249,7 @@ class IOLoop(Configurable):
             if IOLoop.current(instance=False) is None:
                 self.make_current()
         elif make_current:
-            if IOLoop.current(instance=False) is None:
+            if IOLoop.current(instance=False) is not None:
                 raise RuntimeError("current IOLoop already exists")
             self.make_current()
 
index f3a0cbdcfe7313bb1057886d8acf01238c821b3e..062d7225b069b6d59fb608f7e49689b518e5250a 100644 (file)
@@ -409,18 +409,44 @@ class TestIOLoop(AsyncTestCase):
 # automatically set as current.
 class TestIOLoopCurrent(unittest.TestCase):
     def setUp(self):
-        self.io_loop = IOLoop()
+        self.io_loop = None
+        IOLoop.clear_current()
 
     def tearDown(self):
-        self.io_loop.close()
+        if self.io_loop is not None:
+            self.io_loop.close()
 
-    def test_current(self):
+    def test_default_current(self):
+        self.io_loop = IOLoop()
+        # The first IOLoop with default arguments is made current.
+        self.assertIs(self.io_loop, IOLoop.current())
+        # A second IOLoop can be created but is not made current.
+        io_loop2 = IOLoop()
+        self.assertIs(self.io_loop, IOLoop.current())
+        io_loop2.close()
+
+    def test_non_current(self):
+        self.io_loop = IOLoop(make_current=False)
+        # The new IOLoop is not initially made current.
+        self.assertIsNone(IOLoop.current(instance=False))
         def f():
+            # But it is current after it is started.
             self.current_io_loop = IOLoop.current()
             self.io_loop.stop()
         self.io_loop.add_callback(f)
         self.io_loop.start()
         self.assertIs(self.current_io_loop, self.io_loop)
+        # Now that the loop is stopped, it is no longer current.
+        self.assertIsNone(IOLoop.current(instance=False))
+
+    def test_force_current(self):
+        self.io_loop = IOLoop(make_current=True)
+        self.assertIs(self.io_loop, IOLoop.current())
+        with self.assertRaises(RuntimeError):
+            # A second make_current=True construction cannot succeed.
+            IOLoop(make_current=True)
+        # current() was not affected by the failed construction.
+        self.assertIs(self.io_loop, IOLoop.current())
 
 
 class TestIOLoopAddCallback(AsyncTestCase):