2016-12-29 21:35:41 +00:00
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.response import HTTPResponse
|
|
|
|
from sanic.utils import HOST, PORT
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
import asyncio
|
2017-01-22 02:26:32 +00:00
|
|
|
from queue import Queue
|
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()
|
|
|
|
|
2017-01-22 02:26:32 +00:00
|
|
|
calledq = Queue()
|
|
|
|
|
|
|
|
def set_loop(app, loop):
|
|
|
|
loop.add_signal_handler = MagicMock()
|
|
|
|
|
|
|
|
def after(app, loop):
|
|
|
|
calledq.put(loop.add_signal_handler.called)
|
2016-12-29 21:35:41 +00:00
|
|
|
|
|
|
|
def test_register_system_signals():
|
|
|
|
"""Test if sanic register system signals"""
|
|
|
|
app = Sanic('test_register_system_signals')
|
|
|
|
|
|
|
|
@app.route('/hello')
|
|
|
|
async def hello_route(request):
|
|
|
|
return HTTPResponse()
|
|
|
|
|
2017-02-11 14:30:17 +00:00
|
|
|
app.listener('after_server_start')(stop)
|
|
|
|
app.listener('before_server_start')(set_loop)
|
|
|
|
app.listener('after_server_stop')(after)
|
|
|
|
|
|
|
|
app.run(HOST, PORT)
|
2017-01-22 02:26:32 +00:00
|
|
|
assert calledq.get() == True
|
2016-12-29 21:35:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_dont_register_system_signals():
|
|
|
|
"""Test if sanic don't register system signals"""
|
|
|
|
app = Sanic('test_register_system_signals')
|
|
|
|
|
|
|
|
@app.route('/hello')
|
|
|
|
async def hello_route(request):
|
|
|
|
return HTTPResponse()
|
|
|
|
|
2017-02-11 14:30:17 +00:00
|
|
|
app.listener('after_server_start')(stop)
|
|
|
|
app.listener('before_server_start')(set_loop)
|
|
|
|
app.listener('after_server_stop')(after)
|
|
|
|
|
|
|
|
app.run(HOST, PORT, register_sys_signals=False)
|
2017-01-22 02:26:32 +00:00
|
|
|
assert calledq.get() == False
|