Skip to content

Commit

Permalink
Merge 073aca6 into 2b8b742
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisuhlendorf committed May 10, 2018
2 parents 2b8b742 + 073aca6 commit fff29a1
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 71 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tornado >=4.2.1,<4.5.0
tornado >=4.2.1,<=5.0
schematics == 1.1.1
scales == 1.0.9
6 changes: 3 additions & 3 deletions supercell/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ def main(self, with_signals=True):

if with_signals:
def sig_handler(sig, frame):
IOLoop.instance().add_callback(self.shutdown) # noqa
IOLoop.current().add_callback(self.shutdown) # noqa
signal.signal(signal.SIGTERM, sig_handler)
signal.signal(signal.SIGINT, sig_handler)

self.slog.info('Starting supercell')
IOLoop.instance().start()
IOLoop.current().start()

def shutdown(self):
"""Gaceful shutdown of the server.
Expand All @@ -127,7 +127,7 @@ def shutdown(self):
current requests are allowed to finish. After this period the `IOLoop`
is stopped.
"""
io_loop = IOLoop.instance()
io_loop = IOLoop.current()
self.slog.info('Stopping HTTP server')
self.server.stop()

Expand Down
3 changes: 2 additions & 1 deletion supercell/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def set_commandline(self, monkeypatch):
monkeypatch.setattr(sys, 'argv', ['pytest'] + self.ARGV)

def get_new_ioloop(self):
return IOLoop.instance()
return IOLoop.current\
()

def get_app(self):
service = self.SERVICE()
Expand Down
4 changes: 0 additions & 4 deletions test/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from schematics.types import StringType

from tornado import gen
from tornado.ioloop import IOLoop
from tornado.testing import AsyncHTTPTestCase

import supercell.api as s
Expand Down Expand Up @@ -94,9 +93,6 @@ def get(self, *args, **kwargs):

class TestCacheDecorator(AsyncHTTPTestCase):

def get_new_ioloop(self):
return IOLoop.instance()

def get_app(self):
env = Environment()
env.add_handler(r'/', MyHandler,
Expand Down
15 changes: 7 additions & 8 deletions test/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
else:
from unittest2 import TestCase

from tornado import httputil
from tornado.web import Application, RequestHandler

from supercell.environment import Environment
Expand All @@ -35,7 +36,7 @@ def test_simple_app_creation(self):
env = Environment()
app = env.get_application()
self.assertIsInstance(app, Application)
self.assertEqual(len(app.handlers), 2)
self.assertEqual(len(app.default_router.rules), 3)

def test_config_file_paths(self):
env = Environment()
Expand All @@ -54,13 +55,11 @@ def get(self):
self.assertEqual(len(env._handlers), 1)

app = env.get_application()
self.assertEqual(len(app.handlers), 3)
(host_pattern, [spec]) = [h for h in app.handlers
if not h[1][0].regex.pattern.startswith(
'/_system')][0]
self.assertEqual(host_pattern.pattern, '.*$')
self.assertEqual(spec.regex.pattern, '/test$')
self.assertEqual(spec.handler_class, MyHandler)

request = httputil.HTTPServerRequest(uri='/test')
handler_delegate = app.default_router.find_handler(request)
self.assertIsNotNone(handler_delegate)
self.assertEqual(handler_delegate.handler_class, MyHandler)

def test_managed_object_access(self):
env = Environment()
Expand Down
7 changes: 0 additions & 7 deletions test/test_healthchecks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import json

from tornado.ioloop import IOLoop
from tornado.testing import AsyncHTTPTestCase

from supercell.health import SystemHealthCheck
Expand All @@ -30,9 +29,6 @@

class TestBasicHealthChecks(AsyncHTTPTestCase):

def get_new_ioloop(self):
return IOLoop.instance()

def get_app(self):
env = Environment()
env.add_handler('/_system/check', SystemHealthCheck)
Expand Down Expand Up @@ -63,9 +59,6 @@ def get(self):

class TestCustomHealthCheck(AsyncHTTPTestCase):

def get_new_ioloop(self):
return IOLoop.instance()

def get_app(self):
env = Environment()
env.add_health_check('test', SimpleHealthCheckExample)
Expand Down
4 changes: 0 additions & 4 deletions test/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import json

from tornado.ioloop import IOLoop
from tornado.testing import AsyncHTTPTestCase

from schematics.models import Model
Expand Down Expand Up @@ -126,9 +125,6 @@ def get_app(self):
env.add_handler('/otherresult', MyHandlerReturningOtherStuff)
return env.get_application()

def get_new_ioloop(self):
return IOLoop.instance()

def test_that_header_exists(self):
response = self.fetch('/test')

Expand Down
4 changes: 0 additions & 4 deletions test/test_queryparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from schematics.types import StringType
from schematics.types import IntType

from tornado.ioloop import IOLoop
from tornado.testing import AsyncHTTPTestCase

import supercell.api as s
Expand Down Expand Up @@ -78,9 +77,6 @@ def get_app(self):
env.tornado_settings['debug'] = True
return env.get_application()

def get_new_ioloop(self):
return IOLoop.instance()

def test_simple_params(self):
response = self.fetch('/test?docid=1&message=A%20test')

Expand Down
16 changes: 0 additions & 16 deletions test/test_requesthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from schematics.types.compound import ListType
from schematics.types.compound import ModelType

from tornado.ioloop import IOLoop
from tornado.testing import AsyncHTTPTestCase

import supercell.api as s
Expand Down Expand Up @@ -99,9 +98,6 @@ def get_app(self):
env.add_handler('/test_echo', MyEchoHandler)
return env.get_application()

def get_new_ioloop(self):
return IOLoop.instance()

def test_simple_handler(self):
response = self.fetch('/test', headers={'Accept':
s.MediaType.ApplicationJson})
Expand Down Expand Up @@ -187,9 +183,6 @@ def get_app(self):
env.add_handler('/testencoding/(.*)', EncodingTestingHandler)
return env.get_application()

def get_new_ioloop(self):
return IOLoop.instance()

def test_latinone_handler(self):
response = self.fetch('/testencoding/alfredo-p%e9rez-rubalcaba')
self.assertEqual(200, response.code)
Expand Down Expand Up @@ -232,9 +225,6 @@ def get(self, *args, **kwargs):
'html_test_template')
return env.get_application()

def get_new_ioloop(self):
return IOLoop.instance()

def test_simple_html(self):
response = self.fetch('/test_html/')
self.assertEqual(200, response.code)
Expand Down Expand Up @@ -265,9 +255,6 @@ def get(self, *args, **kwargs):
'html_test_template')
return env.get_application()

def get_new_ioloop(self):
return IOLoop.instance()

def test_simple_html(self):
response = self.fetch('/test_html/')
self.assertEqual(500, response.code)
Expand Down Expand Up @@ -318,9 +305,6 @@ def get(self, *args, **kwargs):
env.add_handler('/test_partial_complex', MyHandlerWithPartialComplex)
return env.get_application()

def get_new_ioloop(self):
return IOLoop.instance()

def test_provide_partial_model_with_partial_false(self):
response = self.fetch(
'/test_no_partial',
Expand Down
4 changes: 0 additions & 4 deletions test/test_returning_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import pytest
import json

from tornado.ioloop import IOLoop
from tornado.testing import AsyncHTTPTestCase
from tornado.web import HTTPError

Expand Down Expand Up @@ -109,9 +108,6 @@ class ApplicationIntegrationTest(AsyncHTTPTestCase):
def empty_commandline(self, monkeypatch):
monkeypatch.setattr(sys, 'argv', [])

def get_new_ioloop(self):
return IOLoop.instance()

def get_app(self):
service = MyService()
service.initialize_logging()
Expand Down
4 changes: 0 additions & 4 deletions test/test_returning_non_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import pytest

from tornado.ioloop import IOLoop
from tornado.testing import AsyncHTTPTestCase

from supercell.api import async
Expand Down Expand Up @@ -52,9 +51,6 @@ class ApplicationIntegrationTest(AsyncHTTPTestCase):
def empty_commandline(self, monkeypatch):
monkeypatch.setattr(sys, 'argv', [])

def get_new_ioloop(self):
return IOLoop.instance()

def get_app(self):
service = MyService()
service.initialize_logging()
Expand Down
32 changes: 21 additions & 11 deletions test/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from schematics.models import Model
from schematics.types import StringType
import tornado.options
from supercell.testing import AsyncHTTPTestCase
from tornado.testing import AsyncHTTPTestCase

import supercell.api as s
from supercell.environment import Environment
Expand Down Expand Up @@ -110,17 +110,18 @@ def test_logging_initialization(self):

service.initialize_logging()

@mock.patch('tornado.ioloop.IOLoop.instance')
@mock.patch('tornado.ioloop.IOLoop.current')
def test_main_method(self, ioloop_instance_mock):
service = MyService()
service.main()

expected = [mock.call(), mock.call().add_handler(mock.ANY, mock.ANY,
mock.ANY),
mock.call(), mock.call().start()]
assert expected == ioloop_instance_mock.mock_calls
ioloop_instance_mock.assert_called()
ioloop_instance_mock().start.assert_called()
ioloop_instance_mock().add_handler.assert_called()
service.shutdown()


@mock.patch('tornado.ioloop.IOLoop.instance')
@mock.patch('tornado.ioloop.IOLoop.current')
@mock.patch('socket.fromfd')
def test_startup_with_socket_fd(self, socket_fromfd_mock,
ioloop_instance_mock):
Expand All @@ -137,8 +138,10 @@ def test_startup_with_socket_fd(self, socket_fromfd_mock,

assert (mock.call(123, socket.AF_INET, socket.SOCK_STREAM)
in socket_fromfd_mock.mock_calls)
service.shutdown()

@mock.patch('tornado.ioloop.IOLoop.instance')

@mock.patch('tornado.ioloop.IOLoop.current')
def test_graceful_shutdown_pending_callbacks(self, ioloop_instance_mock):
service = MyService()
service.main()
Expand All @@ -163,7 +166,7 @@ def test_graceful_shutdown_pending_callbacks(self, ioloop_instance_mock):

assert expected == ioloop_instance_mock.mock_calls

@mock.patch('tornado.ioloop.IOLoop.instance')
@mock.patch('tornado.ioloop.IOLoop.current')
def test_graceful_final_shutdown(self, ioloop_instance_mock):
service = MyService()
service.main()
Expand All @@ -181,12 +184,19 @@ def test_graceful_final_shutdown(self, ioloop_instance_mock):
assert expected == ioloop_instance_mock.mock_calls

service.config.max_grace_seconds = 3
service.shutdown()


class ApplicationIntegrationTest(AsyncHTTPTestCase):

ARGV = []
SERVICE = MyService
@pytest.fixture(autouse=True)
def set_commandline(self, monkeypatch):
monkeypatch.setattr(sys, 'argv', ['pytest'])

def get_app(self):
service = MyService()
service.initialize_logging()
return service.get_app()

def test_simple_get(self):
response = self.fetch('/test', headers={'Accept':
Expand Down
4 changes: 0 additions & 4 deletions test/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from schematics.types import StringType

from tornado import gen
from tornado.ioloop import IOLoop
from tornado.testing import AsyncHTTPTestCase

import supercell.api as s
Expand Down Expand Up @@ -67,9 +66,6 @@ def do_something(self):

class TestSupercellStats(AsyncHTTPTestCase):

def get_new_ioloop(self):
return IOLoop.instance()

def get_app(self):
env = Environment()
env.add_handler('/teststats', MyHandler)
Expand Down

0 comments on commit fff29a1

Please sign in to comment.