Skip to content

Commit 5e09202

Browse files
committed
Fixed macos testing.
1 parent f593179 commit 5e09202

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

tests/middlewares/test_task_retry.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import asyncio
2-
import platform
3-
import time
42

53
import pytest
64

75
from taskiq import InMemoryBroker, SimpleRetryMiddleware
86
from taskiq.exceptions import NoResultError
97

10-
pytestmark = pytest.mark.skipif(
11-
platform.system().lower() == "darwin",
12-
reason="Not supported on macOS",
13-
)
14-
158

169
@pytest.mark.anyio
1710
async def test_wait_result() -> None:
@@ -22,18 +15,18 @@ async def test_wait_result() -> None:
2215
runs = 0
2316

2417
@broker.task(retry_on_error=True)
25-
def run_task() -> str:
18+
async def run_task() -> str:
2619
nonlocal runs
2720

2821
if runs == 0:
2922
runs += 1
3023
raise Exception("Retry")
3124

32-
time.sleep(0.2)
3325
return "hello world!"
3426

3527
task = await run_task.kiq()
3628
resp = await task.wait_result(0.1, timeout=1)
29+
assert runs == 1
3730

3831
assert resp.return_value == "hello world!"
3932

@@ -45,24 +38,28 @@ async def test_wait_result_error() -> None:
4538
SimpleRetryMiddleware(no_result_on_retry=False),
4639
)
4740
runs = 0
41+
lock = asyncio.Lock()
4842

4943
@broker.task(retry_on_error=True)
50-
def run_task() -> str:
51-
nonlocal runs
44+
async def run_task() -> str:
45+
nonlocal runs, lock
46+
47+
await lock.acquire()
5248

5349
if runs == 0:
5450
runs += 1
5551
raise ValueError("Retry")
5652

57-
time.sleep(0.2)
5853
return "hello world!"
5954

6055
task = await run_task.kiq()
6156
resp = await task.wait_result(0.1, timeout=1)
62-
with pytest.raises(ValueError):
63-
resp.raise_for_error()
57+
assert resp.is_err
58+
assert runs == 1
59+
60+
broker.result_backend.results.pop(task.task_id) # type: ignore
61+
lock.release()
6462

65-
await asyncio.sleep(0.2)
6663
resp = await task.wait_result(timeout=1)
6764
assert resp.return_value == "hello world!"
6865

@@ -73,32 +70,34 @@ async def test_wait_result_no_result() -> None:
7370
broker = InMemoryBroker().with_middlewares(
7471
SimpleRetryMiddleware(no_result_on_retry=False),
7572
)
76-
done = False
73+
done = asyncio.Event()
7774
runs = 0
75+
lock = asyncio.Lock()
7876

7977
@broker.task(retry_on_error=True)
80-
def run_task() -> str:
81-
nonlocal runs, done
78+
async def run_task() -> str:
79+
nonlocal runs, done, lock
80+
81+
await lock.acquire()
8282

8383
if runs == 0:
8484
runs += 1
8585
raise ValueError("Retry")
8686

87-
time.sleep(0.2)
88-
done = True
87+
done.set()
8988
raise NoResultError
9089

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

96-
await asyncio.sleep(0.2)
97-
resp = await task.wait_result(timeout=1)
98-
with pytest.raises(ValueError):
99-
resp.raise_for_error()
95+
broker.result_backend.results.pop(task.task_id) # type: ignore
96+
lock.release()
10097

101-
assert done
98+
assert await asyncio.wait_for(done.wait(), timeout=1)
99+
with pytest.raises(KeyError):
100+
await broker.result_backend.get_result(task.task_id)
102101

103102

104103
@pytest.mark.anyio

0 commit comments

Comments
 (0)