From: Ben Darnell Date: Thu, 30 Jul 2015 02:49:54 +0000 (-0400) Subject: Fix incorrect check in IOLoop(make_current=True). X-Git-Tag: v4.3.0b1~72 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d02ed606f1c52636462633d009bdcbaac644331;p=thirdparty%2Ftornado.git Fix incorrect check in IOLoop(make_current=True). Expand tests. Fixes #1469. --- diff --git a/tornado/ioloop.py b/tornado/ioloop.py index 67e33b521..87d4168e4 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -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() diff --git a/tornado/test/ioloop_test.py b/tornado/test/ioloop_test.py index f3a0cbdcf..062d7225b 100644 --- a/tornado/test/ioloop_test.py +++ b/tornado/test/ioloop_test.py @@ -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):