| 1 | from typing import Any |
| 2 | |
| 3 | from .util import parametrize_setstatprofile |
| 4 | |
| 5 | |
| 6 | class CallCounter: |
| 7 | def __init__(self) -> None: |
| 8 | self.count = 0 |
| 9 | |
| 10 | def __call__(self, *args: Any, **kwds: Any) -> Any: |
| 11 | self.count += 1 |
| 12 | |
| 13 | |
| 14 | @parametrize_setstatprofile |
| 15 | def test_increment(setstatprofile): |
| 16 | time = 0.0 |
| 17 | |
| 18 | def fake_time(): |
| 19 | return time |
| 20 | |
| 21 | def fake_sleep(duration): |
| 22 | nonlocal time |
| 23 | time += duration |
| 24 | |
| 25 | counter = CallCounter() |
| 26 | |
| 27 | setstatprofile(counter, timer_func=fake_time, timer_type="timer_func") |
| 28 | |
| 29 | for _ in range(100): |
| 30 | fake_sleep(1.0) |
| 31 | |
| 32 | setstatprofile(None) |
| 33 | |
| 34 | assert counter.count == 100 |