Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Jan 5, 2022
1 parent 18c6709 commit 458a4ea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
42 changes: 26 additions & 16 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ def _loop_add_task(
else:
task = loop.create_task(prepped, name=name)

if name and register:
if name and register and sys.version_info > (3, 7):
app._task_registry[name] = task

return task
Expand Down Expand Up @@ -1625,10 +1625,12 @@ def add_task(
def get_task(
self, name: str, *, raise_exception: bool = True
) -> Optional[Task]:
if sys.version_info == (3, 7):
raise RuntimeError(
"This feature is only supported on using Python 3.8+."
if sys.version_info < (3, 8):
error_logger.warning(
"This feature (get_task) is only supported on using "
"Python 3.8+."
)
return
try:
return self._task_registry[name]
except KeyError:
Expand All @@ -1645,10 +1647,12 @@ async def cancel_task(
*,
raise_exception: bool = True,
) -> None:
if sys.version_info == (3, 7):
raise RuntimeError(
"This feature is only supported on using Python 3.8+."
if sys.version_info < (3, 8):
error_logger.warning(
"This feature (cancel_task) is only supported on using "
"Python 3.8+."
)
return
task = self.get_task(name, raise_exception=raise_exception)
if task and not task.cancelled():
args: Tuple[str, ...] = ()
Expand All @@ -1667,10 +1671,12 @@ async def cancel_task(
...

def purge_tasks(self):
if sys.version_info == (3, 7):
raise RuntimeError(
"This feature is only supported on using Python 3.8+."
if sys.version_info < (3, 8):
error_logger.warning(
"This feature (purge_tasks) is only supported on using "
"Python 3.8+."
)
return
for task in self.tasks:
if task.done() or task.cancelled():
name = task.get_name()
Expand All @@ -1683,10 +1689,12 @@ def purge_tasks(self):
def shutdown_tasks(
self, timeout: Optional[float] = None, increment: float = 0.1
):
if sys.version_info == (3, 7):
raise RuntimeError(
"This feature is only supported on using Python 3.8+."
if sys.version_info < (3, 8):
error_logger.warning(
"This feature (shutdown_tasks) is only supported on using "
"Python 3.8+."
)
return
for task in self.tasks:
task.cancel()

Expand All @@ -1700,10 +1708,12 @@ def shutdown_tasks(

@property
def tasks(self):
if sys.version_info == (3, 7):
raise RuntimeError(
"This feature is only supported on using Python 3.8+."
if sys.version_info < (3, 8):
error_logger.warning(
"This feature (tasks) is only supported on using "
"Python 3.8+."
)
return
return iter(self._task_registry.values())

# -------------------------------------------------------------------- #
Expand Down
20 changes: 20 additions & 0 deletions tests/test_create_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys

from threading import Event
from unittest.mock import Mock

import pytest

Expand Down Expand Up @@ -77,6 +78,25 @@ async def stop(app, _):
app.run()


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"


@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():
Expand Down

0 comments on commit 458a4ea

Please sign in to comment.