Test Failed
Pull Request — master (#480)
by
unknown
09:10
created

TestPacer.pacer_wrapper()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
"""Tests kytos.core.pacing module."""
2
3
import asyncio
4
import time
5
6
import pytest
7
8
from kytos.core.pacing import Pacer, PacerWrapper
9
10
11
class TestPacer:
12
    """Pacer tests"""
13
14
    @pytest.fixture
15
    def pacer(self) -> Pacer:
16
        """Setup the pacer"""
17
        return Pacer("memory://")
18
19
    @pytest.fixture(params=['fixed_window', 'elastic_window'])
20
    def configured_pacer(self, pacer: Pacer, request):
21
        """Configure the pacer to have a paced action."""
22
        pacer.inject_config(
23
            {
24
                "paced_action": {
25
                    "pace": "10/second",
26
                    "strategy": request.param,
27
                },
28
            }
29
        )
30
        return pacer
31
32
    @pytest.fixture
33
    def pacer_wrapper(self, pacer):
34
        """Setup a wrapper around the pacer."""
35
        return PacerWrapper("test_space", pacer)
36
37
    def test_check_strategies(self, pacer: Pacer):
38
        """Check which strategies are present."""
39
        assert set(pacer.sync_strategies) == {
40
            'fixed_window', 'elastic_window',
41
        }
42
        assert set(pacer.async_strategies) == {
43
            'fixed_window', 'elastic_window',
44
        }
45
46
    def test_missing_pace(self, pacer: Pacer):
47
        """Test what happens when no pace is set."""
48
        pacer.hit("unpaced_action")
49
50
    def test_existing_pace(self, configured_pacer: Pacer):
51
        """Test what happens when a pace is set"""
52
        configured_pacer.hit("paced_action")
53
54
    async def test_async_missing_pace(self, pacer: Pacer):
55
        """Test what happens when no pace is set."""
56
        await pacer.ahit("unpaced_action")
57
58
    async def test_async_existing_pace(self, configured_pacer: Pacer):
59
        """Test what happens when a pace is set"""
60
        await configured_pacer.ahit("paced_action")
61
62
    @pytest.mark.skip(reason="Inconsistent behaviour with elastic_window")
63
    async def test_async_pace_limit(self, configured_pacer: Pacer):
64
        """Test that actions are being properly paced"""
65
        async def micro_task():
66
            await configured_pacer.ahit("paced_action")
67
68
        async with asyncio.timeout(20):
69
            await asyncio.gather(
70
                *[
71
                    micro_task()
72
                    for _ in range(50)
73
                ]
74
            )
75
76
    async def test_async_pace_limit_exceeded(self, configured_pacer: Pacer):
77
        """Test that actions are being properly paced"""
78
        async def micro_task():
79
            await configured_pacer.ahit("paced_action")
80
81
        with pytest.raises(TimeoutError):
82
            async with asyncio.timeout(9):
83
                await asyncio.gather(
84
                    *[
85
                        micro_task()
86
                        for _ in range(100)
87
                    ]
88
                )
89
90
    def test_pace_limit(self, configured_pacer: Pacer):
91
        """Test that actions are being properly paced"""
92
        actions_executed = 0
93
94
        start = time.time()
95
96
        while actions_executed < 50:
97
            configured_pacer.hit("paced_action")
98
            actions_executed = actions_executed + 1
99
100
        end = time.time()
101
102
        elapsed = end - start
103
104
        assert elapsed > 4
105