3
3
from redis .asyncio .connection import Connection , UnixDomainSocketConnection
4
4
from redis .asyncio .retry import Retry
5
5
from redis .backoff import AbstractBackoff , NoBackoff
6
- from redis .exceptions import ConnectionError
6
+ from redis .exceptions import ConnectionError , TimeoutError
7
7
8
8
9
9
class BackoffMock (AbstractBackoff ):
@@ -22,23 +22,55 @@ def compute(self, failures):
22
22
class TestConnectionConstructorWithRetry :
23
23
"Test that the Connection constructors properly handles Retry objects"
24
24
25
+ @pytest .mark .parametrize ("Class" , [Connection , UnixDomainSocketConnection ])
26
+ def test_retry_on_error_set (self , Class ):
27
+ class CustomError (Exception ):
28
+ pass
29
+
30
+ retry_on_error = [ConnectionError , TimeoutError , CustomError ]
31
+ c = Class (retry_on_error = retry_on_error )
32
+ assert c .retry_on_error == retry_on_error
33
+ assert isinstance (c .retry , Retry )
34
+ assert c .retry ._retries == 1
35
+ assert set (c .retry ._supported_errors ) == set (retry_on_error )
36
+
37
+ @pytest .mark .parametrize ("Class" , [Connection , UnixDomainSocketConnection ])
38
+ def test_retry_on_error_not_set (self , Class ):
39
+ c = Class ()
40
+ assert c .retry_on_error == []
41
+ assert isinstance (c .retry , Retry )
42
+ assert c .retry ._retries == 0
43
+
25
44
@pytest .mark .parametrize ("retry_on_timeout" , [False , True ])
26
45
@pytest .mark .parametrize ("Class" , [Connection , UnixDomainSocketConnection ])
27
- def test_retry_on_timeout_boolean (self , Class , retry_on_timeout ):
46
+ def test_retry_on_timeout (self , Class , retry_on_timeout ):
28
47
c = Class (retry_on_timeout = retry_on_timeout )
29
48
assert c .retry_on_timeout == retry_on_timeout
30
49
assert isinstance (c .retry , Retry )
31
50
assert c .retry ._retries == (1 if retry_on_timeout else 0 )
32
51
33
52
@pytest .mark .parametrize ("retries" , range (10 ))
34
53
@pytest .mark .parametrize ("Class" , [Connection , UnixDomainSocketConnection ])
35
- def test_retry_on_timeout_retry (self , Class , retries : int ):
54
+ def test_retry_with_retry_on_timeout (self , Class , retries : int ):
36
55
retry_on_timeout = retries > 0
37
56
c = Class (retry_on_timeout = retry_on_timeout , retry = Retry (NoBackoff (), retries ))
38
57
assert c .retry_on_timeout == retry_on_timeout
39
58
assert isinstance (c .retry , Retry )
40
59
assert c .retry ._retries == retries
41
60
61
+ @pytest .mark .parametrize ("retries" , range (10 ))
62
+ @pytest .mark .parametrize ("Class" , [Connection , UnixDomainSocketConnection ])
63
+ def test_retry_with_retry_on_error (self , Class , retries : int ):
64
+ class CustomError (Exception ):
65
+ pass
66
+
67
+ retry_on_error = [ConnectionError , TimeoutError , CustomError ]
68
+ c = Class (retry_on_error = retry_on_error , retry = Retry (NoBackoff (), retries ))
69
+ assert c .retry_on_error == retry_on_error
70
+ assert isinstance (c .retry , Retry )
71
+ assert c .retry ._retries == retries
72
+ assert set (c .retry ._supported_errors ) == set (retry_on_error )
73
+
42
74
43
75
class TestRetry :
44
76
"Test that Retry calls backoff and retries the expected number of times"
0 commit comments