This repository was archived by the owner on Feb 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathtest_retry_base.py
More file actions
293 lines (252 loc) · 10.1 KB
/
test_retry_base.py
File metadata and controls
293 lines (252 loc) · 10.1 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Copyright 2017 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
import re
import mock
import pytest
import requests.exceptions
from google.api_core import exceptions
from google.api_core import retry
from google.auth import exceptions as auth_exceptions
def test_if_exception_type():
predicate = retry.if_exception_type(ValueError)
assert predicate(ValueError())
assert not predicate(TypeError())
def test_if_exception_type_multiple():
predicate = retry.if_exception_type(ValueError, TypeError)
assert predicate(ValueError())
assert predicate(TypeError())
assert not predicate(RuntimeError())
def test_if_transient_error():
assert retry.if_transient_error(exceptions.InternalServerError(""))
assert retry.if_transient_error(exceptions.TooManyRequests(""))
assert retry.if_transient_error(exceptions.ServiceUnavailable(""))
assert retry.if_transient_error(requests.exceptions.ConnectionError(""))
assert retry.if_transient_error(requests.exceptions.ChunkedEncodingError(""))
assert retry.if_transient_error(auth_exceptions.TransportError(""))
assert not retry.if_transient_error(exceptions.InvalidArgument(""))
# Make uniform return half of its maximum, which will be the calculated
# sleep time.
@mock.patch("random.uniform", autospec=True, side_effect=lambda m, n: n)
def test_exponential_sleep_generator_base_2(uniform):
gen = retry.exponential_sleep_generator(1, 60, multiplier=2)
result = list(itertools.islice(gen, 8))
assert result == [1, 2, 4, 8, 16, 32, 60, 60]
def test_build_retry_error_empty_list():
"""
attempt to build a retry error with no errors encountered
should return a generic RetryError
"""
from google.api_core.retry import build_retry_error
from google.api_core.retry import RetryFailureReason
reason = RetryFailureReason.NON_RETRYABLE_ERROR
src, cause = build_retry_error([], reason, 10)
assert isinstance(src, exceptions.RetryError)
assert cause is None
assert src.message == "Unknown error"
def test_build_retry_error_timeout_message():
"""
should provide helpful error message when timeout is reached
"""
from google.api_core.retry import build_retry_error
from google.api_core.retry import RetryFailureReason
reason = RetryFailureReason.TIMEOUT
cause = RuntimeError("timeout")
src, found_cause = build_retry_error([ValueError(), cause], reason, 10)
assert isinstance(src, exceptions.RetryError)
assert src.message == "Timeout of 10.0s exceeded"
# should attach appropriate cause
assert found_cause is cause
def test_build_retry_error_empty_timeout():
"""
attempt to build a retry error when timeout is None
should return a generic timeout error message
"""
from google.api_core.retry import build_retry_error
from google.api_core.retry import RetryFailureReason
reason = RetryFailureReason.TIMEOUT
src, _ = build_retry_error([], reason, None)
assert isinstance(src, exceptions.RetryError)
assert src.message == "Timeout exceeded"
class Test_BaseRetry(object):
def _make_one(self, *args, **kwargs):
return retry.retry_base._BaseRetry(*args, **kwargs)
def test_constructor_defaults(self):
retry_ = self._make_one()
assert retry_._predicate == retry.if_transient_error
assert retry_._initial == 1
assert retry_._maximum == 60
assert retry_._multiplier == 2
assert retry_._timeout == 120
assert retry_._on_error is None
assert retry_.timeout == 120
assert retry_.timeout == 120
def test_constructor_options(self):
_some_function = mock.Mock()
retry_ = self._make_one(
predicate=mock.sentinel.predicate,
initial=1,
maximum=2,
multiplier=3,
timeout=4,
on_error=_some_function,
)
assert retry_._predicate == mock.sentinel.predicate
assert retry_._initial == 1
assert retry_._maximum == 2
assert retry_._multiplier == 3
assert retry_._timeout == 4
assert retry_._on_error is _some_function
@pytest.mark.parametrize("use_deadline", [True, False])
@pytest.mark.parametrize("value", [None, 0, 1, 4, 42, 5.5])
def test_with_timeout(self, use_deadline, value):
retry_ = self._make_one(
predicate=mock.sentinel.predicate,
initial=1,
maximum=2,
multiplier=3,
timeout=4,
on_error=mock.sentinel.on_error,
)
new_retry = (
retry_.with_timeout(value)
if not use_deadline
else retry_.with_deadline(value)
)
assert retry_ is not new_retry
assert new_retry._timeout == value
assert (
new_retry.timeout == value
if not use_deadline
else new_retry.deadline == value
)
# the rest of the attributes should remain the same
assert new_retry._predicate is retry_._predicate
assert new_retry._initial == retry_._initial
assert new_retry._maximum == retry_._maximum
assert new_retry._multiplier == retry_._multiplier
assert new_retry._on_error is retry_._on_error
def test_with_predicate(self):
retry_ = self._make_one(
predicate=mock.sentinel.predicate,
initial=1,
maximum=2,
multiplier=3,
timeout=4,
on_error=mock.sentinel.on_error,
)
new_retry = retry_.with_predicate(mock.sentinel.predicate)
assert retry_ is not new_retry
assert new_retry._predicate == mock.sentinel.predicate
# the rest of the attributes should remain the same
assert new_retry._timeout == retry_._timeout
assert new_retry._initial == retry_._initial
assert new_retry._maximum == retry_._maximum
assert new_retry._multiplier == retry_._multiplier
assert new_retry._on_error is retry_._on_error
def test_with_delay_noop(self):
retry_ = self._make_one(
predicate=mock.sentinel.predicate,
initial=1,
maximum=2,
multiplier=3,
timeout=4,
on_error=mock.sentinel.on_error,
)
new_retry = retry_.with_delay()
assert retry_ is not new_retry
assert new_retry._initial == retry_._initial
assert new_retry._maximum == retry_._maximum
assert new_retry._multiplier == retry_._multiplier
@pytest.mark.parametrize(
"originals,updated,expected",
[
[(1, 2, 3), (4, 5, 6), (4, 5, 6)],
[(1, 2, 3), (0, 0, 0), (0, 0, 0)],
[(1, 2, 3), (None, None, None), (1, 2, 3)],
[(0, 0, 0), (None, None, None), (0, 0, 0)],
[(1, 2, 3), (None, 0.5, None), (1, 0.5, 3)],
[(1, 2, 3), (None, 0.5, 4), (1, 0.5, 4)],
[(1, 2, 3), (9, None, None), (9, 2, 3)],
],
)
def test_with_delay(self, originals, updated, expected):
retry_ = self._make_one(
predicate=mock.sentinel.predicate,
initial=originals[0],
maximum=originals[1],
multiplier=originals[2],
timeout=14,
on_error=mock.sentinel.on_error,
)
new_retry = retry_.with_delay(
initial=updated[0], maximum=updated[1], multiplier=updated[2]
)
assert retry_ is not new_retry
assert new_retry._initial == expected[0]
assert new_retry._maximum == expected[1]
assert new_retry._multiplier == expected[2]
# the rest of the attributes should remain the same
assert new_retry._timeout == retry_._timeout
assert new_retry._predicate is retry_._predicate
assert new_retry._on_error is retry_._on_error
def test_with_delay_partial_options(self):
retry_ = self._make_one(
predicate=mock.sentinel.predicate,
initial=1,
maximum=2,
multiplier=3,
timeout=4,
on_error=mock.sentinel.on_error,
)
new_retry = retry_.with_delay(initial=4)
assert retry_ is not new_retry
assert new_retry._initial == 4
assert new_retry._maximum == 2
assert new_retry._multiplier == 3
new_retry = retry_.with_delay(maximum=4)
assert retry_ is not new_retry
assert new_retry._initial == 1
assert new_retry._maximum == 4
assert new_retry._multiplier == 3
new_retry = retry_.with_delay(multiplier=4)
assert retry_ is not new_retry
assert new_retry._initial == 1
assert new_retry._maximum == 2
assert new_retry._multiplier == 4
# the rest of the attributes should remain the same
assert new_retry._timeout == retry_._timeout
assert new_retry._predicate is retry_._predicate
assert new_retry._on_error is retry_._on_error
def test___str__(self):
def if_exception_type(exc):
return bool(exc) # pragma: NO COVER
# Explicitly set all attributes as changed Retry defaults should not
# cause this test to start failing.
retry_ = self._make_one(
predicate=if_exception_type,
initial=1.0,
maximum=60.0,
multiplier=2.0,
timeout=120.0,
on_error=None,
)
assert re.match(
(
r"<_BaseRetry predicate=<function.*?if_exception_type.*?>, "
r"initial=1.0, maximum=60.0, multiplier=2.0, timeout=120.0, "
r"on_error=None>"
),
str(retry_),
)