Total Complexity | 3 |
Total Lines | 41 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | """Test kytos.core.retry module.""" |
||
2 | |||
3 | from unittest.mock import patch, MagicMock |
||
4 | from kytos.core.retry import before_sleep, for_all_methods, retries |
||
5 | from tenacity import retry_if_exception_type, stop_after_attempt |
||
6 | |||
7 | |||
8 | @patch("kytos.core.retry.LOG") |
||
9 | def test_before_sleep(mock_log): |
||
10 | """Test before sleep.""" |
||
11 | state = MagicMock() |
||
12 | state.fn.__name__ = "some_name" |
||
13 | state.seconds_since_start = 1 |
||
14 | before_sleep(state) |
||
15 | assert mock_log.warning.call_count == 1 |
||
16 | |||
17 | |||
18 | def test_for_all_methods_retries(): |
||
19 | """test for_all_methods retries.""" |
||
20 | |||
21 | mock = MagicMock() |
||
22 | max_retries = 3 |
||
23 | |||
24 | @for_all_methods( |
||
25 | retries, |
||
26 | retry=retry_if_exception_type((ValueError,)), |
||
27 | stop=stop_after_attempt(max_retries), |
||
28 | reraise=True, |
||
29 | ) |
||
30 | class SomeClass: |
||
31 | def some_method(self, mock) -> None: |
||
32 | mock() |
||
33 | raise ValueError("some error") |
||
34 | |||
35 | some_class = SomeClass() |
||
36 | try: |
||
37 | some_class.some_method(mock) |
||
38 | except ValueError: |
||
39 | pass |
||
40 | assert mock.call_count == max_retries |
||
41 |