Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add triggers events when async create_server #885

Merged
merged 4 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ settings.py
docs/_build/
docs/_api/
build/*
.DS_Store
2 changes: 1 addition & 1 deletion docs/sanic/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,14 +633,31 @@ async def create_server(self, host=None, port=None, debug=False,
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:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_server_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ deps =
coverage
pytest
pytest-cov
pytest-sanic
pytest-sugar
aiohttp==1.3.5
chardet<=2.3.0
Expand Down