Add named tasks (#2304)
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
from sanic.exceptions import SanicException
|
||||
from sanic.response import text
|
||||
|
||||
|
||||
@@ -48,3 +52,41 @@ def test_create_task_with_app_arg(app):
|
||||
|
||||
_, response = app.test_client.get("/")
|
||||
assert response.text == "test_create_task_with_app_arg"
|
||||
|
||||
|
||||
@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()
|
||||
|
||||
app.run()
|
||||
|
||||
|
||||
@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")
|
||||
|
||||
91
tests/test_tasks.py
Normal file
91
tests/test_tasks.py
Normal file
@@ -0,0 +1,91 @@
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
from asyncio.tasks import Task
|
||||
from unittest.mock import Mock, call
|
||||
|
||||
import pytest
|
||||
|
||||
from sanic.app import Sanic
|
||||
from sanic.response import empty
|
||||
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
async def dummy(n=0):
|
||||
for _ in range(n):
|
||||
await asyncio.sleep(1)
|
||||
return True
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mark_app_running(app):
|
||||
app.is_running = True
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Not supported in 3.7")
|
||||
async def test_add_task_returns_task(app: Sanic):
|
||||
task = app.add_task(dummy())
|
||||
|
||||
assert isinstance(task, Task)
|
||||
assert len(app._task_registry) == 0
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Not supported in 3.7")
|
||||
async def test_add_task_with_name(app: Sanic):
|
||||
task = app.add_task(dummy(), name="dummy")
|
||||
|
||||
assert isinstance(task, Task)
|
||||
assert len(app._task_registry) == 1
|
||||
assert task is app.get_task("dummy")
|
||||
|
||||
for task in app.tasks:
|
||||
assert task in app._task_registry.values()
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Not supported in 3.7")
|
||||
async def test_cancel_task(app: Sanic):
|
||||
task = app.add_task(dummy(3), name="dummy")
|
||||
|
||||
assert task
|
||||
assert not task.done()
|
||||
assert not task.cancelled()
|
||||
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
assert not task.done()
|
||||
assert not task.cancelled()
|
||||
|
||||
await app.cancel_task("dummy")
|
||||
|
||||
assert task.cancelled()
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Not supported in 3.7")
|
||||
async def test_purge_tasks(app: Sanic):
|
||||
app.add_task(dummy(3), name="dummy")
|
||||
|
||||
await app.cancel_task("dummy")
|
||||
|
||||
assert len(app._task_registry) == 1
|
||||
|
||||
app.purge_tasks()
|
||||
|
||||
assert len(app._task_registry) == 0
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Not supported in 3.7")
|
||||
def test_shutdown_tasks_on_app_stop(app: Sanic):
|
||||
app.shutdown_tasks = Mock()
|
||||
|
||||
@app.route("/")
|
||||
async def handler(_):
|
||||
return empty()
|
||||
|
||||
app.test_client.get("/")
|
||||
|
||||
app.shutdown_tasks.call_args == [
|
||||
call(timeout=0),
|
||||
call(15.0),
|
||||
]
|
||||
Reference in New Issue
Block a user