fix async run, add tests
This commit is contained in:
parent
7257e5794f
commit
b44d927172
|
@ -297,7 +297,8 @@ def serve(host, port, request_handler, error_handler, before_start=None,
|
||||||
:param protocol: Subclass of asyncio protocol class
|
:param protocol: Subclass of asyncio protocol class
|
||||||
:return: Nothing
|
:return: Nothing
|
||||||
"""
|
"""
|
||||||
loop = async_loop.new_event_loop()
|
loop = loop or async_loop.new_event_loop()
|
||||||
|
asyncio.set_event_loop_policy(async_loop.EventLoopPolicy())
|
||||||
asyncio.set_event_loop(loop)
|
asyncio.set_event_loop(loop)
|
||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
|
|
28
tests/test_async_run.py
Normal file
28
tests/test_async_run.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
from sanic import Sanic
|
||||||
|
from sanic.response import json
|
||||||
|
from multiprocessing import Event
|
||||||
|
from signal import signal, SIGINT
|
||||||
|
import asyncio
|
||||||
|
import requests
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
def test_async_run():
|
||||||
|
app = Sanic(__name__)
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
async def test(request):
|
||||||
|
return json({"answer": "42"})
|
||||||
|
|
||||||
|
server = app.create_server(host="0.0.0.0", port=8001)
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
task = asyncio.ensure_future(server)
|
||||||
|
signal(SIGINT, lambda s, f: loop.close())
|
||||||
|
try:
|
||||||
|
t = Thread(target=loop.run_forever)
|
||||||
|
t.start()
|
||||||
|
except:
|
||||||
|
loop.stop()
|
||||||
|
res = requests.get('http://localhost:8001')
|
||||||
|
loop.stop()
|
||||||
|
assert res.json()['answer'] == '42'
|
18
tests/test_loop_policy.py
Normal file
18
tests/test_loop_policy.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
from sanic import Sanic
|
||||||
|
from sanic.response import text
|
||||||
|
from sanic.utils import sanic_endpoint_test
|
||||||
|
import asyncio
|
||||||
|
import uvloop
|
||||||
|
|
||||||
|
def test_loop_policy():
|
||||||
|
app = Sanic('test_loop_policy')
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def test(request):
|
||||||
|
return text("OK")
|
||||||
|
|
||||||
|
server = app.create_server()
|
||||||
|
|
||||||
|
request, response = sanic_endpoint_test(app)
|
||||||
|
assert isinstance(asyncio.get_event_loop_policy(),
|
||||||
|
uvloop.EventLoopPolicy)
|
Loading…
Reference in New Issue
Block a user