From 6d1d4ade19a6b5aaf572dbba68540b2976a43a53 Mon Sep 17 00:00:00 2001 From: Diogo Dutra Date: Thu, 29 Dec 2016 19:35:41 -0200 Subject: [PATCH] add a flag to skip SIGINT and SIGTERM signals registration --- sanic/sanic.py | 5 +++-- sanic/server.py | 8 +++++--- tests/test_sanic.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 tests/test_sanic.py diff --git a/sanic/sanic.py b/sanic/sanic.py index ecf5b652..6c9eb0fd 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -239,7 +239,7 @@ class Sanic: def run(self, host="127.0.0.1", port=8000, debug=False, before_start=None, after_start=None, before_stop=None, after_stop=None, sock=None, - workers=1, loop=None): + workers=1, loop=None, register_sys_signals=True): """ Runs the HTTP Server and listens until keyboard interrupt or term signal. On termination, drains connections before closing. @@ -273,7 +273,8 @@ class Sanic: 'error_handler': self.error_handler, 'request_timeout': self.config.REQUEST_TIMEOUT, 'request_max_size': self.config.REQUEST_MAX_SIZE, - 'loop': loop + 'loop': loop, + 'register_sys_signals': register_sys_signals } # -------------------------------------------- # diff --git a/sanic/server.py b/sanic/server.py index 11756005..17f5965e 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -226,7 +226,8 @@ def trigger_events(events, loop): def serve(host, port, request_handler, error_handler, before_start=None, after_start=None, before_stop=None, after_stop=None, debug=False, request_timeout=60, sock=None, - request_max_size=None, reuse_port=False, loop=None): + request_max_size=None, reuse_port=False, loop=None, + register_sys_signals=True): """ Starts asynchronous HTTP Server on an individual process. :param host: Address to host on @@ -286,8 +287,9 @@ def serve(host, port, request_handler, error_handler, before_start=None, trigger_events(after_start, loop) # Register signals for graceful termination - for _signal in (SIGINT, SIGTERM): - loop.add_signal_handler(_signal, loop.stop) + if register_sys_signals: + for _signal in (SIGINT, SIGTERM): + loop.add_signal_handler(_signal, loop.stop) try: loop.run_forever() diff --git a/tests/test_sanic.py b/tests/test_sanic.py new file mode 100644 index 00000000..589253f7 --- /dev/null +++ b/tests/test_sanic.py @@ -0,0 +1,41 @@ +from sanic import Sanic +from sanic.response import HTTPResponse +from sanic.utils import HOST, PORT +from unittest.mock import MagicMock +import pytest +import asyncio + + +async def stop(app): + await asyncio.sleep(0.2) + app.stop() + + +def test_register_system_signals(): + """Test if sanic register system signals""" + app = Sanic('test_register_system_signals') + + @app.route('/hello') + async def hello_route(request): + return HTTPResponse() + + loop = asyncio.new_event_loop() + loop.add_signal_handler = MagicMock() + asyncio.ensure_future(stop(app), loop=loop) + app.run(HOST, PORT, loop=loop) + assert loop.add_signal_handler.called == True + + +def test_dont_register_system_signals(): + """Test if sanic don't register system signals""" + app = Sanic('test_register_system_signals') + + @app.route('/hello') + async def hello_route(request): + return HTTPResponse() + + loop = asyncio.new_event_loop() + loop.add_signal_handler = MagicMock() + asyncio.ensure_future(stop(app), loop=loop) + app.run(HOST, PORT, loop=loop, register_sys_signals=False) + assert loop.add_signal_handler.called == False