def test_that_production_enables_reuse_port(self): runner = sprockets.http.runner.Runner(self.application) runner.run(8000) self.assertTrue(self.http_server.bind.called) args, kwargs = self.http_server.bind.call_args_list[0] self.assertEqual(args, (8000, )) self.assertEqual(kwargs['reuse_port'], True)
def test_that_production_run_starts_in_multiprocess_mode(self): runner = sprockets.http.runner.Runner(self.application) runner.run(8000) self.assertTrue(self.http_server.bind.called) args, kwargs = self.http_server.bind.call_args_list[0] self.assertEqual(args, (8000, )) self.http_server.start.assert_called_once_with(0)
def test_that_exceptions_from_shutdown_callbacks_are_ignored(self): another_callback = mock.Mock() self.application.runner_callbacks['shutdown'].append(another_callback) self.shutdown_callback.side_effect = Exception runner = sprockets.http.runner.Runner(self.application) runner.run(8080) runner._shutdown() self.shutdown_callback.assert_called_once_with(self.application) another_callback.assert_called_once_with(self.application)
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_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_stops_after_timelimit(self): def add_timeout(_, callback): time.sleep(0.1) 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, [])
def test_that_on_start_callbacks_are_invoked(self): future = concurrent.Future() def on_started(*args, **kwargs): with mock.patch('sprockets.http.runner.Runner.stop_server'): runner._shutdown() future.set_result(True) application = web.Application() with mock.patch('sprockets.http.runner.Runner.start_server'): runner = sprockets.http.runner.Runner(application, on_start=[on_started]) runner.run(8000) self.assertTrue(future.result())
def test_that_exceptions_from_before_run_callbacks_are_terminal(self): another_callback = mock.Mock() self.application.runner_callbacks['before_run'].append( another_callback) self.before_run_callback.side_effect = Exception sys_exit = mock.Mock() sys_exit.side_effect = SystemExit with mock.patch('sprockets.http.runner.sys') as sys_module: sys_module.exit = sys_exit with self.assertRaises(SystemExit): runner = sprockets.http.runner.Runner(self.application) runner.run(8080) self.before_run_callback.assert_called_once_with(self.application, self.io_loop) another_callback.assert_not_called() self.shutdown_callback.assert_called_once_with(self.application) sys_exit.assert_called_once_with(70)
def test_that_exceptions_from_before_run_callbacks_are_terminal(self): another_callback = mock.Mock() self.application.runner_callbacks['before_run'].append( another_callback) self.before_run_callback.side_effect = Exception sys_exit = mock.Mock() sys_exit.side_effect = SystemExit with mock.patch('sprockets.http.runner.sys') as sys_module: sys_module.exit = sys_exit with self.assertRaises(SystemExit): runner = sprockets.http.runner.Runner(self.application) runner.run(8080) self.before_run_callback.assert_called_once_with( self.application, self.io_loop) another_callback.assert_not_called() self.shutdown_callback.assert_called_once_with(self.application) sys_exit.assert_called_once_with(70)
def test_that_shutdown_futures_are_waited_on(self): future = concurrent.Future() def on_started(*args, **kwargs): with mock.patch('sprockets.http.runner.Runner.stop_server'): runner._shutdown() def on_shutdown(*args, **kwargs): def shutdown_complete(): future.set_result(True) ioloop.IOLoop.current().add_timeout(1, shutdown_complete) return future application = web.Application() with mock.patch('sprockets.http.runner.Runner.start_server'): runner = sprockets.http.runner.Runner(application, on_start=[on_started], shutdown=[on_shutdown]) runner.run(8000) self.assertTrue(future.result())
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_shutdown_callback_invoked(self): runner = sprockets.http.runner.Runner(self.application) runner.run(8080) runner._shutdown() self.shutdown_callback.assert_called_once_with(self.application)
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_before_run_callback_invoked(self): runner = sprockets.http.runner.Runner(self.application) runner.run(8080) self.before_run_callback.assert_called_once_with(self.application, self.io_loop)
def test_that_http_server_settings_are_used(self): runner = sprockets.http.runner.Runner(self.application) runner.run(8000) self.httpserver_module.HTTPServer.assert_called_once_with( self.application, **self.application.settings)
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_before_run_callback_invoked(self): runner = sprockets.http.runner.Runner(self.application) runner.run(8080) self.before_run_callback.assert_called_once_with( self.application, self.io_loop)