From: Ovidiu Predescu Date: Tue, 12 Jul 2011 20:32:59 +0000 (-0700) Subject: Don't use defaults in reactor.py as they cause unneeded initializations. X-Git-Tag: v2.1.0~86 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3bb589fbd0c88dd12e65ceacda358867d1ec299e;p=thirdparty%2Ftornado.git Don't use defaults in reactor.py as they cause unneeded initializations. Fix the unittest to not initialize IOLoop's global instance. --- diff --git a/tornado/test/runtests.py b/tornado/test/runtests.py index e18b24f31..cbaa068d8 100755 --- a/tornado/test/runtests.py +++ b/tornado/test/runtests.py @@ -14,11 +14,11 @@ TEST_MODULES = [ 'tornado.test.ioloop_test', 'tornado.test.iostream_test', 'tornado.test.process_test', - 'tornado.test.twistedreactor_test', 'tornado.test.simple_httpclient_test', 'tornado.test.stack_context_test', 'tornado.test.template_test', 'tornado.test.testing_test', + 'tornado.test.twistedreactor_test', 'tornado.test.web_test', 'tornado.test.wsgi_test', ] diff --git a/tornado/test/twistedreactor_test.py b/tornado/test/twistedreactor_test.py index e82af3a2a..7f9a383be 100644 --- a/tornado/test/twistedreactor_test.py +++ b/tornado/test/twistedreactor_test.py @@ -23,14 +23,11 @@ import sys import thread import unittest -import tornado.twisted.reactor -tornado.twisted.reactor.install() -from twisted.internet import reactor - from twisted.internet.interfaces import IReadDescriptor, IWriteDescriptor from twisted.python import log from tornado.platform.auto import Waker +from tornado.ioloop import IOLoop from tornado.twisted.reactor import TornadoReactor from tornado.testing import AsyncTestCase, LogTrapTestCase @@ -40,7 +37,7 @@ log.startLogging(sys.stdout) class ReactorWhenRunningTest(unittest.TestCase): def setUp(self): - self._reactor = TornadoReactor() + self._reactor = TornadoReactor(IOLoop()) def test_whenRunning(self): self._whenRunningCalled = False @@ -60,7 +57,7 @@ class ReactorWhenRunningTest(unittest.TestCase): class ReactorCallLaterTest(unittest.TestCase): def setUp(self): - self._reactor = TornadoReactor() + self._reactor = TornadoReactor(IOLoop()) def test_callLater(self): self._laterCalled = False @@ -80,7 +77,7 @@ class ReactorCallLaterTest(unittest.TestCase): class ReactorTwoCallLaterTest(unittest.TestCase): def setUp(self): - self._reactor = TornadoReactor() + self._reactor = TornadoReactor(IOLoop()) def test_callLater(self): self._later1Called = False @@ -110,7 +107,7 @@ class ReactorTwoCallLaterTest(unittest.TestCase): class ReactorCallFromThreadTest(unittest.TestCase): def setUp(self): - self._reactor = TornadoReactor() + self._reactor = TornadoReactor(IOLoop()) self._mainThread = thread.get_ident() def _newThreadRun(self, a, b): @@ -130,7 +127,7 @@ class ReactorCallFromThreadTest(unittest.TestCase): class ReactorCallInThread(unittest.TestCase): def setUp(self): - self._reactor = TornadoReactor() + self._reactor = TornadoReactor(IOLoop()) self._mainThread = thread.get_ident() def _fnCalledInThread(self, *args, **kwargs): @@ -190,7 +187,7 @@ class ReactorReaderWriterTest(unittest.TestCase): fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) def setUp(self): - self._reactor = TornadoReactor() + self._reactor = TornadoReactor(IOLoop()) r, w = os.pipe() self._set_nonblocking(r) self._set_nonblocking(w) @@ -205,7 +202,7 @@ class ReactorReaderWriterTest(unittest.TestCase): reads it, check the value and ends the test. """ def checkReadInput(fd): - self.assertEqual(fd.read(), 'x') + self.assertTrue(fd.read().startswith('x')) self._reactor.stop() self._reader = Reader(self._p1, checkReadInput) self._writer = Writer(self._p2, lambda fd: fd.write('x')) diff --git a/tornado/twisted/reactor.py b/tornado/twisted/reactor.py index 0b6d02f6d..4f3d1fc71 100644 --- a/tornado/twisted/reactor.py +++ b/tornado/twisted/reactor.py @@ -88,7 +88,9 @@ class TornadoReactor(PosixReactorBase): """ implements(IReactorTime, IReactorFDSet) - def __init__(self, ioloop=tornado.ioloop.IOLoop.instance()): + def __init__(self, ioloop): + if not ioloop: + ioloop = tornado.ioloop.IOLoop.instance() self._ioloop = ioloop self._readers = {} self._writers = {} @@ -241,10 +243,12 @@ class TornadoReactor(PosixReactorBase): self.running = True self._ioloop.start() -def install(ioloop=tornado.ioloop.IOLoop.instance()): +def install(ioloop=None): """ Install the Tornado reactor. """ + if not ioloop: + ioloop = tornado.ioloop.IOLoop.instance() reactor = TornadoReactor(ioloop) from twisted.internet.main import installReactor installReactor(reactor)