Skip to content

Commit

Permalink
Fixed macos testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rius committed Dec 18, 2024
1 parent f593179 commit 5e09202
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions tests/middlewares/test_task_retry.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import asyncio
import platform
import time

import pytest

from taskiq import InMemoryBroker, SimpleRetryMiddleware
from taskiq.exceptions import NoResultError

pytestmark = pytest.mark.skipif(
platform.system().lower() == "darwin",
reason="Not supported on macOS",
)


@pytest.mark.anyio
async def test_wait_result() -> None:
Expand All @@ -22,18 +15,18 @@ async def test_wait_result() -> None:
runs = 0

@broker.task(retry_on_error=True)
def run_task() -> str:
async def run_task() -> str:
nonlocal runs

if runs == 0:
runs += 1
raise Exception("Retry")

time.sleep(0.2)
return "hello world!"

task = await run_task.kiq()
resp = await task.wait_result(0.1, timeout=1)
assert runs == 1

assert resp.return_value == "hello world!"

Expand All @@ -45,24 +38,28 @@ async def test_wait_result_error() -> None:
SimpleRetryMiddleware(no_result_on_retry=False),
)
runs = 0
lock = asyncio.Lock()

@broker.task(retry_on_error=True)
def run_task() -> str:
nonlocal runs
async def run_task() -> str:
nonlocal runs, lock

await lock.acquire()

if runs == 0:
runs += 1
raise ValueError("Retry")

time.sleep(0.2)
return "hello world!"

task = await run_task.kiq()
resp = await task.wait_result(0.1, timeout=1)
with pytest.raises(ValueError):
resp.raise_for_error()
assert resp.is_err
assert runs == 1

broker.result_backend.results.pop(task.task_id) # type: ignore
lock.release()

await asyncio.sleep(0.2)
resp = await task.wait_result(timeout=1)
assert resp.return_value == "hello world!"

Expand All @@ -73,32 +70,34 @@ async def test_wait_result_no_result() -> None:
broker = InMemoryBroker().with_middlewares(
SimpleRetryMiddleware(no_result_on_retry=False),
)
done = False
done = asyncio.Event()
runs = 0
lock = asyncio.Lock()

@broker.task(retry_on_error=True)
def run_task() -> str:
nonlocal runs, done
async def run_task() -> str:
nonlocal runs, done, lock

await lock.acquire()

if runs == 0:
runs += 1
raise ValueError("Retry")

time.sleep(0.2)
done = True
done.set()
raise NoResultError

task = await run_task.kiq()
resp = await task.wait_result(0.1, timeout=1)
with pytest.raises(ValueError):
resp.raise_for_error()

await asyncio.sleep(0.2)
resp = await task.wait_result(timeout=1)
with pytest.raises(ValueError):
resp.raise_for_error()
broker.result_backend.results.pop(task.task_id) # type: ignore
lock.release()

assert done
assert await asyncio.wait_for(done.wait(), timeout=1)
with pytest.raises(KeyError):
await broker.result_backend.get_result(task.task_id)


@pytest.mark.anyio
Expand Down

0 comments on commit 5e09202

Please sign in to comment.