Passed
Pull Request — master (#480)
by
unknown
04:52
created

TestPacer.pacer_wrapper()   A

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 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
        ]
23
    )
24 1
    def strategy(self, request):
25
        """Provide a strategy for tests."""
26 1
        return request.param
27
28 1
    @pytest.fixture
29 1
    def configured_pacer(self, pacer: Pacer, strategy):
30
        """Configure the pacer to have a paced action."""
31 1
        pacer.inject_config(
32
            {
33
                "paced_action": {
34
                    "pace": "10/second",
35
                    "strategy": strategy,
36
                },
37
            }
38
        )
39 1
        return pacer
40
41 1
    @pytest.fixture
42 1
    def pacer_wrapper(self, pacer):
43
        """Setup a wrapper around the pacer."""
44
        return PacerWrapper("test_space", pacer)
45
46 1
    def test_check_strategies(self, pacer: Pacer):
47
        """Check which strategies are present."""
48 1
        assert set(pacer.sync_strategies) == {
49
            'fixed_window',
50
        }
51 1
        assert set(pacer.async_strategies) == {
52
            'fixed_window',
53
        }
54
55 1
    def test_missing_pace(self, pacer: Pacer):
56
        """Test what happens when no pace is set."""
57 1
        pacer.hit("unpaced_action")
58
59 1
    def test_existing_pace(self, configured_pacer: Pacer):
60
        """Test what happens when a pace is set"""
61 1
        configured_pacer.hit("paced_action")
62
63 1
    async def test_async_missing_pace(self, pacer: Pacer):
64
        """Test what happens when no pace is set."""
65 1
        await pacer.ahit("unpaced_action")
66
67 1
    async def test_async_existing_pace(self, configured_pacer: Pacer):
68
        """Test what happens when a pace is set"""
69 1
        await configured_pacer.ahit("paced_action")
70
71 1
    async def test_async_pace_limit(self, configured_pacer: Pacer):
72
        """Test that actions are being properly paced"""
73 1
        async def micro_task():
74 1
            await configured_pacer.ahit("paced_action")
75
76 1
        loop = asyncio.get_event_loop()
77
78 1
        start = loop.time()
79 1
        async with asyncio.timeout(5):
80 1
            await asyncio.gather(
81
                *[
82
                    micro_task()
83
                    for _ in range(20)
84
                ]
85
            )
86 1
        end = loop.time()
87
88 1
        elapsed = end - start
89
90 1
        assert elapsed > 1
91
92 1
    def test_pace_limit(self, configured_pacer: Pacer):
93
        """Test that actions are being properly paced"""
94 1
        actions_executed = 0
95
96 1
        start = time.time()
97
98 1
        while actions_executed < 20:
99 1
            configured_pacer.hit("paced_action")
100 1
            actions_executed = actions_executed + 1
101
102 1
        end = time.time()
103
104 1
        elapsed = end - start
105
106 1
        assert elapsed > 1
107
108 1
    def test_nonexistant_strategy(self, pacer: Pacer):
109
        """Make sure that nonexistant strategies raise an exception"""
110 1
        with pytest.raises(ValueError):
111 1
            pacer.inject_config(
112
                {
113
                    "paced_action": {
114
                        "pace": "10/second",
115
                        "strategy": "non-existant strategy",
116
                    },
117
                }
118
            )
119
120 1
    def test_bad_pace(self, pacer: Pacer, strategy):
121
        """Make sure that bad pace values raise an exception"""
122 1
        with pytest.raises(ValueError):
123 1
            pacer.inject_config(
124
                {
125
                    "paced_action": {
126
                        "pace": "z10/second",
127
                        "strategy": strategy,
128
                    },
129
                }
130
            )
131