2021-12-20 21:50:04 +00:00
|
|
|
import asyncio
|
|
|
|
from asyncio.tasks import Task
|
|
|
|
from unittest.mock import Mock, call
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from sanic.app import Sanic
|
2022-01-16 07:03:04 +00:00
|
|
|
from sanic.application.state import ApplicationServerInfo, ServerStage
|
2021-12-20 21:50:04 +00:00
|
|
|
from sanic.response import empty
|
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
try:
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
except ImportError:
|
2022-06-27 09:19:26 +01:00
|
|
|
from tests.asyncmock import AsyncMock # type: ignore
|
2022-01-16 07:03:04 +00:00
|
|
|
|
2021-12-20 21:50:04 +00:00
|
|
|
pytestmark = pytest.mark.asyncio
|
|
|
|
|
|
|
|
|
|
|
|
async def dummy(n=0):
|
|
|
|
for _ in range(n):
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2022-01-16 07:03:04 +00:00
|
|
|
def mark_app_running(app: Sanic):
|
|
|
|
app.state.server_info.append(
|
|
|
|
ApplicationServerInfo(
|
|
|
|
stage=ServerStage.SERVING, settings={}, server=AsyncMock()
|
|
|
|
)
|
|
|
|
)
|
2021-12-20 21:50:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_add_task_returns_task(app: Sanic):
|
|
|
|
task = app.add_task(dummy())
|
|
|
|
|
|
|
|
assert isinstance(task, Task)
|
|
|
|
assert len(app._task_registry) == 0
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-02-08 06:33:09 +00:00
|
|
|
async def test_purge_tasks_with_create_task(app: Sanic):
|
|
|
|
app.add_task(asyncio.create_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
|
|
|
|
|
|
|
|
|
2021-12-23 22:30:27 +00:00
|
|
|
def test_shutdown_tasks_on_app_stop():
|
|
|
|
class TestSanic(Sanic):
|
|
|
|
shutdown_tasks = Mock()
|
|
|
|
|
|
|
|
app = TestSanic("Test")
|
2021-12-20 21:50:04 +00:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
async def handler(_):
|
|
|
|
return empty()
|
|
|
|
|
|
|
|
app.test_client.get("/")
|
|
|
|
|
|
|
|
app.shutdown_tasks.call_args == [
|
|
|
|
call(timeout=0),
|
|
|
|
call(15.0),
|
|
|
|
]
|