Metadata-Version: 2.1
Name: sleepfake
Version: 1.0.0
Summary: Fake the time.sleep/asyncio.sleep function during tests.
Author-email: Guillaume Pouyat <guillaume.pouyat@protonmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Testing :: Mocking
Requires-Python: >=3.8
Requires-Dist: freezegun>=1.4.0
Requires-Dist: pre-commit>=3.6.2
Requires-Dist: pytest>=8.0.2
Requires-Dist: pytz>=2024.1
Description-Content-Type: text/markdown

# ⏰ SleepFake

[![pypi](https://img.shields.io/pypi/v/sleepfake.svg)](https://pypi.python.org/pypi/sleepfake)
[![Version](https://img.shields.io/pypi/v/sleepfake)](<https://pypi.python.org/pypi/sleepfake>)
[![License](https://img.shields.io/pypi/l/sleepfake)](<https://pypi.python.org/pypi/sleepfake>)

`SleepFake` is a compact Python package (under 100 lines) that provides a context manager to simulate the `time.sleep` and `asyncio.sleep` functions during tests. This is useful for testing time-dependent code without the need to actually wait for time to pass. The real magic behind this package comes from [freezegun](https://github.com/spulec/freezegun). 🎩✨

## Installation

```bash
pip install sleepfake
```

## 🚀 Usage

```python
import asyncio
import time

from sleepfake import SleepFake


def test_example():
    real_start = time.time()
    with SleepFake():
        start = time.time()
        time.sleep(10)
        end = time.time()
        assert end - start == 10
    real_end = time.time()
    assert real_end - real_start < 1


async def test_async_example():
    real_start = asyncio.get_event_loop().time()
    with SleepFake():
        start = asyncio.get_event_loop().time()
        await asyncio.gather(asyncio.sleep(5), asyncio.sleep(5), asyncio.sleep(5))
        end = asyncio.get_event_loop().time()
        assert end - start <= 5.5  # almost 5 seconds  # noqa: PLR2004
        assert end - start >= 5  # almost 5 seconds  # noqa: PLR2004
    real_end = asyncio.get_event_loop().time()
    assert real_end - real_start < 1  # almost 0 seconds
```

## Local Development

### Prerequisites

Install [rye](https://rye-up.com/)

```bash
curl -sSf https://rye-up.com/get | bash
```

### Install dep

```bash
rye sync
```

### Run tests

```bash
rye run test
```

### Run linter

```bash
rye run lint
```

## Acknowledgments 🙏

- [freezegun](https://github.com/spulec/freezegun)
