2016-12-29 21:35:41 +00:00
|
|
|
import asyncio
|
2020-03-18 15:01:18 +00:00
|
|
|
import os
|
|
|
|
import signal
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2017-01-22 02:26:32 +00:00
|
|
|
from queue import Queue
|
2019-04-23 22:44:42 +01:00
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
|
|
from sanic.response import HTTPResponse
|
|
|
|
from sanic.testing import HOST, PORT
|
2016-12-29 21:35:41 +00:00
|
|
|
|
|
|
|
|
2017-01-22 02:26:32 +00:00
|
|
|
async def stop(app, loop):
|
|
|
|
await asyncio.sleep(0.1)
|
2016-12-29 21:35:41 +00:00
|
|
|
app.stop()
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
|
2017-01-22 02:26:32 +00:00
|
|
|
calledq = Queue()
|
|
|
|
|
2018-10-22 21:25:38 +01:00
|
|
|
|
2017-01-22 02:26:32 +00:00
|
|
|
def set_loop(app, loop):
|
2020-03-18 15:01:18 +00:00
|
|
|
global mock
|
|
|
|
mock = MagicMock()
|
|
|
|
if os.name == "nt":
|
|
|
|
signal.signal = mock
|
|
|
|
else:
|
|
|
|
loop.add_signal_handler = mock
|
2017-01-22 02:26:32 +00:00
|
|
|
|
2018-10-22 21:25:38 +01:00
|
|
|
|
2017-01-22 02:26:32 +00:00
|
|
|
def after(app, loop):
|
2020-03-18 15:01:18 +00:00
|
|
|
calledq.put(mock.called)
|
2016-12-29 21:35:41 +00:00
|
|
|
|
2018-10-22 21:25:38 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_register_system_signals(app):
|
2016-12-29 21:35:41 +00:00
|
|
|
"""Test if sanic register system signals"""
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/hello")
|
2016-12-29 21:35:41 +00:00
|
|
|
async def hello_route(request):
|
|
|
|
return HTTPResponse()
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
app.listener("after_server_start")(stop)
|
|
|
|
app.listener("before_server_start")(set_loop)
|
|
|
|
app.listener("after_server_stop")(after)
|
2017-02-11 14:30:17 +00:00
|
|
|
|
2018-03-16 04:28:52 +00:00
|
|
|
app.run(HOST, PORT)
|
2018-10-22 21:25:38 +01:00
|
|
|
assert calledq.get() is True
|
2016-12-29 21:35:41 +00:00
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_dont_register_system_signals(app):
|
2016-12-29 21:35:41 +00:00
|
|
|
"""Test if sanic don't register system signals"""
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/hello")
|
2016-12-29 21:35:41 +00:00
|
|
|
async def hello_route(request):
|
|
|
|
return HTTPResponse()
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
app.listener("after_server_start")(stop)
|
|
|
|
app.listener("before_server_start")(set_loop)
|
|
|
|
app.listener("after_server_stop")(after)
|
2017-02-11 14:30:17 +00:00
|
|
|
|
2018-03-16 04:28:52 +00:00
|
|
|
app.run(HOST, PORT, register_sys_signals=False)
|
2018-10-22 21:25:38 +01:00
|
|
|
assert calledq.get() is False
|