Skip to content

Commit

Permalink
Add tasks tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Jan 4, 2024
1 parent 4f70f15 commit 2bf2d66
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
9 changes: 8 additions & 1 deletion backgrounder/decorator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import asyncio
import functools
from typing import Any, Callable

import anyio.from_thread
import anyio.to_thread
import nest_asyncio
import sniffio

from backgrounder.concurrency import AsyncCallable
from backgrounder.tasks import Task

nest_asyncio.apply()


def background(fn: Callable[..., Any]) -> Callable[..., Any]:
"""
Expand All @@ -24,7 +29,9 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
task = Task(fn, *args, **kwargs)
try:
sniffio.current_async_library()
return anyio.from_thread.run(fn, *args, **kwargs)
async_callable = AsyncCallable(fn)
loop = asyncio.get_event_loop()
return loop.run_until_complete(async_callable(*args, **kwargs))
except sniffio.AsyncLibraryNotFoundError:
return anyio.run(task)

Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ classifiers = [
"Topic :: Internet :: WWW/HTTP :: HTTP Servers",
"Topic :: Internet :: WWW/HTTP",
]
dependencies = ["anyio>=4.2.0,<5"]
dependencies = ["anyio>=4.2.0,<5", "nest_asyncio>=1.5.8,<2.0.0"]
keywords = ["backgrounder"]

[project.urls]
Expand Down Expand Up @@ -133,6 +133,11 @@ check_untyped_defs = true
module = "docs_src.*"
ignore_errors = true

[[tool.mypy.overrides]]
module = ["nest_asyncio.*"]
ignore_missing_imports = true
ignore_errors = true

[tool.pytest.ini_options]
addopts = ["--strict-config", "--strict-markers"]
xfail_strict = true
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from esmerald.testclient import TestClient


@pytest.fixture(scope="module")
def anyio_backend():
return ("asyncio", {"debug": True})


@pytest.fixture
def test_client_factory(anyio_backend_name, anyio_backend_options):
return functools.partial(
Expand Down
24 changes: 14 additions & 10 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
from backgrounder.decorator import background
from backgrounder.tasks import Task

# @background
# async def work(number):
# # Do something expensive here.
# return number
GLOBAL = 0

pytestmark = pytest.mark.anyio

# @pytest.mark.anyio
# async def test_decorator_async():
# result = await work(2)
# assert result == 2

async def test_decorator_async():
TOTAL = 0

@background
async def work():
# Do something expensive here.
nonlocal TOTAL
TOTAL = 1

await work()
assert TOTAL == 1


@pytest.mark.asyncio
async def test_task():
TOTAL = 0

Expand All @@ -33,7 +38,6 @@ async def work(number):
assert TOTAL == 1


@pytest.mark.asyncio
async def test_task_blocking():
TOTAL = 0

Expand Down

0 comments on commit 2bf2d66

Please sign in to comment.