|
21 | 21 |
|
22 | 22 | @pytest.fixture
|
23 | 23 | async def simple_async_task():
|
24 |
| - await asyncio.sleep(0.1) |
25 |
| - return True |
26 |
| - |
27 |
| - |
28 |
| -@pytest.fixture |
29 |
| -async def long_running_async_task(): |
30 |
| - await asyncio.sleep(0.3) |
31 | 24 | return True
|
32 | 25 |
|
33 | 26 |
|
34 | 27 | class TestTimeoutGuard(object):
|
| 28 | + default_timeout = 1 |
| 29 | + |
35 | 30 | def make_timeout_guard(self, timeout):
|
36 | 31 | return auth_aiohttp.timeout_guard(timeout)
|
37 | 32 |
|
38 | 33 | @pytest.mark.asyncio
|
39 |
| - async def test_timeout_with_single_async_task_within_bounds( |
| 34 | + async def test_timeout_with_simple_async_task_within_bounds( |
40 | 35 | self, simple_async_task
|
41 | 36 | ):
|
42 | 37 | task = False
|
43 |
| - async with self.make_timeout_guard(timeout=0.2) as with_timeout: |
44 |
| - task = await with_timeout(simple_async_task) |
| 38 | + with patch("time.monotonic", side_effect=[0, 0.25, 0.75]): |
| 39 | + with patch("asyncio.wait_for", lambda coro, timeout: coro): |
| 40 | + async with self.make_timeout_guard( |
| 41 | + timeout=self.default_timeout |
| 42 | + ) as with_timeout: |
| 43 | + task = await with_timeout(simple_async_task) |
45 | 44 |
|
46 | 45 | # Task succeeds.
|
47 | 46 | assert task is True
|
48 | 47 |
|
49 | 48 | @pytest.mark.asyncio
|
50 |
| - async def test_timeout_with_single_async_task_out_of_bounds( |
| 49 | + async def test_timeout_with_simple_async_task_out_of_bounds( |
51 | 50 | self, simple_async_task
|
52 | 51 | ):
|
53 | 52 | task = False
|
54 |
| - with pytest.raises(TimeoutError) as exc: |
55 |
| - async with self.make_timeout_guard(timeout=0.1) as with_timeout: |
56 |
| - task = await with_timeout(simple_async_task) |
| 53 | + with patch("time.monotonic", side_effect=[0, 1, 1]): |
| 54 | + with patch("asyncio.wait_for", lambda coro, timeout: coro): |
| 55 | + with pytest.raises(TimeoutError) as exc: |
| 56 | + async with self.make_timeout_guard( |
| 57 | + timeout=self.default_timeout |
| 58 | + ) as with_timeout: |
| 59 | + task = await with_timeout(simple_async_task) |
57 | 60 |
|
58 | 61 | # Task does not succeed and the context manager times out i.e. no remaining time left.
|
59 | 62 | assert task is False
|
60 |
| - assert exc.match("Context manager exceeded the configured timeout of 0.1s.") |
61 |
| - |
62 |
| - @pytest.mark.asyncio |
63 |
| - async def test_timeout_with_multiple_async_tasks_within_bounds( |
64 |
| - self, simple_async_task, long_running_async_task |
65 |
| - ): |
66 |
| - task_1 = task_2 = False |
67 |
| - async with self.make_timeout_guard(timeout=0.5) as with_timeout: |
68 |
| - |
69 |
| - task_1 = await with_timeout(simple_async_task) |
70 |
| - task_2 = await with_timeout(long_running_async_task) |
71 |
| - |
72 |
| - # Tasks succeed. |
73 |
| - assert task_1 is True |
74 |
| - assert task_2 is True |
75 |
| - |
76 |
| - @pytest.mark.asyncio |
77 |
| - async def test_timeout_with_multiple_async_tasks_out_of_bounds( |
78 |
| - self, simple_async_task, long_running_async_task |
79 |
| - ): |
80 |
| - task_1 = task_2 = False |
81 |
| - with pytest.raises(TimeoutError) as exc: |
82 |
| - async with self.make_timeout_guard(timeout=0.4) as with_timeout: |
83 |
| - |
84 |
| - # First task succeeds |
85 |
| - task_1 = await with_timeout(simple_async_task) |
86 |
| - task_2 = await with_timeout(long_running_async_task) |
87 |
| - |
88 |
| - # First task succeeds. |
89 |
| - assert task_1 is True |
90 |
| - # Second task fails and the context manager times out i.e. no remaining time left. |
91 |
| - assert task_2 is False |
92 |
| - |
93 |
| - assert exc.match("Context manager exceeded the configured timeout of 0.4s.") |
| 63 | + assert exc.match( |
| 64 | + f"Context manager exceeded the configured timeout of {self.default_timeout}s." |
| 65 | + ) |
94 | 66 |
|
95 | 67 | @pytest.mark.asyncio
|
96 | 68 | async def test_timeout_with_async_task_timing_out_before_context(
|
97 | 69 | self, simple_async_task
|
98 | 70 | ):
|
99 |
| - task_1 = False |
| 71 | + task = False |
100 | 72 | with pytest.raises(TimeoutError) as exc:
|
101 |
| - async with self.make_timeout_guard(timeout=0.4) as with_timeout: |
| 73 | + async with self.make_timeout_guard( |
| 74 | + timeout=self.default_timeout |
| 75 | + ) as with_timeout: |
102 | 76 | with patch("asyncio.wait_for", side_effect=asyncio.TimeoutError):
|
103 |
| - task_1 = await with_timeout(simple_async_task) |
| 77 | + task = await with_timeout(simple_async_task) |
104 | 78 |
|
105 | 79 | # Task does not complete i.e. the operation times out.
|
106 |
| - assert task_1 is False |
| 80 | + assert task is False |
107 | 81 | assert exc.match(
|
108 |
| - f"The operation {simple_async_task} exceeded the configured timeout of 0.4s." |
| 82 | + f"The operation {simple_async_task} exceeded the configured timeout of {self.default_timeout}s." |
109 | 83 | )
|
0 commit comments