2017-02-11 22:52:40 +00:00
|
|
|
import asyncio
|
2021-12-20 21:50:04 +00:00
|
|
|
import sys
|
2019-04-23 22:44:42 +01:00
|
|
|
from threading import Event
|
|
|
|
|
2021-12-20 21:50:04 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from sanic.exceptions import SanicException
|
2019-04-23 22:44:42 +01:00
|
|
|
from sanic.response import text
|
2017-02-11 22:52:40 +00:00
|
|
|
|
2017-05-04 07:52:18 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_create_task(app):
|
2017-02-11 22:52:40 +00:00
|
|
|
e = Event()
|
2017-05-04 07:52:18 +01:00
|
|
|
|
2017-02-11 22:52:40 +00:00
|
|
|
async def coro():
|
|
|
|
await asyncio.sleep(0.05)
|
|
|
|
e.set()
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/early")
|
2017-02-11 22:52:40 +00:00
|
|
|
def not_set(request):
|
2020-03-28 18:43:14 +00:00
|
|
|
return text(str(e.is_set()))
|
2017-02-11 22:52:40 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/late")
|
2017-02-11 22:52:40 +00:00
|
|
|
async def set(request):
|
|
|
|
await asyncio.sleep(0.1)
|
2020-03-28 18:43:14 +00:00
|
|
|
return text(str(e.is_set()))
|
2017-02-11 22:52:40 +00:00
|
|
|
|
2021-08-05 20:55:42 +01:00
|
|
|
app.add_task(coro)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/early")
|
|
|
|
assert response.body == b"False"
|
2017-02-11 22:52:40 +00:00
|
|
|
|
2021-08-05 20:55:42 +01:00
|
|
|
app.signal_router.reset()
|
|
|
|
app.add_task(coro)
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/late")
|
|
|
|
assert response.body == b"True"
|
2017-12-22 01:12:31 +00:00
|
|
|
|
2018-10-22 21:25:38 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_create_task_with_app_arg(app):
|
2021-08-05 20:55:42 +01:00
|
|
|
@app.after_server_start
|
|
|
|
async def setup_q(app, _):
|
|
|
|
app.ctx.q = asyncio.Queue()
|
2017-12-22 01:12:31 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2021-08-05 20:55:42 +01:00
|
|
|
async def not_set(request):
|
|
|
|
return text(await request.app.ctx.q.get())
|
2017-12-22 01:12:31 +00:00
|
|
|
|
|
|
|
async def coro(app):
|
2021-08-05 20:55:42 +01:00
|
|
|
await app.ctx.q.put(app.name)
|
2017-12-22 01:12:31 +00:00
|
|
|
|
|
|
|
app.add_task(coro)
|
|
|
|
|
2021-08-05 20:55:42 +01:00
|
|
|
_, response = app.test_client.get("/")
|
|
|
|
assert response.text == "test_create_task_with_app_arg"
|
2021-12-20 21:50:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Not supported in 3.7")
|
|
|
|
def test_create_named_task(app):
|
|
|
|
async def dummy():
|
|
|
|
...
|
|
|
|
|
|
|
|
@app.before_server_start
|
|
|
|
async def setup(app, _):
|
|
|
|
app.add_task(dummy, name="dummy_task")
|
|
|
|
|
|
|
|
@app.after_server_start
|
|
|
|
async def stop(app, _):
|
|
|
|
task = app.get_task("dummy_task")
|
|
|
|
|
|
|
|
assert app._task_registry
|
|
|
|
assert isinstance(task, asyncio.Task)
|
|
|
|
|
|
|
|
assert task.get_name() == "dummy_task"
|
|
|
|
|
|
|
|
app.stop()
|
|
|
|
|
2022-12-15 09:49:26 +00:00
|
|
|
app.run(single_process=True)
|
2021-12-20 21:50:04 +00:00
|
|
|
|
|
|
|
|
2022-01-06 10:40:52 +00:00
|
|
|
def test_named_task_called(app):
|
|
|
|
e = Event()
|
|
|
|
|
|
|
|
async def coro():
|
|
|
|
e.set()
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
async def isset(request):
|
|
|
|
await asyncio.sleep(0.05)
|
|
|
|
return text(str(e.is_set()))
|
|
|
|
|
|
|
|
@app.before_server_start
|
|
|
|
async def setup(app, _):
|
|
|
|
app.add_task(coro, name="dummy_task")
|
|
|
|
|
|
|
|
request, response = app.test_client.get("/")
|
|
|
|
assert response.body == b"True"
|
|
|
|
|
|
|
|
|
2021-12-20 21:50:04 +00:00
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Not supported in 3.7")
|
|
|
|
def test_create_named_task_fails_outside_app(app):
|
|
|
|
async def dummy():
|
|
|
|
...
|
|
|
|
|
|
|
|
message = "Cannot name task outside of a running application"
|
|
|
|
with pytest.raises(RuntimeError, match=message):
|
|
|
|
app.add_task(dummy, name="dummy_task")
|
|
|
|
assert not app._task_registry
|
|
|
|
|
|
|
|
message = 'Registered task named "dummy_task" not found.'
|
|
|
|
with pytest.raises(SanicException, match=message):
|
|
|
|
app.get_task("dummy_task")
|