Skip to content
Closed
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
2 changes: 2 additions & 0 deletions newsfragments/3578.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`trio.testing.MockClock.jump` now rejects NaN with `ValueError` without
changing the clock's time.
4 changes: 3 additions & 1 deletion src/trio/_core/_mock_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ def jump(self, seconds: float) -> None:
seconds (float): the number of seconds to jump the clock forward.

Raises:
ValueError: if you try to pass a negative value for ``seconds``.
ValueError: if ``seconds`` is negative or NaN.

"""
if isnan(seconds):
raise ValueError("seconds must not be NaN")
if seconds < 0:
raise ValueError("time can't go backwards")
self._virtual_base += seconds
18 changes: 18 additions & 0 deletions src/trio/_core/_tests/test_mock_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ def test_mock_clock() -> None:
assert c2.current_time() < 10


@pytest.mark.parametrize("rate", [0.0, 2.0])
def test_mock_clock_jump_nan_preserves_time(rate: float) -> None:
clock = MockClock()
clock._real_clock = lambda: 123.0
clock.rate = rate
clock.jump(1.2)

with pytest.raises(ValueError, match=r"^seconds must not be NaN$"):
clock.jump(float("nan"))

assert clock.current_time() == 1.2
clock.jump(0)
assert clock.current_time() == 1.2
clock.jump(0.8)
assert clock.current_time() == 2.0
assert clock.rate == rate


async def test_mock_clock_autojump(mock_clock: MockClock) -> None:
assert mock_clock.autojump_threshold == inf

Expand Down
Loading