Support only tornado>=5.0

- Drop support for all versions of tornado prior to 5.0. This negates
the need for version-aware conditionals that previous existed.
- Update tox to test Tornado 5.0 and the unpinned latest version.
This commit is contained in:
Andrew Rabert 2018-11-26 16:54:34 -05:00 committed by Andrew Rabert
parent 8dfb33d6cc
commit 234fb6d479
5 changed files with 11 additions and 68 deletions

View file

@ -1 +1 @@
tornado>=3.1,<6 tornado>=5,<6

View file

@ -1,12 +1,8 @@
import asyncio
import logging import logging
import sys import sys
from tornado import concurrent, web from tornado import concurrent, web
import tornado
ASYNCIO_TORNADO = tornado.version_info[0] >= 5
if ASYNCIO_TORNADO:
import asyncio
class _ShutdownHandler(object): class _ShutdownHandler(object):
@ -45,18 +41,12 @@ class _ShutdownHandler(object):
def _maybe_stop(self): def _maybe_stop(self):
now = self.io_loop.time() now = self.io_loop.time()
if now < self.__deadline and self._ioloop_has_tasks(): if now < self.__deadline and asyncio.Task.all_tasks():
self.io_loop.add_timeout(now + 1, self._maybe_stop) self.io_loop.add_timeout(now + 1, self._maybe_stop)
else: else:
self.io_loop.stop() self.io_loop.stop()
self.logger.info('stopped IOLoop') self.logger.info('stopped IOLoop')
def _ioloop_has_tasks(self):
if ASYNCIO_TORNADO:
return bool(asyncio.Task.all_tasks())
else:
return bool(self.io_loop._callbacks or self.io_loop._timeouts)
class CallbackManager(object): class CallbackManager(object):
""" """
@ -131,8 +121,8 @@ class CallbackManager(object):
try: try:
maybe_future = callback(self.tornado_application) maybe_future = callback(self.tornado_application)
if ASYNCIO_TORNADO and asyncio.iscoroutine(maybe_future): if asyncio.iscoroutine(maybe_future):
maybe_future = asyncio.create_task(maybe_future) maybe_future = asyncio.create_task(maybe_future)
if concurrent.is_future(maybe_future): if concurrent.is_future(maybe_future):
shutdown.add_future(maybe_future) shutdown.add_future(maybe_future)

View file

@ -12,7 +12,6 @@ import signal
import sys import sys
from tornado import httpserver, ioloop from tornado import httpserver, ioloop
import tornado
import sprockets.http.app import sprockets.http.app
@ -91,12 +90,7 @@ class Runner(object):
self.server.listen(port_number) self.server.listen(port_number)
else: else:
self.logger.info('starting processes on port %d', port_number) self.logger.info('starting processes on port %d', port_number)
if tornado.version_info >= (4, 4): self.server.bind(port_number, reuse_port=True)
self.server.bind(port_number, reuse_port=True)
else:
self.logger.warning('port reuse disabled, please upgrade to'
'at least Tornado 4.4')
self.server.bind(port_number)
self.server.start(number_of_procs) self.server.start(number_of_procs)
def stop_server(self): def stop_server(self):

View file

@ -15,7 +15,6 @@ except ImportError:
open_name = '__builtin__.open' open_name = '__builtin__.open'
from tornado import concurrent, httpserver, httputil, ioloop, testing, web from tornado import concurrent, httpserver, httputil, ioloop, testing, web
import tornado
import sprockets.http.mixins import sprockets.http.mixins
import sprockets.http.runner import sprockets.http.runner
@ -410,8 +409,6 @@ class RunnerTests(MockHelper, unittest.TestCase):
self.http_server.start.assert_called_once_with(0) self.http_server.start.assert_called_once_with(0)
@unittest.skipUnless(tornado.version_info >= (4, 4),
'port reuse requries newer tornado')
def test_that_production_enables_reuse_port(self): def test_that_production_enables_reuse_port(self):
runner = sprockets.http.runner.Runner(self.application) runner = sprockets.http.runner.Runner(self.application)
runner.run(8000) runner.run(8000)
@ -448,34 +445,6 @@ class RunnerTests(MockHelper, unittest.TestCase):
self.io_loop.add_callback_from_signal.assert_called_once_with( self.io_loop.add_callback_from_signal.assert_called_once_with(
runner._shutdown) runner._shutdown)
@unittest.skipUnless(tornado.version_info < (5,), '')
def test_that_shutdown_waits_for_callbacks(self):
def add_timeout(_, callback):
self.io_loop._callbacks.pop()
callback()
self.io_loop.add_timeout = mock.Mock(side_effect=add_timeout)
self.io_loop._callbacks = [mock.Mock(), mock.Mock()]
runner = sprockets.http.runner.Runner(self.application)
runner.run(8000)
runner._shutdown()
self.io_loop.stop.assert_called_once_with()
self.assertEqual(self.io_loop.add_timeout.call_count, 2)
@unittest.skipUnless(tornado.version_info < (5,), '')
def test_that_shutdown_waits_for_timeouts(self):
def add_timeout(_, callback):
self.io_loop._timeouts.pop()
callback()
self.io_loop.add_timeout = mock.Mock(side_effect=add_timeout)
self.io_loop._timeouts = [mock.Mock(), mock.Mock()]
runner = sprockets.http.runner.Runner(self.application)
runner.run(8000)
runner._shutdown()
self.io_loop.stop.assert_called_once_with()
self.assertEqual(self.io_loop.add_timeout.call_count, 2)
def test_that_shutdown_stops_after_timelimit(self): def test_that_shutdown_stops_after_timelimit(self):
def add_timeout(_, callback): def add_timeout(_, callback):
time.sleep(0.1) time.sleep(0.1)

20
tox.ini
View file

@ -1,5 +1,5 @@
[tox] [tox]
envlist = py35,py36,py37,tornado42,tornado44,tornado45 envlist = py35,py36,py37,tornado,tornado50
indexserver = indexserver =
default = https://pypi.python.org/simple default = https://pypi.python.org/simple
toxworkdir = build/tox toxworkdir = build/tox
@ -13,22 +13,12 @@ commands =
deps = deps =
-rrequires/testing.txt -rrequires/testing.txt
[testenv:tornado42] [testenv:tornado]
commands = commands =
{envbindir}/pip install tornado>=4.2,<4.3 {envbindir}/pip install tornado
{[testenv]commands} {[testenv]commands}
[testenv:tornado43] [testenv:tornado50]
commands = commands =
{envbindir}/pip install tornado>=4.3,<4.4 {envbindir}/pip install tornado=5.0
{[testenv]commands}
[testenv:tornado44]
commands =
{envbindir}/pip install tornado>=4.4,<4.5
{[testenv]commands}
[testenv:tornado45]
commands =
{envbindir}/pip install tornado>=4.5,<4.6
{[testenv]commands} {[testenv]commands}