pytest_test_categories.adapters.sleep

Production sleep blocker adapter using patching.

This module provides the production implementation of SleepBlockerPort that actually intercepts sleep calls by patching time and asyncio modules.

The SleepPatchingBlocker follows hexagonal architecture principles: - Implements the SleepBlockerPort interface (port) - Patches sleep functions to intercept sleep attempts - Raises SleepViolationError on unauthorized sleeps - Restores original functions on deactivation

Intercepted Entry Points: - time.sleep (standard library) - asyncio.sleep (coroutine - returns immediately for small tests)

Note: threading.Event.wait() with timeout is not currently intercepted as it has legitimate uses for synchronization. It can be added based on user feedback.

Example

>>> blocker = SleepPatchingBlocker()
>>> try:
...     blocker.activate(TestSize.SMALL, EnforcementMode.STRICT)
...     time.sleep(0.1)  # Raises SleepViolationError
... finally:
...     blocker.deactivate()  # Restore original sleep behavior

See also

  • SleepBlockerPort: The abstract interface in ports/sleep.py

  • FakeSleepBlocker: Test adapter in adapters/fake_sleep.py

  • SubprocessPatchingBlocker: Similar production adapter pattern for process

Classes

SleepPatchingBlocker

Production adapter that patches time.sleep and asyncio.sleep to block sleep calls.

Module Contents

class pytest_test_categories.adapters.sleep.SleepPatchingBlocker(/, **data)[source]

Bases: pytest_test_categories.ports.sleep.SleepBlockerPort

Production adapter that patches time.sleep and asyncio.sleep to block sleep calls.

This adapter intercepts sleep calls by patching: - time.sleep (standard library) - asyncio.sleep (coroutine)

The patching is reversible - deactivate() restores the original functions.

Parameters:

data (Any)

state[source]

Current blocker state (inherited from SleepBlockerPort).

current_test_size[source]

The test size set during activation.

current_enforcement_mode[source]

The enforcement mode set during activation.

current_test_nodeid[source]

The pytest node ID of the current test.

Warning

This adapter modifies global state (time, asyncio modules). Always use in a try/finally block or context manager to ensure cleanup.

Example

>>> blocker = SleepPatchingBlocker()
>>> try:
...     blocker.activate(TestSize.SMALL, EnforcementMode.STRICT)
...     time.sleep(0.1)  # Raises SleepViolationError
... finally:
...     blocker.deactivate()

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

model_post_init(context, /)[source]

Initialize post-Pydantic setup, storing references to original functions.

Parameters:

context (object)

Return type:

None

reset()[source]

Reset blocker to initial state, restoring original functions.

This is safe to call regardless of current state.

Return type:

None

current_enforcement_mode: pytest_test_categories.ports.network.EnforcementMode | None = None[source]
current_test_nodeid: str = None[source]
current_test_size: pytest_test_categories.types.TestSize | None = None[source]