File: test_execution_timer.py

package info (click to toggle)
python-throttler 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 180 kB
  • sloc: python: 473; makefile: 4; sh: 2
file content (78 lines) | stat: -rw-r--r-- 2,213 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import time
from math import isclose

import pytest

from throttler import execution_timer, execution_timer_async

# Weak machines may be used for CI, causing delays
ABS_TOL = 0.2 if os.getenv('CI') else 0.1


class TestExecutionTimer:
    @pytest.mark.parametrize(
        ('period',), ((1.,), (3.,))
    )
    def test_without_align(self, period: float):
        @execution_timer(period)
        def t():
            curr_ts = time.time()
            if i > 0:
                assert isclose(curr_ts - prev_ts, period, abs_tol=ABS_TOL)
            return curr_ts

        prev_ts = None
        for i in range(3):
            prev_ts = t()

    @pytest.mark.parametrize(
        ('period',), ((3.,), (5.,))
    )
    def test_with_align(self, period: float):
        @execution_timer(period, align_sleep=True)
        def t():
            curr_ts = time.time()
            if i > 0:
                assert isclose(curr_ts % period, 0., abs_tol=ABS_TOL)
                if i > 1:
                    assert isclose(curr_ts - prev_ts, period, abs_tol=ABS_TOL)
            return curr_ts

        prev_ts = None
        for i in range(3):
            prev_ts = t()

    @pytest.mark.asyncio
    @pytest.mark.parametrize(
        ('period',), ((1.,), (3.,))
    )
    async def test_without_align_async(self, period: float):
        @execution_timer_async(period)
        async def t():
            curr_ts = time.time()
            if i > 0:
                assert isclose(curr_ts - prev_ts, period, abs_tol=ABS_TOL)
            return curr_ts

        prev_ts = None
        for i in range(3):
            prev_ts = await t()

    @pytest.mark.asyncio
    @pytest.mark.parametrize(
        ('period',), ((3.,), (5.,))
    )
    async def test_with_align_async(self, period: float):
        @execution_timer_async(period, align_sleep=True)
        async def t():
            curr_ts = time.time()
            if i > 0:
                assert isclose(curr_ts % period, 0., abs_tol=ABS_TOL)
                if i > 1:
                    assert isclose(curr_ts - prev_ts, period, abs_tol=ABS_TOL)
            return curr_ts

        prev_ts = None
        for i in range(3):
            prev_ts = await t()