Skip to content

Commit 9f7d84e

Browse files
committed
avoid the cost of actual time
1 parent 058e606 commit 9f7d84e

File tree

1 file changed

+27
-53
lines changed

1 file changed

+27
-53
lines changed

tests/transport/aio/test_aiohttp.py

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,89 +21,63 @@
2121

2222
@pytest.fixture
2323
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)
3124
return True
3225

3326

3427
class TestTimeoutGuard(object):
28+
default_timeout = 1
29+
3530
def make_timeout_guard(self, timeout):
3631
return auth_aiohttp.timeout_guard(timeout)
3732

3833
@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(
4035
self, simple_async_task
4136
):
4237
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)
4544

4645
# Task succeeds.
4746
assert task is True
4847

4948
@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(
5150
self, simple_async_task
5251
):
5352
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)
5760

5861
# Task does not succeed and the context manager times out i.e. no remaining time left.
5962
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+
)
9466

9567
@pytest.mark.asyncio
9668
async def test_timeout_with_async_task_timing_out_before_context(
9769
self, simple_async_task
9870
):
99-
task_1 = False
71+
task = False
10072
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:
10276
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)
10478

10579
# Task does not complete i.e. the operation times out.
106-
assert task_1 is False
80+
assert task is False
10781
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."
10983
)

0 commit comments

Comments
 (0)