sanic/tests/test_create_task.py

31 lines
696 B
Python
Raw Normal View History

2017-05-04 08:04:02 +01:00
import sanic
from sanic.utils import sanic_endpoint_test
2017-02-11 22:52:40 +00:00
from sanic.response import text
from threading import Event
import asyncio
2017-02-13 22:17:58 +00:00
def test_create_task():
2017-02-11 22:52:40 +00:00
e = Event()
async def coro():
await asyncio.sleep(0.05)
e.set()
2017-05-04 08:04:02 +01:00
app = sanic.Sanic()
2017-02-12 20:07:59 +00:00
app.add_task(coro)
2017-02-11 22:52:40 +00:00
@app.route('/early')
def not_set(request):
return text(e.is_set())
@app.route('/late')
async def set(request):
await asyncio.sleep(0.1)
return text(e.is_set())
2017-05-04 08:04:02 +01:00
request, response = sanic_endpoint_test(app, uri='/early')
2017-02-11 22:52:40 +00:00
assert response.body == b'False'
2017-05-04 08:04:02 +01:00
request, response = sanic_endpoint_test(app, uri='/late')
2017-02-11 22:52:40 +00:00
assert response.body == b'True'