tests: Add comprehensive tests for Runner class.

This commit is contained in:
Dave Shawley 2016-01-28 07:49:20 -05:00
parent 03e85b440c
commit 53b09b2e32

View file

@ -353,3 +353,100 @@ class CallbackTests(MockHelper, unittest.TestCase):
another_callback.assert_not_called()
self.shutdown_callback.assert_called_once_with(self.application)
sys_exit.assert_called_once_with(70)
class RunnerTests(MockHelper, unittest.TestCase):
def setUp(self):
super(RunnerTests, self).setUp()
self.application = mock.Mock()
self.application.settings = {}
self.application.runner_callbacks = {}
self.io_loop = mock.Mock()
self.io_loop._callbacks = []
self.io_loop._timeouts = []
self.io_loop.time = time.time
ioloop_module = self.start_mock('sprockets.http.runner.ioloop')
ioloop_module.IOLoop.instance.return_value = self.io_loop
self.http_server = mock.Mock()
httpserver_module = self.start_mock('sprockets.http.runner.httpserver')
httpserver_module.HTTPServer.return_value = self.http_server
def test_that_run_starts_ioloop(self):
runner = sprockets.http.runner.Runner(self.application)
runner.run(8000)
self.io_loop.start.assert_called_once_with()
def test_that_production_run_starts_in_multiprocess_mode(self):
runner = sprockets.http.runner.Runner(self.application)
runner.run(8000)
self.http_server.bind.assert_called_once_with(8000)
self.http_server.start.assert_called_once_with(0)
def test_that_debug_run_starts_in_singleprocess_mode(self):
self.application.settings['debug'] = True
runner = sprockets.http.runner.Runner(self.application)
runner.run(8000)
self.http_server.listen.assert_called_once_with(8000)
self.http_server.start.assert_not_called()
def test_that_initializer_creates_runner_callbacks_dict(self):
application = web.Application()
_ = sprockets.http.runner.Runner(application)
self.assertEqual(application.runner_callbacks['before_run'], [])
self.assertEqual(application.runner_callbacks['shutdown'], [])
def test_that_signal_handler_invokes_shutdown(self):
with mock.patch('sprockets.http.runner.signal') as signal_module:
runner = sprockets.http.runner.Runner(self.application)
runner.run(8000)
signal_module.signal.assert_any_call(signal_module.SIGINT,
runner._on_signal)
signal_module.signal.assert_any_call(signal_module.SIGTERM,
runner._on_signal)
runner._on_signal(signal_module.SIGINT, mock.Mock())
self.io_loop.add_callback_from_signal.assert_called_once_with(
runner._shutdown)
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)
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 add_timeout(_, callback):
time.sleep(0.05)
callback()
self.io_loop.add_timeout = mock.Mock(side_effect=add_timeout)
self.io_loop._timeouts = [mock.Mock()]
runner = sprockets.http.runner.Runner(self.application)
runner.shutdown_limit = 0.25
runner.run(8000)
runner._shutdown()
self.io_loop.stop.assert_called_once_with()
self.assertNotEqual(self.io_loop._timeouts, [])