From 3eed81c1eb13f012d67f8fa3a783f6af5f76275d Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 23 May 2017 10:59:55 +0800 Subject: [PATCH 1/2] Add a simple integration test for Gunicorn worker --- requirements-dev.txt | 1 + tests/test_worker.py | 22 ++++++++++++++++++++++ tox.ini | 1 + 3 files changed, 24 insertions(+) create mode 100644 tests/test_worker.py diff --git a/requirements-dev.txt b/requirements-dev.txt index 163df025..2efa853e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -9,3 +9,4 @@ pytest tox ujson uvloop +gunicorn diff --git a/tests/test_worker.py b/tests/test_worker.py new file mode 100644 index 00000000..73838f68 --- /dev/null +++ b/tests/test_worker.py @@ -0,0 +1,22 @@ +import time +import json +import shlex +import subprocess +import urllib.request + +import pytest + + +@pytest.fixture(scope='module') +def gunicorn_worker(): + command = 'gunicorn --bind 127.0.0.1:1337 --worker-class sanic.worker.GunicornWorker examples.simple_server:app' + worker = subprocess.Popen(shlex.split(command)) + time.sleep(1) + yield + worker.kill() + + +def test_gunicorn_worker(gunicorn_worker): + with urllib.request.urlopen('http://localhost:1337/') as f: + res = json.loads(f.read(100).decode()) + assert res['test'] diff --git a/tox.ini b/tox.ini index b391c06e..3092e875 100644 --- a/tox.ini +++ b/tox.ini @@ -13,6 +13,7 @@ deps = aiohttp==1.3.5 chardet<=2.3.0 beautifulsoup4 + gunicorn commands = pytest tests {posargs} coverage erase From 6bdc0d2e5ee7f2453a93e5016a0b3f7839373498 Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 23 May 2017 11:28:12 +0800 Subject: [PATCH 2/2] Fix Gunicorn worker --- sanic/app.py | 9 ++++++--- sanic/worker.py | 2 -- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 5e8276c3..3b278361 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -543,6 +543,9 @@ class Sanic: :param protocol: Subclass of asyncio protocol class :return: Nothing """ + if sock is None: + host, port = host or "127.0.0.1", port or 8000 + if log_config: logging.config.dictConfig(log_config) if protocol is None: @@ -590,6 +593,9 @@ class Sanic: NOTE: This does not support multiprocessing and is not the preferred way to run a Sanic application. """ + if sock is None: + host, port = host or "127.0.0.1", port or 8000 + if log_config: logging.config.dictConfig(log_config) if protocol is None: @@ -635,9 +641,6 @@ class Sanic: protocol=HttpProtocol, backlog=100, stop_event=None, register_sys_signals=True, run_async=False, has_log=True): """Helper function used by `run` and `create_server`.""" - if sock is None: - host, port = host or "127.0.0.1", port or 8000 - if isinstance(ssl, dict): # try common aliaseses cert = ssl.get('cert') or ssl.get('certificate') diff --git a/sanic/worker.py b/sanic/worker.py index 30ad91b0..d15fda41 100644 --- a/sanic/worker.py +++ b/sanic/worker.py @@ -48,8 +48,6 @@ class GunicornWorker(base.Worker): protocol = (WebSocketProtocol if self.app.callable.websocket_enabled else HttpProtocol) self._server_settings = self.app.callable._helper( - host=None, - port=None, loop=self.loop, debug=is_debug, protocol=protocol,