Debian Codegraph @

src/trio/_core/_tests/test_mock_clock.py

raw
1 import time
2 from math import inf
3
4 import pytest
5
6 from trio import sleep
7
8 from ... import _core
9 from .. import wait_all_tasks_blocked
10 from .._mock_clock import MockClock
11 from .._run import GLOBAL_RUN_CONTEXT
12 from .tutil import slow
13
14
15 def test_mock_clock() -> None:
16 REAL_NOW = 123.0
17 c = MockClock()
18 c._real_clock = lambda: REAL_NOW
19 repr(c) # smoke test
20 assert c.rate == 0
21 assert c.current_time() == 0
22 c.jump(1.2)
23 assert c.current_time() == 1.2
24 with pytest.raises(ValueError, match=r"^time can't go backwards$"):
25 c.jump(-1)
26 assert c.current_time() == 1.2
27 assert c.deadline_to_sleep_time(1.1) == 0
28 assert c.deadline_to_sleep_time(1.2) == 0
29 assert c.deadline_to_sleep_time(1.3) > 999999
30
31 with pytest.raises(ValueError, match=r"^rate must be >= 0$"):
32 c.rate = -1
33 assert c.rate == 0
34
35 c.rate = 2
36 assert c.current_time() == 1.2
37 REAL_NOW += 1
38 assert c.current_time() == 3.2
39 assert c.deadline_to_sleep_time(3.1) == 0
40 assert c.deadline_to_sleep_time(3.2) == 0
41 assert c.deadline_to_sleep_time(4.2) == 0.5
42
43 c.rate = 0.5
44 assert c.current_time() == 3.2
45 assert c.deadline_to_sleep_time(3.1) == 0
46 assert c.deadline_to_sleep_time(3.2) == 0
47 assert c.deadline_to_sleep_time(4.2) == 2.0
48
49 c.jump(0.8)
50 assert c.current_time() == 4.0
51 REAL_NOW += 1
52 assert c.current_time() == 4.5
53
54 c2 = MockClock(rate=3)
55 assert c2.rate == 3
56 assert c2.current_time() < 10
57
58
59 async def test_mock_clock_autojump(mock_clock: MockClock) -> None:
60 assert mock_clock.autojump_threshold == inf
61
62 mock_clock.autojump_threshold = 0
63 assert mock_clock.autojump_threshold == 0
64
65 real_start = time.perf_counter()
66
67 virtual_start = _core.current_time()
68 for i in range(10):
69 print(f"sleeping {10 * i} seconds")
70 await sleep(10 * i)
71 print("woke up!")
72 assert virtual_start + 10 * i == _core.current_time()
73 virtual_start = _core.current_time()
74
75 real_duration = time.perf_counter() - real_start
76 print(f"Slept {10 * sum(range(10))} seconds in {real_duration} seconds")
77 assert real_duration < 1
78
79 mock_clock.autojump_threshold = 0.02
80 t = _core.current_time()
81 # this should wake up before the autojump threshold triggers, so time
82 # shouldn't change
83 await wait_all_tasks_blocked()
84 assert t == _core.current_time()
85 # this should too
86 await wait_all_tasks_blocked(0.01)
87 assert t == _core.current_time()
88
89 # set up a situation where the autojump task is blocked for a long long
90 # time, to make sure that cancel-and-adjust-threshold logic is working
91 mock_clock.autojump_threshold = 10000
92 await wait_all_tasks_blocked()
93 mock_clock.autojump_threshold = 0
94 # if the above line didn't take affect immediately, then this would be
95 # bad:
96 # ignore ASYNC116, not sleep_forever, trying to test a large but finite sleep
97 await sleep(100000) # noqa: ASYNC116
98
99
100 async def test_mock_clock_autojump_interference(mock_clock: MockClock) -> None:
101 mock_clock.autojump_threshold = 0.02
102
103 mock_clock2 = MockClock()
104 # messing with the autojump threshold of a clock that isn't actually
105 # installed in the run loop shouldn't do anything.
106 mock_clock2.autojump_threshold = 0.01
107
108 # if the autojump_threshold of 0.01 were in effect, then the next line
109 # would block forever, as the autojump task kept waking up to try to
110 # jump the clock.
111 await wait_all_tasks_blocked(0.015)
112
113 # but the 0.02 limit does apply
114 # ignore ASYNC116, not sleep_forever, trying to test a large but finite sleep
115 await sleep(100000) # noqa: ASYNC116
116
117
118 def test_mock_clock_autojump_preset() -> None:
119 # Check that we can set the autojump_threshold before the clock is
120 # actually in use, and it gets picked up
121 mock_clock = MockClock(autojump_threshold=0.1)
122 mock_clock.autojump_threshold = 0.01
123 real_start = time.perf_counter()
124 _core.run(sleep, 10000, clock=mock_clock)
125 assert time.perf_counter() - real_start < 1
126
127
128 async def test_mock_clock_autojump_0_and_wait_all_tasks_blocked_0(
129 mock_clock: MockClock,
130 ) -> None:
131 # Checks that autojump_threshold=0 doesn't interfere with
132 # calling wait_all_tasks_blocked with the default cushion=0.
133
134 mock_clock.autojump_threshold = 0
135
136 record = []
137
138 async def sleeper() -> None:
139 await sleep(100)
140 record.append("yawn")
141
142 async def waiter() -> None:
143 await wait_all_tasks_blocked()
144 record.append("waiter woke")
145 await sleep(1000)
146 record.append("waiter done")
147
148 async with _core.open_nursery() as nursery:
149 nursery.start_soon(sleeper)
150 nursery.start_soon(waiter)
151
152 assert record == ["waiter woke", "yawn", "waiter done"]
153
154
155 @slow
156 async def test_mock_clock_autojump_0_and_wait_all_tasks_blocked_nonzero(
157 mock_clock: MockClock,
158 ) -> None:
159 # Checks that autojump_threshold=0 doesn't interfere with
160 # calling wait_all_tasks_blocked with a non-zero cushion.
161
162 mock_clock.autojump_threshold = 0
163
164 record = []
165
166 async def sleeper() -> None:
167 await sleep(100)
168 record.append("yawn")
169
170 async def waiter() -> None:
171 await wait_all_tasks_blocked(1)
172 record.append("waiter done")
173
174 async with _core.open_nursery() as nursery:
175 nursery.start_soon(sleeper)
176 nursery.start_soon(waiter)
177
178 assert record == ["waiter done", "yawn"]
179
180
181 async def test_initialization_doesnt_mutate_runner() -> None:
182 before = (
183 GLOBAL_RUN_CONTEXT.runner.clock,
184 GLOBAL_RUN_CONTEXT.runner.clock_autojump_threshold,
185 )
186
187 MockClock(autojump_threshold=2, rate=3)
188
189 after = (
190 GLOBAL_RUN_CONTEXT.runner.clock,
191 GLOBAL_RUN_CONTEXT.runner.clock_autojump_threshold,
192 )
193 assert before == after