Skip to content

Commit d139814

Browse files
committed
fix(retry decorator): allow retry if retries = 1
if we use @Retry(retries=1) we assume that we will try to run function and retry one more time. But range(1) will generate just single element, and actually we will not have chance to retry. By default, we had "try" + "2 retries". To keep it we should decrease default retries value.
1 parent beacee1 commit d139814

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

tests/unit/test_tion.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ def tearDown(self):
2121
self.patch.stop()
2222

2323
def test_success_single_try(self):
24-
@retry(1)
24+
@retry(retries=0)
2525
def a():
2626
self.count += 1
2727
return "expected_result"
2828
self.assertEqual(a(), "expected_result")
2929
self.assertEqual(self.count, 1)
3030

3131
def test_success_two_tries(self):
32-
@retry(2)
32+
@retry(retries=1)
3333
def a():
3434
self.count += 1
3535
return "expected_result"
3636
self.assertEqual(a(), "expected_result")
3737
self.assertEqual(self.count, 1)
3838

3939
def test_failure_two_tries(self):
40-
@retry(2)
40+
@retry(retries=1)
4141
def a():
4242
self.count += 1
4343
raise Exception()
@@ -48,7 +48,7 @@ def a():
4848
self.assertEqual(self.count, 2)
4949

5050
def test_success_after_third_try(self):
51-
@retry(5)
51+
@retry(retries=5)
5252
def a():
5353
self.count += 1
5454
if self.count == 3:
@@ -75,7 +75,7 @@ def a():
7575
self.assertGreaterEqual(end-start, t_delay)
7676

7777
def test_debug_log_level(self):
78-
@retry(1)
78+
@retry(retries=0)
7979
def debug():
8080
pass
8181

@@ -88,7 +88,7 @@ def debug():
8888

8989
def test_info_log_level(self):
9090
"""only debug and info messages if we have just BTLEDisconnectError and BTLEInternalError"""
91-
@retry(2)
91+
@retry(retries=1)
9292
def info(_e):
9393
if self.count == 0:
9494
self.count += 1
@@ -107,7 +107,7 @@ def info(_e):
107107

108108
def test_warning_log_level(self):
109109
"""Make sure that we have warnings for exception, but have no critical if all goes well finally"""
110-
@retry(2)
110+
@retry(retries=1)
111111
def warning():
112112
if self.count == 0:
113113
self.count += 1
@@ -122,7 +122,7 @@ def warning():
122122

123123
def test_critical_log_level(self):
124124
"""Make sure that we have message at critical level if all goes bas"""
125-
@retry(1)
125+
@retry(retries=0)
126126
def critical():
127127
raise Exception
128128

@@ -134,7 +134,7 @@ def critical():
134134
log_mock.critical.assert_called()
135135

136136
def test_MaxTriesExceededError(self):
137-
@retry(1)
137+
@retry(retries=0)
138138
def e():
139139
raise Exception
140140

tion_btle/tion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class MaxTriesExceededError(Exception):
1414
pass
1515

1616

17-
def retry(retries: int = 3, delay: int = 0):
17+
def retry(retries: int = 2, delay: int = 0):
1818
def decor(f: Callable):
1919
def wrapper(*args, **kwargs):
20-
for i in range(retries):
20+
for i in range(retries+1):
2121
try:
2222
_LOGGER.debug("Trying %d/%d: %s(args=%s,kwargs=%s)", i, retries, f.__name__, args, kwargs)
2323
return f(*args, **kwargs)

0 commit comments

Comments
 (0)