2017-01-04 06:23:35 +00:00
|
|
|
import signal
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2018-03-16 04:28:52 +00:00
|
|
|
from sanic.testing import HOST, PORT
|
2017-01-04 06:23:35 +00:00
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2017-01-04 06:23:35 +00:00
|
|
|
AVAILABLE_LISTENERS = [
|
2018-12-30 11:18:06 +00:00
|
|
|
"before_server_start",
|
|
|
|
"after_server_start",
|
|
|
|
"before_server_stop",
|
|
|
|
"after_server_stop",
|
2017-01-04 06:23:35 +00:00
|
|
|
]
|
|
|
|
|
2018-09-29 18:54:47 +01:00
|
|
|
skipif_no_alarm = pytest.mark.skipif(
|
2018-12-30 11:18:06 +00:00
|
|
|
not hasattr(signal, "SIGALRM"),
|
|
|
|
reason="SIGALRM is not implemented for this platform, we have to come "
|
|
|
|
"up with another timeout strategy to test these",
|
2018-09-29 18:54:47 +01:00
|
|
|
)
|
|
|
|
|
2017-01-04 06:23:35 +00:00
|
|
|
|
|
|
|
def create_listener(listener_name, in_list):
|
|
|
|
async def _listener(app, loop):
|
2018-12-30 11:18:06 +00:00
|
|
|
print("DEBUG MESSAGE FOR PYTEST for {}".format(listener_name))
|
2017-01-04 06:23:35 +00:00
|
|
|
in_list.insert(0, app.name + listener_name)
|
2018-12-30 11:18:06 +00:00
|
|
|
|
2017-01-04 06:23:35 +00:00
|
|
|
return _listener
|
|
|
|
|
|
|
|
|
|
|
|
def start_stop_app(random_name_app, **run_kwargs):
|
|
|
|
def stop_on_alarm(signum, frame):
|
2018-12-30 11:18:06 +00:00
|
|
|
raise KeyboardInterrupt("SIGINT for sanic to stop gracefully")
|
2017-01-04 06:23:35 +00:00
|
|
|
|
|
|
|
signal.signal(signal.SIGALRM, stop_on_alarm)
|
|
|
|
signal.alarm(1)
|
|
|
|
try:
|
2018-03-16 04:28:52 +00:00
|
|
|
random_name_app.run(HOST, PORT, **run_kwargs)
|
2017-01-04 06:23:35 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-09-29 18:54:47 +01:00
|
|
|
@skipif_no_alarm
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("listener_name", AVAILABLE_LISTENERS)
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_single_listener(app, listener_name):
|
2017-01-04 06:23:35 +00:00
|
|
|
"""Test that listeners on their own work"""
|
2018-08-26 15:43:14 +01:00
|
|
|
output = []
|
2017-02-11 14:30:17 +00:00
|
|
|
# Register listener
|
2018-12-30 11:18:06 +00:00
|
|
|
app.listener(listener_name)(create_listener(listener_name, output))
|
2018-08-26 15:43:14 +01:00
|
|
|
start_stop_app(app)
|
|
|
|
assert app.name + listener_name == output.pop()
|
2017-01-04 06:23:35 +00:00
|
|
|
|
|
|
|
|
2018-09-29 18:54:47 +01:00
|
|
|
@skipif_no_alarm
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("listener_name", AVAILABLE_LISTENERS)
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_register_listener(app, listener_name):
|
2018-02-09 22:01:17 +00:00
|
|
|
"""
|
|
|
|
Test that listeners on their own work with
|
|
|
|
app.register_listener method
|
|
|
|
"""
|
2018-08-26 15:43:14 +01:00
|
|
|
output = []
|
2018-02-09 22:01:17 +00:00
|
|
|
# Register listener
|
|
|
|
listener = create_listener(listener_name, output)
|
2018-09-29 18:54:47 +01:00
|
|
|
app.register_listener(listener, event=listener_name)
|
2018-08-26 15:43:14 +01:00
|
|
|
start_stop_app(app)
|
|
|
|
assert app.name + listener_name == output.pop()
|
2018-02-09 22:01:17 +00:00
|
|
|
|
|
|
|
|
2018-09-29 18:54:47 +01:00
|
|
|
@skipif_no_alarm
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_all_listeners(app):
|
|
|
|
output = []
|
2017-02-11 14:30:17 +00:00
|
|
|
for listener_name in AVAILABLE_LISTENERS:
|
|
|
|
listener = create_listener(listener_name, output)
|
2018-08-26 15:43:14 +01:00
|
|
|
app.listener(listener_name)(listener)
|
|
|
|
start_stop_app(app)
|
2017-01-04 06:23:35 +00:00
|
|
|
for listener_name in AVAILABLE_LISTENERS:
|
2018-08-26 15:43:14 +01:00
|
|
|
assert app.name + listener_name == output.pop()
|
2017-08-09 06:21:40 +01:00
|
|
|
|
|
|
|
|
2019-06-04 08:58:00 +01:00
|
|
|
@pytest.mark.asyncio
|
2018-08-26 15:43:14 +01:00
|
|
|
async def test_trigger_before_events_create_server(app):
|
2017-08-09 06:21:40 +01:00
|
|
|
class MySanicDb:
|
|
|
|
pass
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.listener("before_server_start")
|
2017-08-09 06:21:40 +01:00
|
|
|
async def init_db(app, loop):
|
|
|
|
app.db = MySanicDb()
|
|
|
|
|
2019-05-30 15:10:00 +01:00
|
|
|
await app.create_server(debug=True, return_asyncio_server=True, port=PORT)
|
2017-08-09 06:21:40 +01:00
|
|
|
|
|
|
|
assert hasattr(app, "db")
|
|
|
|
assert isinstance(app.db, MySanicDb)
|