-
Notifications
You must be signed in to change notification settings - Fork 706
Expand file tree
/
Copy pathtest_recurring_task.py
More file actions
56 lines (37 loc) · 1.44 KB
/
test_recurring_task.py
File metadata and controls
56 lines (37 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from __future__ import annotations
import asyncio
from datetime import timedelta
from unittest.mock import AsyncMock
import pytest
from crawlee._utils.recurring_task import RecurringTask
from tests.unit.utils import run_alone_on_mac
@pytest.fixture
def function() -> AsyncMock:
mock_function = AsyncMock()
mock_function.__name__ = 'mocked_function' # To avoid issues with the function name in RecurringTask
return mock_function
@pytest.fixture
def delay() -> timedelta:
return timedelta(milliseconds=30)
async def test_init(function: AsyncMock, delay: timedelta) -> None:
rt = RecurringTask(function, delay)
assert rt.func == function
assert rt.delay == delay
assert rt.task is None
async def test_start_and_stop(function: AsyncMock, delay: timedelta) -> None:
rt = RecurringTask(function, delay)
rt.start()
await asyncio.sleep(0) # Yield control to allow the task to start
assert isinstance(rt.task, asyncio.Task)
assert not rt.task.done()
await rt.stop()
assert rt.task.done()
@run_alone_on_mac
async def test_execution(function: AsyncMock, delay: timedelta) -> None:
task = RecurringTask(function, delay)
task.start()
await asyncio.sleep(0.1) # Wait enough for the task to execute a few times
await task.stop()
assert isinstance(task.func, AsyncMock) # To let type checker know that the function is a mock
assert task.func.call_count >= 3
await task.stop()