|
32 | 32 | import sys
|
33 | 33 | import time
|
34 | 34 | import tempfile
|
| 35 | +import pytest |
35 | 36 |
|
36 | 37 | from .. import retry
|
37 | 38 |
|
@@ -212,26 +213,39 @@ def method():
|
212 | 213 | yield from range(3)
|
213 | 214 |
|
214 | 215 | def test_retry_iter_reset(self):
|
| 216 | + """Test would fail when the timer does not get reset after every iteration""" |
| 217 | + failure_t0 = None |
215 | 218 | failure_count = 0
|
216 |
| - retry_period = 0.2 |
217 |
| - failure_duration = 0.6 |
| 219 | + retry_period = 0.1 |
| 220 | + failure_duration = 0.7 |
| 221 | + nretry_offset = 10 |
218 | 222 |
|
219 |
| - nfailures = int(failure_duration/retry_period + 0.5) |
| 223 | + nfailures = int(failure_duration / retry_period + 0.5) |
220 | 224 | retry_timeout = failure_duration + retry_period
|
| 225 | + xfail_timeout = failure_duration + retry_period * 0.5 |
221 | 226 |
|
222 | 227 | @retry.retry(retry_period=retry_period, retry_timeout=retry_timeout)
|
223 |
| - def iter_silx(start_index=0): |
224 |
| - nonlocal failure_count |
| 228 | + def iter_with_failure(start_index=0): |
| 229 | + nonlocal failure_count, failure_t0 |
225 | 230 |
|
226 |
| - # This takes `10 * retry_period` seconds |
| 231 | + # This takes `nretry_offset * retry_period` seconds |
227 | 232 | if start_index == 0:
|
228 |
| - for i in range(10): |
| 233 | + for i in range(nretry_offset): |
229 | 234 | time.sleep(retry_period)
|
230 | 235 | yield i
|
| 236 | + failure_t0 = time.time() |
231 | 237 |
|
232 | 238 | # This will fail for slightly longer than `failure_duration` seconds
|
233 | 239 | if failure_count <= nfailures:
|
234 | 240 | failure_count += 1
|
| 241 | + if failure_count > 1: |
| 242 | + iter_time = (time.time() - failure_t0) / (failure_count - 1) |
| 243 | + if (iter_time * nfailures) >= xfail_timeout: |
| 244 | + pytest.xfail("iteration takes much longer than retry_period") |
235 | 245 | raise retry.RetryError()
|
236 | 246 |
|
237 |
| - assert list(iter_silx()) == list(range(10)) |
| 247 | + yield nretry_offset |
| 248 | + |
| 249 | + yielded = list(iter_with_failure()) |
| 250 | + expected = list(range(nretry_offset + 1)) |
| 251 | + assert yielded == expected |
0 commit comments