Skip to content

Commit f500503

Browse files
authored
Merge pull request #3205 from A5rocks/unwanted-mockclock-runner-mutation
Prevent MockClock from mutating the current runner
2 parents b137551 + 6831572 commit f500503

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

newsfragments/3205.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Don't mutate the global runner when MockClock is created.

src/trio/_core/_mock_clock.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def __init__(self, rate: float = 0.0, autojump_threshold: float = inf) -> None:
7070
self._real_base = 0.0
7171
self._virtual_base = 0.0
7272
self._rate = 0.0
73-
self._autojump_threshold = 0.0
73+
7474
# kept as an attribute so that our tests can monkeypatch it
7575
self._real_clock = time.perf_counter
7676

@@ -119,7 +119,8 @@ def _try_resync_autojump_threshold(self) -> None:
119119
except AttributeError:
120120
pass
121121
else:
122-
runner.clock_autojump_threshold = self._autojump_threshold
122+
if runner.clock is self:
123+
runner.clock_autojump_threshold = self._autojump_threshold
123124

124125
# Invoked by the run loop when runner.clock_autojump_threshold is
125126
# exceeded.

src/trio/_core/_tests/test_mock_clock.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ... import _core
99
from .. import wait_all_tasks_blocked
1010
from .._mock_clock import MockClock
11+
from .._run import GLOBAL_RUN_CONTEXT
1112
from .tutil import slow
1213

1314

@@ -175,3 +176,18 @@ async def waiter() -> None:
175176
nursery.start_soon(waiter)
176177

177178
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

0 commit comments

Comments
 (0)