|
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
|
|
|
] |
|
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 |
|
with pytest.raises(NoSuchActionError): |
|
58
|
1 |
|
pacer.hit("unpaced_action") |
|
59
|
|
|
|
|
60
|
1 |
|
def test_existing_pace(self, configured_pacer: Pacer): |
|
61
|
|
|
"""Test what happens when a pace is set""" |
|
62
|
1 |
|
configured_pacer.hit("paced_action") |
|
63
|
|
|
|
|
64
|
1 |
|
async def test_async_missing_pace(self, pacer: Pacer): |
|
65
|
|
|
"""Test what happens when no pace is set.""" |
|
66
|
1 |
|
with pytest.raises(NoSuchActionError): |
|
67
|
1 |
|
await pacer.ahit("unpaced_action") |
|
68
|
|
|
|
|
69
|
1 |
|
async def test_async_existing_pace(self, configured_pacer: Pacer): |
|
70
|
|
|
"""Test what happens when a pace is set""" |
|
71
|
1 |
|
await configured_pacer.ahit("paced_action") |
|
72
|
|
|
|
|
73
|
1 |
|
async def test_async_pace_limit(self, configured_pacer: Pacer): |
|
74
|
|
|
"""Test that actions are being properly paced""" |
|
75
|
1 |
|
async def micro_task(): |
|
76
|
1 |
|
await configured_pacer.ahit("paced_action") |
|
77
|
|
|
|
|
78
|
1 |
|
loop = asyncio.get_event_loop() |
|
79
|
|
|
|
|
80
|
1 |
|
start = loop.time() |
|
81
|
1 |
|
async with asyncio.timeout(5): |
|
82
|
1 |
|
await asyncio.gather( |
|
83
|
|
|
*[ |
|
84
|
|
|
micro_task() |
|
85
|
|
|
for _ in range(20) |
|
86
|
|
|
] |
|
87
|
|
|
) |
|
88
|
1 |
|
end = loop.time() |
|
89
|
|
|
|
|
90
|
1 |
|
elapsed = end - start |
|
91
|
|
|
|
|
92
|
1 |
|
assert elapsed > 1 |
|
93
|
|
|
|
|
94
|
1 |
|
def test_pace_limit(self, configured_pacer: Pacer): |
|
95
|
|
|
"""Test that actions are being properly paced""" |
|
96
|
1 |
|
actions_executed = 0 |
|
97
|
|
|
|
|
98
|
1 |
|
start = time.time() |
|
99
|
|
|
|
|
100
|
1 |
|
while actions_executed < 20: |
|
101
|
1 |
|
configured_pacer.hit("paced_action") |
|
102
|
1 |
|
actions_executed = actions_executed + 1 |
|
103
|
|
|
|
|
104
|
1 |
|
end = time.time() |
|
105
|
|
|
|
|
106
|
1 |
|
elapsed = end - start |
|
107
|
|
|
|
|
108
|
1 |
|
assert elapsed > 1 |
|
109
|
|
|
|
|
110
|
1 |
|
def test_nonexistant_strategy(self, pacer: Pacer): |
|
111
|
|
|
"""Make sure that nonexistant strategies raise an exception""" |
|
112
|
1 |
|
with pytest.raises(ValueError): |
|
113
|
1 |
|
pacer.inject_config( |
|
114
|
|
|
{ |
|
115
|
|
|
"paced_action": { |
|
116
|
|
|
"pace": "10/second", |
|
117
|
|
|
"strategy": "non-existant strategy", |
|
118
|
|
|
}, |
|
119
|
|
|
} |
|
120
|
|
|
) |
|
121
|
|
|
|
|
122
|
1 |
|
def test_bad_pace(self, pacer: Pacer, strategy): |
|
123
|
|
|
"""Make sure that bad pace values raise an exception""" |
|
124
|
1 |
|
with pytest.raises(ValueError): |
|
125
|
1 |
|
pacer.inject_config( |
|
126
|
|
|
{ |
|
127
|
|
|
"paced_action": { |
|
128
|
|
|
"pace": "z10/second", |
|
129
|
|
|
"strategy": strategy, |
|
130
|
|
|
}, |
|
131
|
|
|
} |
|
132
|
|
|
) |
|
133
|
|
|
|