Skip to content

Commit

Permalink
Don't use defaults in reactor.py as they cause unneeded initializations.
Browse files Browse the repository at this point in the history
Fix the unittest to not initialize IOLoop's global instance.
  • Loading branch information
ovidiucp committed Jul 12, 2011
1 parent ec838ed commit 3bb589f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tornado/test/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
'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',
]
Expand Down
19 changes: 8 additions & 11 deletions tornado/test/twistedreactor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@
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

Expand All @@ -40,7 +37,7 @@

class ReactorWhenRunningTest(unittest.TestCase):
def setUp(self):
self._reactor = TornadoReactor()
self._reactor = TornadoReactor(IOLoop())

def test_whenRunning(self):
self._whenRunningCalled = False
Expand All @@ -60,7 +57,7 @@ def anotherWhenRunningCallback(self):

class ReactorCallLaterTest(unittest.TestCase):
def setUp(self):
self._reactor = TornadoReactor()
self._reactor = TornadoReactor(IOLoop())

def test_callLater(self):
self._laterCalled = False
Expand All @@ -80,7 +77,7 @@ def callLaterCallback(self):

class ReactorTwoCallLaterTest(unittest.TestCase):
def setUp(self):
self._reactor = TornadoReactor()
self._reactor = TornadoReactor(IOLoop())

def test_callLater(self):
self._later1Called = False
Expand Down Expand Up @@ -110,7 +107,7 @@ def callLaterCallback2(self):

class ReactorCallFromThreadTest(unittest.TestCase):
def setUp(self):
self._reactor = TornadoReactor()
self._reactor = TornadoReactor(IOLoop())
self._mainThread = thread.get_ident()

def _newThreadRun(self, a, b):
Expand All @@ -130,7 +127,7 @@ def testCallFromThread(self):

class ReactorCallInThread(unittest.TestCase):
def setUp(self):
self._reactor = TornadoReactor()
self._reactor = TornadoReactor(IOLoop())
self._mainThread = thread.get_ident()

def _fnCalledInThread(self, *args, **kwargs):
Expand Down Expand Up @@ -190,7 +187,7 @@ def _set_nonblocking(self, fd):
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)
Expand All @@ -205,7 +202,7 @@ def _testReadWrite(self):
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'))
Expand Down
8 changes: 6 additions & 2 deletions tornado/twisted/reactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -241,10 +243,12 @@ def mainLoop(self):
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)
Expand Down

0 comments on commit 3bb589f

Please sign in to comment.