Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3205.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Don't mutate the global runner when MockClock is created.
5 changes: 3 additions & 2 deletions src/trio/_core/_mock_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self, rate: float = 0.0, autojump_threshold: float = inf) -> None:
self._real_base = 0.0
self._virtual_base = 0.0
self._rate = 0.0
self._autojump_threshold = 0.0

# kept as an attribute so that our tests can monkeypatch it
self._real_clock = time.perf_counter

Expand Down Expand Up @@ -119,7 +119,8 @@ def _try_resync_autojump_threshold(self) -> None:
except AttributeError:
pass
else:
runner.clock_autojump_threshold = self._autojump_threshold
if runner.clock is self:
runner.clock_autojump_threshold = self._autojump_threshold

# Invoked by the run loop when runner.clock_autojump_threshold is
# exceeded.
Expand Down
16 changes: 16 additions & 0 deletions src/trio/_core/_tests/test_mock_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ... import _core
from .. import wait_all_tasks_blocked
from .._mock_clock import MockClock
from .._run import GLOBAL_RUN_CONTEXT
from .tutil import slow


Expand Down Expand Up @@ -175,3 +176,18 @@ async def waiter() -> None:
nursery.start_soon(waiter)

assert record == ["waiter done", "yawn"]


async def test_initialization_doesnt_mutate_runner() -> None:
before = (
GLOBAL_RUN_CONTEXT.runner.clock,
GLOBAL_RUN_CONTEXT.runner.clock_autojump_threshold,
)

MockClock(autojump_threshold=2, rate=3)

after = (
GLOBAL_RUN_CONTEXT.runner.clock,
GLOBAL_RUN_CONTEXT.runner.clock_autojump_threshold,
)
assert before == after
Loading