From f99a723627f2b3bf47e70932cf672c88d7a2f169 Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Wed, 2 Aug 2017 09:05:33 -0700 Subject: [PATCH 1/3] fixed small doc issue --- .gitignore | 1 + docs/sanic/testing.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 73e923d3..4a834a7a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ settings.py docs/_build/ docs/_api/ build/* +.DS_Store diff --git a/docs/sanic/testing.md b/docs/sanic/testing.md index b8427a00..0aca9184 100644 --- a/docs/sanic/testing.md +++ b/docs/sanic/testing.md @@ -59,7 +59,7 @@ the available arguments to aiohttp can be found [in the documentation for ClientSession](https://aiohttp.readthedocs.io/en/stable/client_reference.html#client-session). -# pytest-sanic +## pytest-sanic [pytest-sanic](https://github.com/yunstanford/pytest-sanic) is a pytest plugin, it helps you to test your code asynchronously. Just write tests like, From d5d1d3b45a449b8b84aea0478712475bfb7ba26e Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Tue, 8 Aug 2017 21:58:10 -0700 Subject: [PATCH 2/3] add trigger before_start events in create_server --- sanic/app.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sanic/app.py b/sanic/app.py index f0ccad86..7587c0c2 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -633,14 +633,28 @@ class Sanic: warnings.simplefilter('default') warnings.warn("stop_event will be removed from future versions.", DeprecationWarning) + server_settings = self._helper( host=host, port=port, debug=debug, ssl=ssl, sock=sock, loop=get_event_loop(), protocol=protocol, backlog=backlog, run_async=True, has_log=log_config is not None) + # Trigger before_start events + await self.trigger_events(server_settings.get('before_start', []), server_settings.get('loop')) + return await serve(**server_settings) + async def trigger_events(self, events, loop): + """Trigger events (functions or async) + :param events: one or more sync or async functions to execute + :param loop: event loop + """ + for event in events: + result = event(loop) + if isawaitable(result): + await result + async def _run_request_middleware(self, request): # The if improves speed. I don't know why if self.request_middleware: From 80f27b1db9ae7c3eff4201bdc9b4b2896b076626 Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Tue, 8 Aug 2017 22:21:40 -0700 Subject: [PATCH 3/3] add unit tests and make flake8 happy --- sanic/app.py | 5 ++++- tests/test_server_events.py | 17 +++++++++++++++++ tox.ini | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/sanic/app.py b/sanic/app.py index 7587c0c2..c0c850e8 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -641,7 +641,10 @@ class Sanic: has_log=log_config is not None) # Trigger before_start events - await self.trigger_events(server_settings.get('before_start', []), server_settings.get('loop')) + await self.trigger_events( + server_settings.get('before_start', []), + server_settings.get('loop') + ) return await serve(**server_settings) diff --git a/tests/test_server_events.py b/tests/test_server_events.py index 0dcaba1c..d78f0aed 100644 --- a/tests/test_server_events.py +++ b/tests/test_server_events.py @@ -59,3 +59,20 @@ def test_all_listeners(): start_stop_app(random_name_app) for listener_name in AVAILABLE_LISTENERS: assert random_name_app.name + listener_name == output.pop() + + +async def test_trigger_before_events_create_server(): + + class MySanicDb: + pass + + app = Sanic("test_sanic_app") + + @app.listener('before_server_start') + async def init_db(app, loop): + app.db = MySanicDb() + + await app.create_server() + + assert hasattr(app, "db") + assert isinstance(app.db, MySanicDb) diff --git a/tox.ini b/tox.ini index de1dc2d5..dcebb050 100644 --- a/tox.ini +++ b/tox.ini @@ -10,6 +10,7 @@ deps = coverage pytest pytest-cov + pytest-sanic pytest-sugar aiohttp==1.3.5 chardet<=2.3.0