diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 00835fb..d14deb3 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -24,8 +24,12 @@ jobs: strategy: fail-fast: false matrix: - python-version: [ "3.9", "3.10", "3.11", "3.12", "3.13" ] - name: Python ${{ matrix.python-version }} + python-version: [ "3.9", "3.10", "3.11", "3.12" ] + include: + - python-version: "3.13" + - python-version: "3.13" + install-extras: "pytest-asyncio<1.0.0" + name: Python ${{ matrix.python-version }}${{ matrix.install-extras && ', ' || '' }}${{ matrix.install-extras }} runs-on: ubuntu-24.04 timeout-minutes: 5 steps: @@ -36,6 +40,8 @@ jobs: - run: pip install -r requirements.txt - run: pip install -e . + - run: pip install "${{ matrix.install-extras }}" + if: ${{ matrix.install-extras }} - run: pytest --color=yes --cov=looptime --cov-branch - name: Publish coverage to Coveralls.io diff --git a/README.md b/README.md index ab60d49..96f6705 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,6 @@ [![CI](https://github.com/nolar/looptime/workflows/Thorough%20tests/badge.svg)](https://github.com/nolar/looptime/actions/workflows/thorough.yaml) [![codecov](https://codecov.io/gh/nolar/looptime/branch/main/graph/badge.svg)](https://codecov.io/gh/nolar/looptime) [![Coverage Status](https://coveralls.io/repos/github/nolar/looptime/badge.svg?branch=main)](https://coveralls.io/github/nolar/looptime?branch=main) -[![Total alerts](https://img.shields.io/lgtm/alerts/g/nolar/looptime.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/nolar/looptime/alerts/) -[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/nolar/looptime.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/nolar/looptime/context:python) [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit) ## What? @@ -19,10 +17,10 @@ The effects of time removal can be seen from both sides: This hides the code overhead and network latencies from the time measurements, making the loop time sharply and predictably advancing in configured steps. -* From the **observer's (i.e. your) point of view,** +* From the **observer's (i.e. your personal) point of view,** all activities of the event loop, such as sleeps, events/conditions waits, timeouts, "later" callbacks, happen in near-zero amount of the real time - (above the usual code overhead). + (due to the usual code execution overhead). This speeds up the execution of tests without breaking the tests' time-based design, even if they are designed to run in seconds or minutes. @@ -59,19 +57,19 @@ at the same time: * One is for the coroutine-under-test which moves between states in the background. * Another one is for the test itself, which controls the flow - of that coroutine-under-test: it sets events, injects data, etc. + of that coroutine-under-test: it schedules events, injects data, etc. In textbook cases with simple coroutines that are more like regular functions, it is possible to design a test so that it runs straight to the end in one hop — with all the preconditions set and data prepared in advance in the test setup. -However, in real-world cases, the tests often must verify that +However, in the real-world cases, the tests often must verify that the coroutine stops at some point, waits for a condition for some limited time, and then passes or fails. The problem is often "solved" by mocking the low-level coroutines of sleep/wait that we expect the coroutine-under-test to call. But this violates the main -principle of good unit-tests: test the promise, not the implementation. +principle of good unit-tests: **test the promise, not the implementation.** Mocking and checking the low-level coroutines is based on the assumptions of how the coroutine is implemented internally, which can change over time. Good tests do not change on refactoring if the protocol remains the same. @@ -79,8 +77,8 @@ Good tests do not change on refactoring if the protocol remains the same. Another (straightforward) approach is to not mock the low-level routines, but to spend the real-world time, just in short bursts as hard-coded in the test. Not only it makes the whole test-suite slower, it also brings the execution -time close to the values where the code overhead affects the timing -and makes it difficult to assert on the coroutine's pure time. +time close to the values where the code overhead or measurement errors affect +the timing, which makes it difficult to assert on the coroutine's pure time. ## Solution @@ -232,13 +230,26 @@ async def test_me(): `start` (`float` or `None`, or a no-argument callable that returns the same) is the initial time of the event loop. -If it is callable, it is invoked once per event loop to get the value: +If it is a callable, it is invoked once per event loop to get the value: e.g. `start=time.monotonic` to align with the true time, or `start=lambda: random.random() * 100` to add some unpredictability. `None` is treated the same as `0.0`. -The default is `0.0`. +The default is `0.0`. For reusable event loops, the default is to keep +the time untouched, which means `0.0` or the explicit value for the first test, +but then an ever-increasing value for the 2nd, 3rd, and further tests. + +Note: pytest-asyncio 1.0.0+ introduced event loops with higher scopes, +e.g. class-, module-, packages-, session-scoped event loops used in tests. +Such event loops are reused, so their time continues growing through many tests. +However, if the test is explicitly configured with the start time, +that time is enforced to the event loop when the test function starts — +to satisfy the clearly declared intentions — even if the time moves backwards, +which goes against the nature of the time itself (monotonically growing). +This might lead to surprises in time measurements outside of the test, +e.g. in fixtures: the code durations can become negative, or the events can +happen (falsely) before they are scheduled (loop-clock-wise). Be careful. ### The end of time @@ -255,6 +266,8 @@ e.g. with `asyncio.sleep(0)`, simple `await` statements, etc. If set to `None`, there is no end of time, and the event loop runs as long as needed. Note: `0` means ending the time immediately on start. +Be careful with the explicit ending time in higher-scoped event loops +of pytest-asyncio>=1.0.0, since they time increases through many tests. If it is a callable, it is called once per event loop to get the value: e.g. `end=lambda: time.monotonic() + 10`. @@ -512,6 +525,63 @@ For example, with the resolution `0.001`, the time everything smaller than `0.001` becomes `0` and probably misbehaves._ +### Time magic coverage + +The time compaction magic is enabled only for the duration of the test, +i.e. the test function — but not the fixtures. +The fixtures run in the real (wall-clock) time. + +The options (including the force starting time) are applied at the test function +starting moment, not when it is setting up the fixtures (even function-scoped). + +This is caused by a new concept of multiple co-existing event loops +in pytest-asyncio>=1.0.0: + +- It is unclear which options to apply to higher-scoped fixtures + used by many tests, which themselves use higher-scoped event loops — + especially in selective partial runs. Technically, it is the 1st test, + with the options of 2nd and further tests simply ignored. +- It is impossible to guess which event loop will be the running loop + in the test until we reach the test itself, i.e. we do not know this + when setting up the fixtures, even function-scoped fixtures. +- There is no way to cover the fixture teardown (no hook in pytest), + only for the fixture setup and post-teardown cleanup. + +As such, this functionality (covering of function-scoped fixtures) +was abandoned — since it was never promised, tested, or documented — +plus an assumption that it was never used by anyone (it should not be). +It was rather a side effect of the previous implemention, +which is not available or possible anymore. + + +### pytest-asyncio>=1.0.0 + +As it is said above, pytest-asyncio>=1.0.0 introduced several co-existing +event loops of different scopes. The time compaction in these event loops +is NOT activated. Only the running loop of the test function is activated. + +Configuring and activating multiple co-existing event loops brings a few +conceptual challenges, which require a good sample case to look into, +and some time to think. + +Would you need time compaction in your fixtures of higher scopes, +do it explicitly: + +```python +import asyncio +import pytest + +@pytest.fixture +async def fixt(): + loop = asyncio.get_running_loop() + loop.setup_looptime(start=123, end=456) + with loop.looptime_enabled(): + await do_things() +``` + +There is #11 to add a feature to do this automatically, but it is not yet done. + + ## Extras ### Chronometers @@ -605,6 +675,8 @@ the loop time also includes the time of all fixtures setups. Do you use a custom event loop? No problem! Create a test-specific descendant with the provided mixin — and it will work the same as the default event loop. +For `pytest-asyncio<1.0.0`: + ```python import looptime import pytest @@ -620,6 +692,29 @@ def event_loop(): return LooptimeCustomEventLoop() ``` +For `pytest-asyncio>=1.0.0`: + +```python +import asyncio +import looptime +import pytest +from wherever import CustomEventLoop + + +class LooptimeCustomEventLoop(looptime.LoopTimeEventLoop, CustomEventLoop): + pass + + +class LooptimeCustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy): + def new_event_loop(self): + return LooptimeCustomEventLoop() + + +@pytest.fixture(scope='session') +def event_loop_policy(): + return LooptimeCustomEventLoopPolicy() +``` + Only selector-based event loops are supported: the event loop must rely on `self._selector.select(timeout)` to sleep for `timeout` true-time seconds. Everything that inherits from `asyncio.BaseEventLoop` should work. @@ -627,6 +722,8 @@ Everything that inherits from `asyncio.BaseEventLoop` should work. You can also patch almost any event loop class or event loop object the same way as `looptime` does that (via some dirty hackery): +For `pytest-asyncio<1.0.0`: + ```python import asyncio import looptime @@ -639,6 +736,25 @@ def event_loop(): return looptime.patch_event_loop(loop) ``` +For `pytest-asyncio>=1.0.0`: + +```python +import asyncio +import looptime +import pytest + + +class LooptimeEventLoopPolicy(asyncio.DefaultEventLoopPolicy): + def new_event_loop(self): + loop = super().new_event_loop() + return looptime.patch_event_loop(loop) + + +@pytest.fixture(scope='session') +def event_loop_policy(): + return LooptimeEventLoopPolicy() +``` + `looptime.make_event_loop_class(cls)` constructs a new class that inherits from the referenced class and the specialised event loop class mentioned above. The resulting classes are cached, so it can be safely called multiple times. diff --git a/looptime/__init__.py b/looptime/__init__.py index 0686424..3882114 100644 --- a/looptime/__init__.py +++ b/looptime/__init__.py @@ -1,10 +1,11 @@ from .chronometers import Chronometer -from .loops import IdleTimeoutError, LoopTimeEventLoop, LoopTimeoutError +from .loops import IdleTimeoutError, LoopTimeEventLoop, LoopTimeoutError, TimeWarning from .patchers import make_event_loop_class, new_event_loop, patch_event_loop, reset_caches from .timeproxies import LoopTimeProxy __all__ = [ 'Chronometer', + 'TimeWarning', 'LoopTimeProxy', 'IdleTimeoutError', 'LoopTimeoutError', diff --git a/looptime/loops.py b/looptime/loops.py index fe4a6d2..e99162f 100644 --- a/looptime/loops.py +++ b/looptime/loops.py @@ -1,10 +1,12 @@ from __future__ import annotations import asyncio +import contextlib import selectors import time +import warnings import weakref -from typing import TYPE_CHECKING, Any, Callable, MutableSet, TypeVar, cast, overload +from typing import TYPE_CHECKING, Any, Callable, Iterator, MutableSet, TypeVar, cast, overload _T = TypeVar('_T') @@ -16,6 +18,11 @@ AnyTask = asyncio.Task +class TimeWarning(UserWarning): + """Issued when the loop time moves backwards, violating its monotonicity.""" + pass + + class LoopTimeoutError(asyncio.TimeoutError): """A special kind of timeout when the loop's time reaches its end.""" pass @@ -61,6 +68,7 @@ def setup_looptime( idle_step: float | None = None, idle_timeout: float | None = None, noop_cycles: int = 42, + _enabled: bool | None = None, # None means do nothing ) -> None: """ Set all the fake-time fields and patch the i/o selector. @@ -69,9 +77,33 @@ def setup_looptime( when the mixin/class is injected into the existing event loop object. In that case, the object is already initialised except for these fields. """ + new_time: float | None = start() if callable(start) else start + end_time: float | None = end() if callable(end) else end + old_time: float | None + try: + # NB: using the existing (old) reciprocal! + old_time = self.__int2time(self.__now) + except AttributeError: # initial setup: either reciprocals or __now are absent + old_time = None + new_time = float(new_time) if new_time is not None else None + + # If it is the 2nd or later setup, double-check on time monotonicity. + # In some configurations, this waring might raise an error and fail the test. + # In that case, the time must not be changed for the next test. + if old_time is not None and new_time is not None and new_time < old_time: + warnings.warn( + f"The time of the event loop moves backwards from {old_time} to {new_time}," + " thus breaking the monotonicity of time. This is dangerous!" + " Perhaps, caused by reusing a higher-scope event loop in tests." + " Revise the scopes of fixtures & event loops." + " Remove the start=… kwarg and rely on arbitrary time values." + " Migrate from `loop.time()` to the `looptime` numeric fixture.", + TimeWarning, + ) + self.__resolution_reciprocal: int = round(1/resolution) - self.__now: int = self.__time2int(start() if callable(start) else start) or 0 - self.__end: int | None = self.__time2int(end() if callable(end) else end) + self.__now: int = self.__time2int(new_time or old_time) or 0 + self.__end: int | None = self.__time2int(end_time) self.__idle_timeout: int | None = self.__time2int(idle_timeout) self.__idle_step: int | None = self.__time2int(idle_step) @@ -85,6 +117,13 @@ def setup_looptime( self.__sync_clock: Callable[[], float] = time.perf_counter self.__sync_ts: float | None = None # system/true-time clock timestamp + try: + self.__enabled # type: ignore + except AttributeError: + self.__enabled = _enabled if _enabled is not None else True # old behaviour + else: + self.__enabled = _enabled if _enabled is not None else self.__enabled + # TODO: why do we patch the selector as an object while the event loop as a class? # this should be the same patching method for both. try: @@ -93,6 +132,24 @@ def setup_looptime( self.__original_select = self._selector.select self._selector.select = self.__replaced_select # type: ignore + @property + def looptime_on(self) -> bool: + return bool(self.__enabled) + + @contextlib.contextmanager + def looptime_enabled(self) -> Iterator[None]: + """ + Temporarily enable the time compaction, restore the normal mode on exit. + """ + if self.__enabled: + raise RuntimeError('Looptime mode is already enabled. Entered twice? Avoid this!') + old_enabled = self.__enabled + self.__enabled = True + try: + yield + finally: + self.__enabled = old_enabled + def time(self) -> float: return self.__int2time(self.__now) @@ -111,6 +168,16 @@ def __replaced_select(self, timeout: float | None) -> list[tuple[Any, Any]]: if ready: pass + # If nothing to do right now, and the time is not compacted, truly sleep as requested. + # Move the fake time by the exact real time spent in this wait (±discrepancies). + elif not self.__enabled: + t0 = time.monotonic() + ready = self.__original_select(timeout=timeout) + t1 = time.monotonic() + + # If timeout=None, it never exists until ready. This timeout check is for typing only. + self.__now += self.__time2int(t1 - t0 if ready or timeout is None else timeout) + # Regardless of the timeout, if there are executors sync futures, we move the time in steps. # The timeout (if present) can limit the size of the step, but not the logic of stepping. # Generally, external things (threads) take some time (e.g. for thread spawning). diff --git a/looptime/plugin.py b/looptime/plugin.py index c2ca31f..0d8fd4b 100644 --- a/looptime/plugin.py +++ b/looptime/plugin.py @@ -1,15 +1,146 @@ +""" +Integrations with pytest & pytest-asyncio (irrelevant for other frameworks). + + +IMPLEMENTATION DETAIL - reverse time movement +============================================= + +Problem +------- + +Pytest-asyncio>=1.0.0 has removed the ``event_loop`` fixture and fully switched +to the ``event_loop_policy`` (session-scoped) plus several independent fixtures: +session-, package-, module-, class-, function-scoped. It means that a test +might use any of these fixtures or several or all of them at the same time. + +As a result, our previous assumption that every test & all its fixtures run +in its own isolated short-lived event loop, is now broken: + +- A single event loop can be shared by multiple (but not all) tests. +- A single test can be spread over multiple (but not all) event loops. + +An classic example: + +- A session-scoped fixture ``server`` starts a port listener & an HTTP server. +- A module-scoped fixture ``data`` populates the server via POST requests. +- A few function-scoped tests access & assert these data via GET requests. +- Other tests verify the database and do not touch the event loops. + +Looptime suggests setting the start time of the event loop or expect it to be 0. +This simplifies assertions, scheduling of events, callbacks, and other triggers. + +As a result, a long-living event loop might see the time set/reset by tests, +and in most cases, it will be moving the time backwards. + +Time, by its nature, is supposed to be monotonic (but can be non-linear), +specifically positively monotonic — always growing, never going backwards. + + +Solution +-------- + +We sacrifice this core property of time for the sake of simplicity of tests. + +So we should be prepared for the consequences when all hell breaks loose. E.g.: +the callbacks and other events triggering before they were set up (clock-wise); +the durations of activities being negative; so on. + +Either way, we set the loop time as requested, but with a few nuances: + +1. If the start time is NOT explicitly defined, for higher-scoped event loops, + we keep the time as is for every test and let it flow monotonically. + Previously, the higher-scoped fixtures did not exist, so nothing breaks. + +2. If the start time is explicitly defined and is in the future, move the time + forwards as specified — indistinguishable from the previous behaviour + (except there could be artifacts from the previous tests in the loop). + +3. If the start time is explicitly defined and is in the past, issue a warning + of class ``looptime.TimeWarning``, which inherits from ``UserWarning``, + indicating a user-side misbehaviour & broken test-suite design. + It can be configured to raise an error (strict mode), or be ignored. + +This ensures the most possible backwards compatibility with the old behavior +with a few truthworthy assumptions in mind: + +- Fixtures do not measure time and do not rely on time. Their purpose should be + preparing the environment, filling the data. Only the tests can move the time. + As such, they will not suffer much from the backward time movements. + +- Old-style tests typically use the function scope & the function-scoped loop, + which has the time set at 0 by default. No changes to the previous behaviour. + +- New-style tests that run in higher-scoped loops (a new pytest-asyncio feature) + should not rely on an isolated event loop and the time starting with 0, + and should be clearly prepared for the backward time movements + if they express the intention to reset the start time of the event loop. + Such tests should measure the "since" and "till" and assert on the difference. + + +IMPLEMENTATION DETAIL — patching always, activating on-demand +============================================================= + +In order to make event loops compatible with looptime, they (the event loops) +MUST be patched at creation, not in the middle of a runtime when it reaches +the looptime-enabled tests (consider a global session-scoped event loop here). + +Therefore, we patch ALL the implicit event loops of pytest-asyncio, regardless +of whether they are supposed to be used or not. They are disabled (inactive) +initally, i.e. their time flows normally, using the wall-clock (true) time. + +We then activate the looptime magic on demand for those tests that need it, +and only when needed (i.e. when requested/configured/marked). + +We only activate the time magic on the running loop of the test, and only +during the test execution. We do not compact the time of the event loops +used in fixtures, even when the fixtures use the same-scoped event loop. + +(This might be a breaking change. See the assumptions above for the rationale.) + +Even for the lowest "function" scope, we cannot patch-and-activate it only once +at creation, since at the time of the event loop setup (creation), +we do not know which event loop will be the running loop of the test. +This affects to which loop the configured options should be applied. + +We only know this when we reach the test. +We then apply the options, and activate the pre-patched running event loop. +""" from __future__ import annotations import asyncio -from typing import Any, cast +import warnings +from typing import Any +import _pytest.nodes import pytest from looptime import loops, patchers, timeproxies -@pytest.fixture() -def looptime() -> timeproxies.LoopTimeProxy: +# Critical implementation details: It MUST be sync! It CANNOT be async! +# It might seem that the easiest way to implement the ``looptime`` fixture is +# to make it ``async def`` and get the running loop inside. This does NOT work. +# When a function-scoped fixture is used in any higher-scoped test, it degrades +# the test from its scope to the function scope and breaks the test design. +# See an example at https://github.com/pytest-dev/pytest-asyncio/issues/1142. +# As such, the fixture MUST be synchronous (simple ``def``). As a result, +# the fixture CANNOT get a running loop, because there is no running loop. +# We take the running loop inside the time-measuring proxy at runtime. +@pytest.fixture +def looptime(request: pytest.FixtureRequest) -> timeproxies.LoopTimeProxy: + """ + The event loop time for assertions. + + The fixture's numeric value is the loop time as a number of seconds + since the "time zero", which is usuaully the creation time + of the event loop, but can be adjusted by the ``start=…`` option. + + - It can be used in assertions & comparisons (``==``, ``<=``, etc). + - It can also be used in simple math (additions, substractions, etc). + - It can be converted to ``int()`` or ``float()``. + + It is an equivalent of a more wordy ``asyncio.get_running_loop().time()``. + """ return timeproxies.LoopTimeProxy() @@ -25,27 +156,161 @@ def pytest_addoption(parser: Any) -> None: help="Run unmarked tests with the fake loop time by default.") +EventLoopScopes = dict[str, list[str]] # {fixture_name -> [outer_scopes, …, innermost_scope]} +EVENT_LOOP_SCOPES = pytest.StashKey[EventLoopScopes]() + + @pytest.hookimpl(wrapper=True) -def pytest_fixture_setup(fixturedef: Any, request: Any) -> Any: +def pytest_fixture_setup(fixturedef: pytest.FixtureDef[Any], request: pytest.FixtureRequest) -> Any: + # Setup as usual. We do the magic only afterwards, when we have the event loop created. result = yield + # Only do the magic if in the area of our interest & only for fixtures making the event loops. + if _should_patch(fixturedef, request) and isinstance(result, asyncio.BaseEventLoop): + + # Populate the helper mapper of names-to-scopes, as used in the test hook below. + if EVENT_LOOP_SCOPES not in request.session.stash: + request.session.stash[EVENT_LOOP_SCOPES] = {} + event_loop_scopes: EventLoopScopes = request.session.stash[EVENT_LOOP_SCOPES] + event_loop_scopes.setdefault(fixturedef.argname, []).append(fixturedef.scope) + + # Patch the event loop at creation — even if unused and not enabled. We cannot patch later + # in the middle of the run: e.g. for a session-scoped loop used in a few tests out of many. + # NB: For the lowest "function" scope, we still cannot decide which options to use, since + # we do not know yet if it will be the running loop or not — so we cannot optimize here + # in order to patch-and-configure only once; we must patch here & configure+activate later. + result = patchers.patch_event_loop(result, _enabled=False) + + return result + + +@pytest.hookimpl(wrapper=True) +def pytest_fixture_post_finalizer(fixturedef: pytest.FixtureDef[Any], request: pytest.FixtureRequest) -> Any: + # Cleanup the helper mapper of the fixture's names-to-scopes, as used in the test-running hook. + # Internal consistency check: some cases should not happen, but we do not fail if they do. + if EVENT_LOOP_SCOPES in request.session.stash: + event_loop_scopes: EventLoopScopes = request.session.stash[EVENT_LOOP_SCOPES] + if fixturedef.argname not in event_loop_scopes: + warnings.warn( + f"Fixture {fixturedef.argname!r} not found in the cache of scopes." + f" Report as a bug, please add a reproducible snippet.", + RuntimeWarning, + ) + elif not event_loop_scopes[fixturedef.argname]: + warnings.warn( + f"Fixture {fixturedef.argname!r} has the empty cache of scopes." + f" Report as a bug, please add a reproducible snippet.", + RuntimeWarning, + ) + elif event_loop_scopes[fixturedef.argname][-1] != fixturedef.scope: + warnings.warn( + f"Fixture {fixturedef.argname!r} has the broken cache of scopes:" + f" {event_loop_scopes[fixturedef.argname]!r}, expecting {fixturedef.scope!r}" + f" Report as a bug, please add a reproducible snippet.", + RuntimeWarning, + ) + else: + event_loop_scopes[fixturedef.argname][-1:] = [] + + # Go as usual. + return (yield) + + +# This hook is the latest (deepest) possible entrypoint before diving into the test function itself, +# with all the fixtures executed earlier, so that their setup time is not taken into account. +# Here, we know the actual running loop (out of many) chosen by pytest-asyncio & its marks/configs. +# The alternatives to consider — the subtle differences are unclear to me for now: +# - pytest_pyfunc_call(pyfuncitem) +# - pytest_runtest_call(item) +# - pytest_runtest_setup(item), wrapped as @hookimpl(trylast=True) +@pytest.hookimpl(wrapper=True) +def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> Any: + + # Get the running loop from the pre-populated & pre-resolved fixtures (done in the setup stage). + # This includes all the auto-used fixtures, but NOT the dynamic `getfixturevalue(…)` ones. + # Alternatively, use the private `pyfuncitem._request.getfixturevalue(…)`, though this is hacky. + funcargs: dict[str, Any] = pyfuncitem.funcargs + if 'event_loop_policy' in funcargs: # pytest-asyncio>=1.0.0 + # This can be ANY event loop of ANY declared scope of pytest-asyncio. + policy: asyncio.AbstractEventLoopPolicy = funcargs['event_loop_policy'] + running_loop = policy.get_event_loop() + elif 'event_loop' in funcargs: # pytest-asyncio<1.0.0 + # The hook itself has NO "running" loop — because it is sync, not async. + running_loop = funcargs['event_loop'] + else: # not pytest-asyncio? not our business! + return (yield) + + # The event loop is not patched? We are doomed to fail, so let it run somehow on its own. + # This might happen if the custom event loop policy was set not by pytest-asyncio. + if not isinstance(running_loop, loops.LoopTimeEventLoop): + return (yield) + + # If not enabled/enforced for this test, even if the event loop is patched, let it run as usual. + options: dict[str, Any] | None = _get_options(pyfuncitem) + if options is None: + return (yield) + + # Finally, if enabled/enforced, activate the magic and run the test in the compacted time mode. + # We only activate the running loop for the test, not the other event loops used in fixtures. + running_loop.setup_looptime(**options) + with running_loop.looptime_enabled(): + return (yield) + + +def _should_patch(fixturedef: pytest.FixtureDef[Any], request: pytest.FixtureRequest) -> bool: + """ + Check if the fixture should be patched (in case it is an event loop). + + Only patch the implicit (hidden) event loops and their user-side overrides. + They are declared as internal with underscored names, but nevertheless. + Example implicit names: ``_session_event_loop`` … ``_function_event_loop``. + + We do not intercept arbitrary fixtures or event loops of unknown plugins. + Custom event loops can be patched explicitly if needed. + """ + # pytest-asyncio<1.0.0 exposed the specific fixture; deprecated since >=0.23.0, removed >=1.0.0. if fixturedef.argname == "event_loop": - loop = cast(asyncio.BaseEventLoop, result) - if not isinstance(loop, loops.LoopTimeEventLoop): + return True - # True means implicitly on; False means explicitly off; None means "only if marked". - option: bool | None = request.config.getoption('looptime') + # pytest-asyncio>=1.0.0 exposes several event loops, one per scope, all hidden in the module. + asyncio_plugin = request.config.pluginmanager.getplugin("asyncio") # a module object + asyncio_names: set[str] = { + name for name in dir(asyncio_plugin) if _is_fixture(getattr(asyncio_plugin, name)) + } + asyncio_module = asyncio_plugin.__name__ + fixture_module = fixturedef.func.__module__ + should_patch = fixture_module == asyncio_module or fixturedef.argname in asyncio_names + return should_patch - markers = list(request.node.iter_markers('looptime')) - enabled = bool((markers or option is True) and option is not False) # but not None! - options = {} - for marker in reversed(markers): - options.update(marker.kwargs) - enabled = bool(marker.args[0]) if marker.args else enabled - if enabled: - patched_loop = patchers.patch_event_loop(loop) - patched_loop.setup_looptime(**options) - result = patched_loop +def _is_fixture(obj: Any) -> bool: + # Any of these internal names can be moved or renamed any time. Do our best to guess. + import _pytest.fixtures - return result + try: + if isinstance(obj, _pytest.fixtures.FixtureFunctionDefinition): + return True + except AttributeError: + pass + try: + if isinstance(obj, _pytest.fixtures.FixtureFunctionMarker): + return True + except AttributeError: + pass + return False + + +def _get_options(node: _pytest.nodes.Node) -> dict[str, Any] | None: + """Combine all the declared looptime options; None for disabled.""" + + # True means implicitly on; False means explicitly off; None means "only if marked". + flag: bool | None = node.config.getoption('looptime') + + markers = list(node.iter_markers('looptime')) + enabled: bool = bool((markers or flag is True) and not flag is False) + options: dict[str, Any] = {} + for marker in reversed(markers): + options.update(marker.kwargs) + enabled = bool(marker.args[0]) if marker.args else enabled + + return options if enabled else None diff --git a/requirements.txt b/requirements.txt index ec4f742..7ad3a82 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,6 +9,6 @@ isort mypy==1.16.0 pre-commit pytest -pytest-asyncio<1.0.0 +pytest-asyncio pytest-cov pytest-mock diff --git a/tests/test_chronometers.py b/tests/test_chronometers.py index ae6a2ed..8597a5d 100644 --- a/tests/test_chronometers.py +++ b/tests/test_chronometers.py @@ -52,7 +52,8 @@ async def test_async_context_manager(): @pytest.mark.asyncio @pytest.mark.looptime(start=100) -async def test_readme_example(chronometer, event_loop): +async def test_readme_example(chronometer): + event_loop = asyncio.get_running_loop() with chronometer, looptime.Chronometer(event_loop.time) as loopometer: await asyncio.sleep(1) await asyncio.sleep(1) diff --git a/tests/test_patching.py b/tests/test_patching.py index bebe423..b637922 100644 --- a/tests/test_patching.py +++ b/tests/test_patching.py @@ -1,5 +1,7 @@ import asyncio +import pytest + import looptime @@ -33,6 +35,7 @@ def test_patching_initialises(): def test_initial_time_is_customized(): loop = looptime.patch_event_loop(asyncio.new_event_loop(), start=123) + assert loop.looptime_on assert loop.time() == 123 @@ -41,4 +44,49 @@ def test_new_event_loop_is_patched_out_of_the_box(): loop = looptime.new_event_loop(start=123) assert isinstance(loop, default_loop_cls) assert isinstance(loop, looptime.LoopTimeEventLoop) + assert loop.looptime_on assert loop.time() == 123 + + +def test_patching_activates_by_default(): + old_loop = MyEventLoop() + new_loop = looptime.patch_event_loop(old_loop) + assert new_loop.looptime_on + + +def test_patching_disabled_if_specified(): + old_loop = MyEventLoop() + new_loop = looptime.patch_event_loop(old_loop, _enabled=False) + assert not new_loop.looptime_on + + +def test_contextual_activation(): + old_loop = MyEventLoop() + new_loop = looptime.patch_event_loop(old_loop, _enabled=False) + with new_loop.looptime_enabled(): + assert new_loop.looptime_on + + +def test_double_activation_protection(): + old_loop = MyEventLoop() + new_loop = looptime.patch_event_loop(old_loop, _enabled=False) + with new_loop.looptime_enabled(): + with pytest.raises(RuntimeError, match=r"already enabled"): + with new_loop.looptime_enabled(): + pass + + +@pytest.mark.parametrize('state', [True, False]) +def test_setup_preserves_the_old_state(state: bool): + old_loop = MyEventLoop() + new_loop = looptime.patch_event_loop(old_loop, _enabled=state) + new_loop.setup_looptime() + assert new_loop.looptime_on if state else not new_loop.looptime_on + + +@pytest.mark.parametrize('state', [True, False]) +def test_setup_overwrites_the_old_state(state: bool): + old_loop = MyEventLoop() + new_loop = looptime.patch_event_loop(old_loop, _enabled=state) + new_loop.setup_looptime(_enabled=not state) + assert not new_loop.looptime_on if state else new_loop.looptime_on diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 2d30b8e..0094d1a 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -3,29 +3,38 @@ def test_cli_default_mode(testdir): testdir.makepyfile(""" + import asyncio import looptime import pytest @pytest.mark.asyncio - async def test_normal(event_loop): - assert not isinstance(event_loop, looptime.LoopTimeEventLoop) + async def test_normal(): + event_loop = asyncio.get_running_loop() + assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert not event_loop.looptime_on @pytest.mark.asyncio @pytest.mark.looptime - async def test_marked(event_loop): + async def test_marked(): + event_loop = asyncio.get_running_loop() assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert event_loop.looptime_on assert event_loop.time() == 0 @pytest.mark.asyncio @pytest.mark.looptime(start=123) - async def test_configured(event_loop): + async def test_configured(): + event_loop = asyncio.get_running_loop() assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert event_loop.looptime_on assert event_loop.time() == 123 @pytest.mark.asyncio @pytest.mark.looptime(False) - async def test_disabled(event_loop): - assert not isinstance(event_loop, looptime.LoopTimeEventLoop) + async def test_disabled(): + event_loop = asyncio.get_running_loop() + assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert not event_loop.looptime_on """) result = testdir.runpytest() result.assert_outcomes(passed=4) @@ -33,30 +42,39 @@ async def test_disabled(event_loop): def test_cli_option_enabled(testdir): testdir.makepyfile(""" + import asyncio import looptime import pytest @pytest.mark.asyncio - async def test_normal(event_loop): + async def test_normal(): + event_loop = asyncio.get_running_loop() assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert event_loop.looptime_on assert event_loop.time() == 0 @pytest.mark.asyncio @pytest.mark.looptime - async def test_marked(event_loop): + async def test_marked(): + event_loop = asyncio.get_running_loop() assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert event_loop.looptime_on assert event_loop.time() == 0 @pytest.mark.asyncio @pytest.mark.looptime(start=123) - async def test_configured(event_loop): + async def test_configured(): + event_loop = asyncio.get_running_loop() assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert event_loop.looptime_on assert event_loop.time() == 123 @pytest.mark.asyncio @pytest.mark.looptime(False) - async def test_disabled(event_loop): - assert not isinstance(event_loop, looptime.LoopTimeEventLoop) + async def test_disabled(): + event_loop = asyncio.get_running_loop() + assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert not event_loop.looptime_on """) result = testdir.runpytest("--looptime") result.assert_outcomes(passed=4) @@ -64,27 +82,36 @@ async def test_disabled(event_loop): def test_cli_option_disabled(testdir): testdir.makepyfile(""" + import asyncio import looptime import pytest @pytest.mark.asyncio - async def test_normal(event_loop): - assert not isinstance(event_loop, looptime.LoopTimeEventLoop) + async def test_normal(): + event_loop = asyncio.get_running_loop() + assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert not event_loop.looptime_on @pytest.mark.asyncio @pytest.mark.looptime - async def test_marked(event_loop): - assert not isinstance(event_loop, looptime.LoopTimeEventLoop) + async def test_marked(): + event_loop = asyncio.get_running_loop() + assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert not event_loop.looptime_on @pytest.mark.asyncio @pytest.mark.looptime(start=123) - async def test_configured(event_loop): - assert not isinstance(event_loop, looptime.LoopTimeEventLoop) + async def test_configured(): + event_loop = asyncio.get_running_loop() + assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert not event_loop.looptime_on @pytest.mark.asyncio @pytest.mark.looptime(False) - async def test_disabled(event_loop): - assert not isinstance(event_loop, looptime.LoopTimeEventLoop) + async def test_disabled(): + event_loop = asyncio.get_running_loop() + assert isinstance(event_loop, looptime.LoopTimeEventLoop) + assert not event_loop.looptime_on """) result = testdir.runpytest("--no-looptime") result.assert_outcomes(passed=4) diff --git a/tests/test_time_moves.py b/tests/test_time_moves.py index 7cad1bd..1a9d1f2 100644 --- a/tests/test_time_moves.py +++ b/tests/test_time_moves.py @@ -1,5 +1,6 @@ import asyncio import time +import warnings import async_timeout import pytest @@ -28,6 +29,14 @@ def test_execution_takes_near_zero_time(chronometer, looptime_loop): assert 0.0 <= chronometer.seconds < 0.01 +def test_execution_takes_true_time_when_disabled(chronometer, looptime_loop): + looptime_loop.setup_looptime(_enabled=False) + with chronometer: + looptime_loop.run_until_complete(asyncio.sleep(1)) + assert looptime_loop.time() == 1 + assert 1 <= chronometer.seconds < 1.1 + + def test_real_time_is_ignored(chronometer, looptime_loop): async def f(): await asyncio.sleep(5) @@ -126,3 +135,31 @@ async def f(): looptime_loop.run_until_complete(f()) assert looptime_loop.time() == expected_time + + +def test_repeated_setup_keeps_the_time(looptime_loop): + looptime_loop.setup_looptime(start=123) + looptime_loop.setup_looptime() + assert looptime_loop.time() == 123 # not zero + + +def test_forward_time_moves_works(looptime_loop): + looptime_loop.setup_looptime(start=123) + looptime_loop.setup_looptime(start=456) + assert looptime_loop.time() == 456 + + +def test_backward_time_moves_warns(looptime_loop): + looptime_loop.setup_looptime(start=456) + with pytest.warns(looptime.TimeWarning, match=r"from 456.0 to 123.0"): + looptime_loop.setup_looptime(start=123) + assert looptime_loop.time() == 123 # modified + + +def test_backward_time_moves_raises_and_keeps_the_time(looptime_loop): + looptime_loop.setup_looptime(start=456) + with pytest.raises(looptime.TimeWarning): + with warnings.catch_warnings(): + warnings.filterwarnings('error', category=looptime.TimeWarning) + looptime_loop.setup_looptime(start=123) + assert looptime_loop.time() == 456 # unmodified