Test Failed
Pull Request — master (#216)
by Vinicius
04:08
created

tests.unit.test_core.test_retry   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A test_for_all_methods_retries() 0 23 2
A test_before_sleep() 0 8 1
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