-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_retry.py
More file actions
62 lines (48 loc) · 1.77 KB
/
test_retry.py
File metadata and controls
62 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from unittest.mock import MagicMock, patch
from mediahaven.retry import (
retry_exponential,
RetryException,
TooManyRetriesException,
NUMBER_OF_TRIES,
DELAY,
BACKOFF,
)
import pytest
@patch("time.sleep")
def test_retry_defaults(time_sleep_mock):
function_mock = MagicMock()
function_mock.side_effect = RetryException
@retry_exponential((RetryException))
def func(self):
function_mock()
# Execute the decorated method
with pytest.raises(TooManyRetriesException):
func(MagicMock())
# Test if function was executed multiple times
assert function_mock.call_count == NUMBER_OF_TRIES
# Test if time.sleep was executed multiple times
assert time_sleep_mock.call_count == NUMBER_OF_TRIES
# Test exponential backoff
assert time_sleep_mock.call_args_list[0][0][0] == DELAY
for i in range(1, NUMBER_OF_TRIES):
prev_val = time_sleep_mock.call_args_list[i - 1][0][0]
assert time_sleep_mock.call_args_list[i][0][0] == prev_val * BACKOFF
@patch("time.sleep")
def test_retry(time_sleep_mock):
function_mock = MagicMock()
function_mock.side_effect = RetryException
@retry_exponential((RetryException), 2, 4, 5)
def func(self):
function_mock()
# Execute the decorated method
with pytest.raises(TooManyRetriesException):
func(MagicMock())
# Test if function was executed multiple times
assert function_mock.call_count == 5
# Test if time.sleep was executed multiple times
assert time_sleep_mock.call_count == 5
# Test exponential backoff
assert time_sleep_mock.call_args_list[0][0][0] == 2
for i in range(1, 5):
prev_val = time_sleep_mock.call_args_list[i - 1][0][0]
assert time_sleep_mock.call_args_list[i][0][0] == prev_val * 4