Skip to content

Commit a6610fc

Browse files
committed
Make autojump/autojump_threshold methods of abc.Clock
1 parent c0fa9bc commit a6610fc

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

src/trio/_abc.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from math import inf
34
import socket
45
from abc import ABC, abstractmethod
56
from typing import TYPE_CHECKING, Generic, TypeVar
@@ -65,6 +66,15 @@ def deadline_to_sleep_time(self, deadline: float) -> float:
6566
6667
"""
6768

69+
@property
70+
def autojump_threshold(self) -> float:
71+
return inf
72+
73+
def autojump(self) -> None:
74+
# If `autojump_threshold()` has the default implementation (returning `inf`),
75+
# this will never be called.
76+
raise NotImplementedError
77+
6878

6979
class Instrument(ABC): # noqa: B024 # conceptually is ABC
7080
"""The interface for run loop instrumentation.

src/trio/_core/_mock_clock.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,17 @@ def autojump_threshold(self, new_autojump_threshold: float) -> None:
105105
self._autojump_threshold = float(new_autojump_threshold)
106106
self._try_resync_autojump_threshold()
107107

108-
# runner.clock_autojump_threshold is an internal API that isn't easily
109-
# usable by custom third-party Clock objects. If you need access to this
110-
# functionality, let us know, and we'll figure out how to make a public
111-
# API. Discussion:
112-
#
113-
# https://github.com/python-trio/trio/issues/1587
114108
def _try_resync_autojump_threshold(self) -> None:
115109
try:
116110
runner = GLOBAL_RUN_CONTEXT.runner
117111
if runner.is_guest:
118112
runner.force_guest_tick_asap()
119113
except AttributeError:
120114
pass
121-
else:
122-
if runner.clock is self:
123-
runner.clock_autojump_threshold = self._autojump_threshold
124115

125116
# Invoked by the run loop when runner.clock_autojump_threshold is
126117
# exceeded.
127-
def _autojump(self) -> None:
118+
def autojump(self) -> None:
128119
statistics = _core.current_statistics()
129120
jump = statistics.seconds_to_next_deadline
130121
if 0 < jump < inf:
@@ -136,7 +127,7 @@ def _real_to_virtual(self, real: float) -> float:
136127
return self._virtual_base + virtual_offset
137128

138129
def start_clock(self) -> None:
139-
self._try_resync_autojump_threshold()
130+
pass
140131

141132
def current_time(self) -> float:
142133
return self._real_to_virtual(self._real_clock())

src/trio/_core/_run.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,9 +1816,6 @@ class Runner: # type: ignore[explicit-any]
18161816
trio_token: TrioToken | None = None
18171817
asyncgens: AsyncGenerators = attrs.Factory(AsyncGenerators)
18181818

1819-
# If everything goes idle for this long, we call clock._autojump()
1820-
clock_autojump_threshold: float = inf
1821-
18221819
# Guest mode stuff
18231820
is_guest: bool = False
18241821
guest_tick_scheduled: bool = False
@@ -2758,8 +2755,8 @@ def unrolled_run(
27582755
# We use 'elif' here because if there are tasks in
27592756
# wait_all_tasks_blocked, then those tasks will wake up without
27602757
# jumping the clock, so we don't need to autojump.
2761-
elif runner.clock_autojump_threshold < timeout:
2762-
timeout = runner.clock_autojump_threshold
2758+
elif runner.clock.autojump_threshold < timeout:
2759+
timeout = runner.clock.autojump_threshold
27632760
idle_primed = IdlePrimedTypes.AUTOJUMP_CLOCK
27642761

27652762
if "before_io_wait" in runner.instruments:
@@ -2810,8 +2807,7 @@ def unrolled_run(
28102807
break
28112808
else:
28122809
assert idle_primed is IdlePrimedTypes.AUTOJUMP_CLOCK
2813-
assert isinstance(runner.clock, _core.MockClock)
2814-
runner.clock._autojump()
2810+
runner.clock.autojump()
28152811

28162812
# Process all runnable tasks, but only the ones that are already
28172813
# runnable now. Anything that becomes runnable during this cycle

src/trio/_core/_tests/test_guest_mode.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ def current_time(self) -> float:
187187
def deadline_to_sleep_time(self, deadline: float) -> float:
188188
raise NotImplementedError()
189189

190+
@property
191+
def autojump_threshold(self) -> float:
192+
raise NotImplementedError()
193+
194+
def autojump(self) -> None:
195+
raise NotImplementedError()
196+
190197
def after_start_never_runs() -> None: # pragma: no cover
191198
pytest.fail("shouldn't get here")
192199

src/trio/_core/_tests/test_mock_clock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ async def waiter() -> None:
181181
async def test_initialization_doesnt_mutate_runner() -> None:
182182
before = (
183183
GLOBAL_RUN_CONTEXT.runner.clock,
184-
GLOBAL_RUN_CONTEXT.runner.clock_autojump_threshold,
184+
GLOBAL_RUN_CONTEXT.runner.clock.autojump_threshold,
185185
)
186186

187187
MockClock(autojump_threshold=2, rate=3)
188188

189189
after = (
190190
GLOBAL_RUN_CONTEXT.runner.clock,
191-
GLOBAL_RUN_CONTEXT.runner.clock_autojump_threshold,
191+
GLOBAL_RUN_CONTEXT.runner.clock.autojump_threshold,
192192
)
193193
assert before == after

0 commit comments

Comments
 (0)