Test Failed
Pull Request — master (#216)
by Vinicius
05:15 queued 01:34
created

tests.unit.test_core.test_retry   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 3

2 Functions

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