TestPacer.pacer_wrapper()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 3
cp 0.6667
crap 1.037
1
"""Tests kytos.core.pacing module."""
2
3 1
import asyncio
4 1
import time
5
6 1
import pytest
7
8 1
from kytos.core.pacing import NoSuchActionError, Pacer, PacerWrapper
9
10
11 1
class TestPacer:
12
    """Pacer tests"""
13
14 1
    @pytest.fixture
15 1
    def pacer(self) -> Pacer:
16
        """Setup the pacer"""
17 1
        return Pacer("memory://")
18
19 1
    @pytest.fixture(
20
        params=[
21
            'fixed_window',
22
            'ignore_pace',
23
        ]
24
    )
25 1
    def strategy(self, request):
26
        """Provide a strategy for tests."""
27 1
        return request.param
28
29 1
    @pytest.fixture
30 1
    def configured_pacer(self, pacer: Pacer, strategy):
31
        """Configure the pacer to have a paced action."""
32 1
        pacer.inject_config(
33
            {
34
                "paced_action": {
35
                    "pace": "10/second",
36
                    "strategy": strategy,
37
                },
38
            }
39
        )
40 1
        return pacer
41
42 1
    @pytest.fixture
43 1
    def pacer_wrapper(self, pacer):
44
        """Setup a wrapper around the pacer."""
45
        return PacerWrapper("test_space", pacer)
46
47 1
    def test_check_strategies(self, pacer: Pacer):
48
        """Check which strategies are present."""
49 1
        assert set(pacer.sync_strategies) == {
50
            'fixed_window',
51
            'ignore_pace'
52
        }
53 1
        assert set(pacer.async_strategies) == {
54
            'fixed_window',
55
            'ignore_pace'
56
        }
57
58 1
    def test_missing_pace(self, pacer: Pacer):
59
        """Test what happens when no pace is set."""
60 1
        with pytest.raises(NoSuchActionError):
61 1
            pacer.hit("unpaced_action")
62
63 1
    def test_existing_pace(self, configured_pacer: Pacer):
64
        """Test what happens when a pace is set"""
65 1
        configured_pacer.hit("paced_action")
66
67 1
    async def test_async_missing_pace(self, pacer: Pacer):
68
        """Test what happens when no pace is set."""
69 1
        with pytest.raises(NoSuchActionError):
70 1
            await pacer.ahit("unpaced_action")
71
72 1
    async def test_async_existing_pace(self, configured_pacer: Pacer):
73
        """Test what happens when a pace is set"""
74 1
        await configured_pacer.ahit("paced_action")
75
76 1
    async def test_async_pace_limit(self, strategy, configured_pacer: Pacer):
77
        """Test that actions are being properly paced"""
78 1
        async def micro_task():
79 1
            await configured_pacer.ahit("paced_action")
80
81 1
        loop = asyncio.get_event_loop()
82
83 1
        start = loop.time()
84 1
        async with asyncio.timeout(5):
85 1
            await asyncio.gather(
86
                *[
87
                    micro_task()
88
                    for _ in range(20)
89
                ]
90
            )
91 1
        end = loop.time()
92
93 1
        elapsed = end - start
94
95 1
        if strategy != 'ignore_pace':
96 1
            assert elapsed > 1
97
        else:
98 1
            assert elapsed < 1
99
100 1
    def test_pace_limit(self, strategy, configured_pacer: Pacer):
101
        """Test that actions are being properly paced"""
102 1
        actions_executed = 0
103
104 1
        start = time.time()
105
106 1
        while actions_executed < 20:
107 1
            configured_pacer.hit("paced_action")
108 1
            actions_executed = actions_executed + 1
109
110 1
        end = time.time()
111
112 1
        elapsed = end - start
113
114 1
        if strategy != 'ignore_pace':
115 1
            assert elapsed > 1
116
        else:
117 1
            assert elapsed < 1
118
119 1
    def test_nonexistant_strategy(self, pacer: Pacer):
120
        """Make sure that nonexistant strategies raise an exception"""
121 1
        with pytest.raises(ValueError):
122 1
            pacer.inject_config(
123
                {
124
                    "paced_action": {
125
                        "pace": "10/second",
126
                        "strategy": "non-existant strategy",
127
                    },
128
                }
129
            )
130
131 1
    def test_bad_pace(self, pacer: Pacer, strategy):
132
        """Make sure that bad pace values raise an exception"""
133 1
        with pytest.raises(ValueError):
134 1
            pacer.inject_config(
135
                {
136
                    "paced_action": {
137
                        "pace": "z10/second",
138
                        "strategy": strategy,
139
                    },
140
                }
141
            )
142