twisted_test.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. # Author: Ovidiu Predescu
  2. # Date: July 2011
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. """
  16. Unittest for the twisted-style reactor.
  17. """
  18. from __future__ import absolute_import, division, print_function
  19. import logging
  20. import os
  21. import shutil
  22. import signal
  23. import sys
  24. import tempfile
  25. import threading
  26. import warnings
  27. from tornado.escape import utf8
  28. from tornado import gen
  29. from tornado.httpclient import AsyncHTTPClient
  30. from tornado.httpserver import HTTPServer
  31. from tornado.ioloop import IOLoop, PollIOLoop
  32. from tornado.platform.auto import set_close_exec
  33. from tornado.testing import bind_unused_port
  34. from tornado.test.util import unittest
  35. from tornado.util import import_object, PY3
  36. from tornado.web import RequestHandler, Application
  37. try:
  38. import fcntl
  39. from twisted.internet.defer import Deferred, inlineCallbacks, returnValue # type: ignore
  40. from twisted.internet.interfaces import IReadDescriptor, IWriteDescriptor # type: ignore
  41. from twisted.internet.protocol import Protocol # type: ignore
  42. from twisted.python import log # type: ignore
  43. from tornado.platform.twisted import TornadoReactor, TwistedIOLoop
  44. from zope.interface import implementer # type: ignore
  45. have_twisted = True
  46. except ImportError:
  47. have_twisted = False
  48. # The core of Twisted 12.3.0 is available on python 3, but twisted.web is not
  49. # so test for it separately.
  50. try:
  51. from twisted.web.client import Agent, readBody # type: ignore
  52. from twisted.web.resource import Resource # type: ignore
  53. from twisted.web.server import Site # type: ignore
  54. # As of Twisted 15.0.0, twisted.web is present but fails our
  55. # tests due to internal str/bytes errors.
  56. have_twisted_web = sys.version_info < (3,)
  57. except ImportError:
  58. have_twisted_web = False
  59. if PY3:
  60. import _thread as thread
  61. else:
  62. import thread
  63. ResourceWarning = None
  64. try:
  65. import asyncio
  66. except ImportError:
  67. asyncio = None
  68. skipIfNoTwisted = unittest.skipUnless(have_twisted,
  69. "twisted module not present")
  70. def save_signal_handlers():
  71. saved = {}
  72. for sig in [signal.SIGINT, signal.SIGTERM, signal.SIGCHLD]:
  73. saved[sig] = signal.getsignal(sig)
  74. if "twisted" in repr(saved):
  75. if not issubclass(IOLoop.configured_class(), TwistedIOLoop):
  76. # when the global ioloop is twisted, we expect the signal
  77. # handlers to be installed. Otherwise, it means we're not
  78. # cleaning up after twisted properly.
  79. raise Exception("twisted signal handlers already installed")
  80. return saved
  81. def restore_signal_handlers(saved):
  82. for sig, handler in saved.items():
  83. signal.signal(sig, handler)
  84. class ReactorTestCase(unittest.TestCase):
  85. def setUp(self):
  86. self._saved_signals = save_signal_handlers()
  87. IOLoop.clear_current()
  88. self._io_loop = IOLoop(make_current=True)
  89. self._reactor = TornadoReactor()
  90. IOLoop.clear_current()
  91. def tearDown(self):
  92. self._io_loop.close(all_fds=True)
  93. restore_signal_handlers(self._saved_signals)
  94. @skipIfNoTwisted
  95. class ReactorWhenRunningTest(ReactorTestCase):
  96. def test_whenRunning(self):
  97. self._whenRunningCalled = False
  98. self._anotherWhenRunningCalled = False
  99. self._reactor.callWhenRunning(self.whenRunningCallback)
  100. self._reactor.run()
  101. self.assertTrue(self._whenRunningCalled)
  102. self.assertTrue(self._anotherWhenRunningCalled)
  103. def whenRunningCallback(self):
  104. self._whenRunningCalled = True
  105. self._reactor.callWhenRunning(self.anotherWhenRunningCallback)
  106. self._reactor.stop()
  107. def anotherWhenRunningCallback(self):
  108. self._anotherWhenRunningCalled = True
  109. @skipIfNoTwisted
  110. class ReactorCallLaterTest(ReactorTestCase):
  111. def test_callLater(self):
  112. self._laterCalled = False
  113. self._now = self._reactor.seconds()
  114. self._timeout = 0.001
  115. dc = self._reactor.callLater(self._timeout, self.callLaterCallback)
  116. self.assertEqual(self._reactor.getDelayedCalls(), [dc])
  117. self._reactor.run()
  118. self.assertTrue(self._laterCalled)
  119. self.assertTrue(self._called - self._now > self._timeout)
  120. self.assertEqual(self._reactor.getDelayedCalls(), [])
  121. def callLaterCallback(self):
  122. self._laterCalled = True
  123. self._called = self._reactor.seconds()
  124. self._reactor.stop()
  125. @skipIfNoTwisted
  126. class ReactorTwoCallLaterTest(ReactorTestCase):
  127. def test_callLater(self):
  128. self._later1Called = False
  129. self._later2Called = False
  130. self._now = self._reactor.seconds()
  131. self._timeout1 = 0.0005
  132. dc1 = self._reactor.callLater(self._timeout1, self.callLaterCallback1)
  133. self._timeout2 = 0.001
  134. dc2 = self._reactor.callLater(self._timeout2, self.callLaterCallback2)
  135. self.assertTrue(self._reactor.getDelayedCalls() == [dc1, dc2] or
  136. self._reactor.getDelayedCalls() == [dc2, dc1])
  137. self._reactor.run()
  138. self.assertTrue(self._later1Called)
  139. self.assertTrue(self._later2Called)
  140. self.assertTrue(self._called1 - self._now > self._timeout1)
  141. self.assertTrue(self._called2 - self._now > self._timeout2)
  142. self.assertEqual(self._reactor.getDelayedCalls(), [])
  143. def callLaterCallback1(self):
  144. self._later1Called = True
  145. self._called1 = self._reactor.seconds()
  146. def callLaterCallback2(self):
  147. self._later2Called = True
  148. self._called2 = self._reactor.seconds()
  149. self._reactor.stop()
  150. @skipIfNoTwisted
  151. class ReactorCallFromThreadTest(ReactorTestCase):
  152. def setUp(self):
  153. super(ReactorCallFromThreadTest, self).setUp()
  154. self._mainThread = thread.get_ident()
  155. def tearDown(self):
  156. self._thread.join()
  157. super(ReactorCallFromThreadTest, self).tearDown()
  158. def _newThreadRun(self):
  159. self.assertNotEqual(self._mainThread, thread.get_ident())
  160. if hasattr(self._thread, 'ident'): # new in python 2.6
  161. self.assertEqual(self._thread.ident, thread.get_ident())
  162. self._reactor.callFromThread(self._fnCalledFromThread)
  163. def _fnCalledFromThread(self):
  164. self.assertEqual(self._mainThread, thread.get_ident())
  165. self._reactor.stop()
  166. def _whenRunningCallback(self):
  167. self._thread = threading.Thread(target=self._newThreadRun)
  168. self._thread.start()
  169. def testCallFromThread(self):
  170. self._reactor.callWhenRunning(self._whenRunningCallback)
  171. self._reactor.run()
  172. @skipIfNoTwisted
  173. class ReactorCallInThread(ReactorTestCase):
  174. def setUp(self):
  175. super(ReactorCallInThread, self).setUp()
  176. self._mainThread = thread.get_ident()
  177. def _fnCalledInThread(self, *args, **kwargs):
  178. self.assertNotEqual(thread.get_ident(), self._mainThread)
  179. self._reactor.callFromThread(lambda: self._reactor.stop())
  180. def _whenRunningCallback(self):
  181. self._reactor.callInThread(self._fnCalledInThread)
  182. def testCallInThread(self):
  183. self._reactor.callWhenRunning(self._whenRunningCallback)
  184. self._reactor.run()
  185. if have_twisted:
  186. @implementer(IReadDescriptor)
  187. class Reader(object):
  188. def __init__(self, fd, callback):
  189. self._fd = fd
  190. self._callback = callback
  191. def logPrefix(self):
  192. return "Reader"
  193. def close(self):
  194. self._fd.close()
  195. def fileno(self):
  196. return self._fd.fileno()
  197. def readConnectionLost(self, reason):
  198. self.close()
  199. def connectionLost(self, reason):
  200. self.close()
  201. def doRead(self):
  202. self._callback(self._fd)
  203. @implementer(IWriteDescriptor)
  204. class Writer(object):
  205. def __init__(self, fd, callback):
  206. self._fd = fd
  207. self._callback = callback
  208. def logPrefix(self):
  209. return "Writer"
  210. def close(self):
  211. self._fd.close()
  212. def fileno(self):
  213. return self._fd.fileno()
  214. def connectionLost(self, reason):
  215. self.close()
  216. def doWrite(self):
  217. self._callback(self._fd)
  218. @skipIfNoTwisted
  219. class ReactorReaderWriterTest(ReactorTestCase):
  220. def _set_nonblocking(self, fd):
  221. flags = fcntl.fcntl(fd, fcntl.F_GETFL)
  222. fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
  223. def setUp(self):
  224. super(ReactorReaderWriterTest, self).setUp()
  225. r, w = os.pipe()
  226. self._set_nonblocking(r)
  227. self._set_nonblocking(w)
  228. set_close_exec(r)
  229. set_close_exec(w)
  230. self._p1 = os.fdopen(r, "rb", 0)
  231. self._p2 = os.fdopen(w, "wb", 0)
  232. def tearDown(self):
  233. super(ReactorReaderWriterTest, self).tearDown()
  234. self._p1.close()
  235. self._p2.close()
  236. def _testReadWrite(self):
  237. """
  238. In this test the writer writes an 'x' to its fd. The reader
  239. reads it, check the value and ends the test.
  240. """
  241. self.shouldWrite = True
  242. def checkReadInput(fd):
  243. self.assertEquals(fd.read(1), b'x')
  244. self._reactor.stop()
  245. def writeOnce(fd):
  246. if self.shouldWrite:
  247. self.shouldWrite = False
  248. fd.write(b'x')
  249. self._reader = Reader(self._p1, checkReadInput)
  250. self._writer = Writer(self._p2, writeOnce)
  251. self._reactor.addWriter(self._writer)
  252. # Test that adding the reader twice adds it only once to
  253. # IOLoop.
  254. self._reactor.addReader(self._reader)
  255. self._reactor.addReader(self._reader)
  256. def testReadWrite(self):
  257. self._reactor.callWhenRunning(self._testReadWrite)
  258. self._reactor.run()
  259. def _testNoWriter(self):
  260. """
  261. In this test we have no writer. Make sure the reader doesn't
  262. read anything.
  263. """
  264. def checkReadInput(fd):
  265. self.fail("Must not be called.")
  266. def stopTest():
  267. # Close the writer here since the IOLoop doesn't know
  268. # about it.
  269. self._writer.close()
  270. self._reactor.stop()
  271. self._reader = Reader(self._p1, checkReadInput)
  272. # We create a writer, but it should never be invoked.
  273. self._writer = Writer(self._p2, lambda fd: fd.write('x'))
  274. # Test that adding and removing the writer leaves us with no writer.
  275. self._reactor.addWriter(self._writer)
  276. self._reactor.removeWriter(self._writer)
  277. # Test that adding and removing the reader doesn't cause
  278. # unintended effects.
  279. self._reactor.addReader(self._reader)
  280. # Wake up after a moment and stop the test
  281. self._reactor.callLater(0.001, stopTest)
  282. def testNoWriter(self):
  283. self._reactor.callWhenRunning(self._testNoWriter)
  284. self._reactor.run()
  285. # Test various combinations of twisted and tornado http servers,
  286. # http clients, and event loop interfaces.
  287. @skipIfNoTwisted
  288. @unittest.skipIf(not have_twisted_web, 'twisted web not present')
  289. class CompatibilityTests(unittest.TestCase):
  290. def setUp(self):
  291. self.saved_signals = save_signal_handlers()
  292. self.io_loop = IOLoop()
  293. self.io_loop.make_current()
  294. self.reactor = TornadoReactor()
  295. def tearDown(self):
  296. self.reactor.disconnectAll()
  297. self.io_loop.clear_current()
  298. self.io_loop.close(all_fds=True)
  299. restore_signal_handlers(self.saved_signals)
  300. def start_twisted_server(self):
  301. class HelloResource(Resource):
  302. isLeaf = True
  303. def render_GET(self, request):
  304. return "Hello from twisted!"
  305. site = Site(HelloResource())
  306. port = self.reactor.listenTCP(0, site, interface='127.0.0.1')
  307. self.twisted_port = port.getHost().port
  308. def start_tornado_server(self):
  309. class HelloHandler(RequestHandler):
  310. def get(self):
  311. self.write("Hello from tornado!")
  312. app = Application([('/', HelloHandler)],
  313. log_function=lambda x: None)
  314. server = HTTPServer(app)
  315. sock, self.tornado_port = bind_unused_port()
  316. server.add_sockets([sock])
  317. def run_ioloop(self):
  318. self.stop_loop = self.io_loop.stop
  319. self.io_loop.start()
  320. self.reactor.fireSystemEvent('shutdown')
  321. def run_reactor(self):
  322. self.stop_loop = self.reactor.stop
  323. self.stop = self.reactor.stop
  324. self.reactor.run()
  325. def tornado_fetch(self, url, runner):
  326. responses = []
  327. client = AsyncHTTPClient()
  328. def callback(response):
  329. responses.append(response)
  330. self.stop_loop()
  331. client.fetch(url, callback=callback)
  332. runner()
  333. self.assertEqual(len(responses), 1)
  334. responses[0].rethrow()
  335. return responses[0]
  336. def twisted_fetch(self, url, runner):
  337. # http://twistedmatrix.com/documents/current/web/howto/client.html
  338. chunks = []
  339. client = Agent(self.reactor)
  340. d = client.request(b'GET', utf8(url))
  341. class Accumulator(Protocol):
  342. def __init__(self, finished):
  343. self.finished = finished
  344. def dataReceived(self, data):
  345. chunks.append(data)
  346. def connectionLost(self, reason):
  347. self.finished.callback(None)
  348. def callback(response):
  349. finished = Deferred()
  350. response.deliverBody(Accumulator(finished))
  351. return finished
  352. d.addCallback(callback)
  353. def shutdown(failure):
  354. if hasattr(self, 'stop_loop'):
  355. self.stop_loop()
  356. elif failure is not None:
  357. # loop hasn't been initialized yet; try our best to
  358. # get an error message out. (the runner() interaction
  359. # should probably be refactored).
  360. try:
  361. failure.raiseException()
  362. except:
  363. logging.error('exception before starting loop', exc_info=True)
  364. d.addBoth(shutdown)
  365. runner()
  366. self.assertTrue(chunks)
  367. return ''.join(chunks)
  368. def twisted_coroutine_fetch(self, url, runner):
  369. body = [None]
  370. @gen.coroutine
  371. def f():
  372. # This is simpler than the non-coroutine version, but it cheats
  373. # by reading the body in one blob instead of streaming it with
  374. # a Protocol.
  375. client = Agent(self.reactor)
  376. response = yield client.request(b'GET', utf8(url))
  377. with warnings.catch_warnings():
  378. # readBody has a buggy DeprecationWarning in Twisted 15.0:
  379. # https://twistedmatrix.com/trac/changeset/43379
  380. warnings.simplefilter('ignore', category=DeprecationWarning)
  381. body[0] = yield readBody(response)
  382. self.stop_loop()
  383. self.io_loop.add_callback(f)
  384. runner()
  385. return body[0]
  386. def testTwistedServerTornadoClientIOLoop(self):
  387. self.start_twisted_server()
  388. response = self.tornado_fetch(
  389. 'http://127.0.0.1:%d' % self.twisted_port, self.run_ioloop)
  390. self.assertEqual(response.body, 'Hello from twisted!')
  391. def testTwistedServerTornadoClientReactor(self):
  392. self.start_twisted_server()
  393. response = self.tornado_fetch(
  394. 'http://127.0.0.1:%d' % self.twisted_port, self.run_reactor)
  395. self.assertEqual(response.body, 'Hello from twisted!')
  396. def testTornadoServerTwistedClientIOLoop(self):
  397. self.start_tornado_server()
  398. response = self.twisted_fetch(
  399. 'http://127.0.0.1:%d' % self.tornado_port, self.run_ioloop)
  400. self.assertEqual(response, 'Hello from tornado!')
  401. def testTornadoServerTwistedClientReactor(self):
  402. self.start_tornado_server()
  403. response = self.twisted_fetch(
  404. 'http://127.0.0.1:%d' % self.tornado_port, self.run_reactor)
  405. self.assertEqual(response, 'Hello from tornado!')
  406. def testTornadoServerTwistedCoroutineClientIOLoop(self):
  407. self.start_tornado_server()
  408. response = self.twisted_coroutine_fetch(
  409. 'http://127.0.0.1:%d' % self.tornado_port, self.run_ioloop)
  410. self.assertEqual(response, 'Hello from tornado!')
  411. @skipIfNoTwisted
  412. class ConvertDeferredTest(unittest.TestCase):
  413. def test_success(self):
  414. @inlineCallbacks
  415. def fn():
  416. if False:
  417. # inlineCallbacks doesn't work with regular functions;
  418. # must have a yield even if it's unreachable.
  419. yield
  420. returnValue(42)
  421. f = gen.convert_yielded(fn())
  422. self.assertEqual(f.result(), 42)
  423. def test_failure(self):
  424. @inlineCallbacks
  425. def fn():
  426. if False:
  427. yield
  428. 1 / 0
  429. f = gen.convert_yielded(fn())
  430. with self.assertRaises(ZeroDivisionError):
  431. f.result()
  432. if have_twisted:
  433. # Import and run as much of twisted's test suite as possible.
  434. # This is unfortunately rather dependent on implementation details,
  435. # but there doesn't appear to be a clean all-in-one conformance test
  436. # suite for reactors.
  437. #
  438. # This is a list of all test suites using the ReactorBuilder
  439. # available in Twisted 11.0.0 and 11.1.0 (and a blacklist of
  440. # specific test methods to be disabled).
  441. twisted_tests = {
  442. 'twisted.internet.test.test_core.ObjectModelIntegrationTest': [],
  443. 'twisted.internet.test.test_core.SystemEventTestsBuilder': [
  444. 'test_iterate', # deliberately not supported
  445. # Fails on TwistedIOLoop and AsyncIOLoop.
  446. 'test_runAfterCrash',
  447. ],
  448. 'twisted.internet.test.test_fdset.ReactorFDSetTestsBuilder': [
  449. "test_lostFileDescriptor", # incompatible with epoll and kqueue
  450. ],
  451. 'twisted.internet.test.test_process.ProcessTestsBuilder': [
  452. # Only work as root. Twisted's "skip" functionality works
  453. # with py27+, but not unittest2 on py26.
  454. 'test_changeGID',
  455. 'test_changeUID',
  456. # This test sometimes fails with EPIPE on a call to
  457. # kqueue.control. Happens consistently for me with
  458. # trollius but not asyncio or other IOLoops.
  459. 'test_childConnectionLost',
  460. ],
  461. # Process tests appear to work on OSX 10.7, but not 10.6
  462. # 'twisted.internet.test.test_process.PTYProcessTestsBuilder': [
  463. # 'test_systemCallUninterruptedByChildExit',
  464. # ],
  465. 'twisted.internet.test.test_tcp.TCPClientTestsBuilder': [
  466. 'test_badContext', # ssl-related; see also SSLClientTestsMixin
  467. ],
  468. 'twisted.internet.test.test_tcp.TCPPortTestsBuilder': [
  469. # These use link-local addresses and cause firewall prompts on mac
  470. 'test_buildProtocolIPv6AddressScopeID',
  471. 'test_portGetHostOnIPv6ScopeID',
  472. 'test_serverGetHostOnIPv6ScopeID',
  473. 'test_serverGetPeerOnIPv6ScopeID',
  474. ],
  475. 'twisted.internet.test.test_tcp.TCPConnectionTestsBuilder': [],
  476. 'twisted.internet.test.test_tcp.WriteSequenceTests': [],
  477. 'twisted.internet.test.test_tcp.AbortConnectionTestCase': [],
  478. 'twisted.internet.test.test_threads.ThreadTestsBuilder': [],
  479. 'twisted.internet.test.test_time.TimeTestsBuilder': [],
  480. # Extra third-party dependencies (pyOpenSSL)
  481. # 'twisted.internet.test.test_tls.SSLClientTestsMixin': [],
  482. 'twisted.internet.test.test_udp.UDPServerTestsBuilder': [],
  483. 'twisted.internet.test.test_unix.UNIXTestsBuilder': [
  484. # Platform-specific. These tests would be skipped automatically
  485. # if we were running twisted's own test runner.
  486. 'test_connectToLinuxAbstractNamespace',
  487. 'test_listenOnLinuxAbstractNamespace',
  488. # These tests use twisted's sendmsg.c extension and sometimes
  489. # fail with what looks like uninitialized memory errors
  490. # (more common on pypy than cpython, but I've seen it on both)
  491. 'test_sendFileDescriptor',
  492. 'test_sendFileDescriptorTriggersPauseProducing',
  493. 'test_descriptorDeliveredBeforeBytes',
  494. 'test_avoidLeakingFileDescriptors',
  495. ],
  496. 'twisted.internet.test.test_unix.UNIXDatagramTestsBuilder': [
  497. 'test_listenOnLinuxAbstractNamespace',
  498. ],
  499. 'twisted.internet.test.test_unix.UNIXPortTestsBuilder': [],
  500. }
  501. if sys.version_info >= (3,):
  502. # In Twisted 15.2.0 on Python 3.4, the process tests will try to run
  503. # but fail, due in part to interactions between Tornado's strict
  504. # warnings-as-errors policy and Twisted's own warning handling
  505. # (it was not obvious how to configure the warnings module to
  506. # reconcile the two), and partly due to what looks like a packaging
  507. # error (process_cli.py missing). For now, just skip it.
  508. del twisted_tests['twisted.internet.test.test_process.ProcessTestsBuilder']
  509. for test_name, blacklist in twisted_tests.items():
  510. try:
  511. test_class = import_object(test_name)
  512. except (ImportError, AttributeError):
  513. continue
  514. for test_func in blacklist: # type: ignore
  515. if hasattr(test_class, test_func):
  516. # The test_func may be defined in a mixin, so clobber
  517. # it instead of delattr()
  518. setattr(test_class, test_func, lambda self: None)
  519. def make_test_subclass(test_class):
  520. class TornadoTest(test_class): # type: ignore
  521. _reactors = ["tornado.platform.twisted._TestReactor"]
  522. def setUp(self):
  523. # Twisted's tests expect to be run from a temporary
  524. # directory; they create files in their working directory
  525. # and don't always clean up after themselves.
  526. self.__curdir = os.getcwd()
  527. self.__tempdir = tempfile.mkdtemp()
  528. os.chdir(self.__tempdir)
  529. super(TornadoTest, self).setUp() # type: ignore
  530. def tearDown(self):
  531. super(TornadoTest, self).tearDown() # type: ignore
  532. os.chdir(self.__curdir)
  533. shutil.rmtree(self.__tempdir)
  534. def flushWarnings(self, *args, **kwargs):
  535. # This is a hack because Twisted and Tornado have
  536. # differing approaches to warnings in tests.
  537. # Tornado sets up a global set of warnings filters
  538. # in runtests.py, while Twisted patches the filter
  539. # list in each test. The net effect is that
  540. # Twisted's tests run with Tornado's increased
  541. # strictness (BytesWarning and ResourceWarning are
  542. # enabled) but without our filter rules to ignore those
  543. # warnings from Twisted code.
  544. filtered = []
  545. for w in super(TornadoTest, self).flushWarnings( # type: ignore
  546. *args, **kwargs):
  547. if w['category'] in (BytesWarning, ResourceWarning):
  548. continue
  549. filtered.append(w)
  550. return filtered
  551. def buildReactor(self):
  552. self.__saved_signals = save_signal_handlers()
  553. return test_class.buildReactor(self)
  554. def unbuildReactor(self, reactor):
  555. test_class.unbuildReactor(self, reactor)
  556. # Clean up file descriptors (especially epoll/kqueue
  557. # objects) eagerly instead of leaving them for the
  558. # GC. Unfortunately we can't do this in reactor.stop
  559. # since twisted expects to be able to unregister
  560. # connections in a post-shutdown hook.
  561. reactor._io_loop.close(all_fds=True)
  562. restore_signal_handlers(self.__saved_signals)
  563. TornadoTest.__name__ = test_class.__name__
  564. return TornadoTest
  565. test_subclass = make_test_subclass(test_class)
  566. globals().update(test_subclass.makeTestCaseClasses())
  567. # Since we're not using twisted's test runner, it's tricky to get
  568. # logging set up well. Most of the time it's easiest to just
  569. # leave it turned off, but while working on these tests you may want
  570. # to uncomment one of the other lines instead.
  571. log.defaultObserver.stop()
  572. # import sys; log.startLogging(sys.stderr, setStdout=0)
  573. # log.startLoggingWithObserver(log.PythonLoggingObserver().emit, setStdout=0)
  574. # import logging; logging.getLogger('twisted').setLevel(logging.WARNING)
  575. # Twisted recently introduced a new logger; disable that one too.
  576. try:
  577. from twisted.logger import globalLogBeginner # type: ignore
  578. except ImportError:
  579. pass
  580. else:
  581. globalLogBeginner.beginLoggingTo([], redirectStandardIO=False)
  582. if have_twisted:
  583. class LayeredTwistedIOLoop(TwistedIOLoop):
  584. """Layers a TwistedIOLoop on top of a TornadoReactor on a PollIOLoop.
  585. This is of course silly, but is useful for testing purposes to make
  586. sure we're implementing both sides of the various interfaces
  587. correctly. In some tests another TornadoReactor is layered on top
  588. of the whole stack.
  589. """
  590. def initialize(self, **kwargs):
  591. self.real_io_loop = PollIOLoop(make_current=False) # type: ignore
  592. reactor = self.real_io_loop.run_sync(gen.coroutine(TornadoReactor))
  593. super(LayeredTwistedIOLoop, self).initialize(reactor=reactor, **kwargs)
  594. self.add_callback(self.make_current)
  595. def close(self, all_fds=False):
  596. super(LayeredTwistedIOLoop, self).close(all_fds=all_fds)
  597. # HACK: This is the same thing that test_class.unbuildReactor does.
  598. for reader in self.reactor._internalReaders:
  599. self.reactor.removeReader(reader)
  600. reader.connectionLost(None)
  601. self.real_io_loop.close(all_fds=all_fds)
  602. def stop(self):
  603. # One of twisted's tests fails if I don't delay crash()
  604. # until the reactor has started, but if I move this to
  605. # TwistedIOLoop then the tests fail when I'm *not* running
  606. # tornado-on-twisted-on-tornado. I'm clearly missing something
  607. # about the startup/crash semantics, but since stop and crash
  608. # are really only used in tests it doesn't really matter.
  609. def f():
  610. self.reactor.crash()
  611. # Become current again on restart. This is needed to
  612. # override real_io_loop's claim to being the current loop.
  613. self.add_callback(self.make_current)
  614. self.reactor.callWhenRunning(f)
  615. if __name__ == "__main__":
  616. unittest.main()