| 1 |
"""Indirection for time functions. |
| 2 |
|
| 3 |
We intentionally grab some "time" functions internally to avoid tests mocking "time" to affect |
| 4 |
pytest runtime information (issue #185). |
| 5 |
|
| 6 |
Fixture "mock_timing" also interacts with this module for pytest's own tests. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
import dataclasses |
| 12 |
from datetime import datetime |
| 13 |
from datetime import timezone |
| 14 |
from time import perf_counter |
| 15 |
from time import sleep |
| 16 |
from time import time |
| 17 |
from typing import TYPE_CHECKING |
| 18 |
|
| 19 |
|
| 20 |
if TYPE_CHECKING: |
| 21 |
from pytest import MonkeyPatch |
| 22 |
|
| 23 |
|
| 24 |
@dataclasses.dataclass(frozen=True) |
| 25 |
class Instant: |
| 26 |
""" |
| 27 |
Represents an instant in time, used to both get the timestamp value and to measure |
| 28 |
the duration of a time span. |
| 29 |
|
| 30 |
Inspired by Rust's `std::time::Instant`. |
| 31 |
""" |
| 32 |
|
| 33 |
# Creation time of this instant, using time.time(), to measure actual time. |
| 34 |
# Note: using a `lambda` to correctly get the mocked time via `MockTiming`. |
| 35 |
# pylint: disable-next=lambda-assignment |
| 36 |
time: float = dataclasses.field(default_factory=lambda: time(), init=False) # noqa: PLW0108 |
| 37 |
|
| 38 |
# Performance counter tick of the instant, used to measure precise elapsed time. |
| 39 |
# Note: using a `lambda` to correctly get the mocked time via `MockTiming`. |
| 40 |
perf_count: float = dataclasses.field( |
| 41 |
default_factory=lambda: perf_counter(), # noqa: PLW0108 |
| 42 |
init=False, |
| 43 |
) |
| 44 |
|
| 45 |
def elapsed(self) -> Duration: |
| 46 |
"""Measure the duration since `Instant` was created.""" |
| 47 |
return Duration(start=self, stop=Instant()) |
| 48 |
|
| 49 |
def as_utc(self) -> datetime: |
| 50 |
"""Instant as UTC datetime.""" |
| 51 |
return datetime.fromtimestamp(self.time, timezone.utc) |
| 52 |
|
| 53 |
|
| 54 |
@dataclasses.dataclass(frozen=True) |
| 55 |
class Duration: |
| 56 |
"""A span of time as measured by `Instant.elapsed()`.""" |
| 57 |
|
| 58 |
start: Instant |
| 59 |
stop: Instant |
| 60 |
|
| 61 |
@property |
| 62 |
def seconds(self) -> float: |
| 63 |
"""Elapsed time of the duration in seconds, measured using a performance counter for precise timing.""" |
| 64 |
return self.stop.perf_count - self.start.perf_count |
| 65 |
|
| 66 |
|
| 67 |
@dataclasses.dataclass |
| 68 |
class MockTiming: |
| 69 |
"""Mocks _pytest.timing with a known object that can be used to control timing in tests |
| 70 |
deterministically. |
| 71 |
|
| 72 |
pytest itself should always use functions from `_pytest.timing` instead of `time` directly. |
| 73 |
|
| 74 |
This then allows us more control over time during testing, if testing code also |
| 75 |
uses `_pytest.timing` functions. |
| 76 |
|
| 77 |
Time is static, and only advances through `sleep` calls, thus tests might sleep over large |
| 78 |
numbers and obtain accurate time() calls at the end, making tests reliable and instant.""" |
| 79 |
|
| 80 |
_current_time: float = datetime(2020, 5, 22, 14, 20, 50).timestamp() |
| 81 |
|
| 82 |
def sleep(self, seconds: float) -> None: |
| 83 |
self._current_time += seconds |
| 84 |
|
| 85 |
def time(self) -> float: |
| 86 |
return self._current_time |
| 87 |
|
| 88 |
def patch(self, monkeypatch: MonkeyPatch) -> None: |
| 89 |
# pylint: disable-next=import-self |
| 90 |
from _pytest import timing # noqa: PLW0406 |
| 91 |
|
| 92 |
monkeypatch.setattr(timing, "sleep", self.sleep) |
| 93 |
monkeypatch.setattr(timing, "time", self.time) |
| 94 |
monkeypatch.setattr(timing, "perf_counter", self.time) |
| 95 |
|
| 96 |
|
| 97 |
__all__ = ["perf_counter", "sleep", "time"] |