def test_retry_once(self): with pytest.raises(self.Predicate): retry_over_time( self.myfun, self.Predicate, max_retries=1, errback=self.errback, interval_max=14, ) assert self.index == 1 # no errback with pytest.raises(self.Predicate): retry_over_time( self.myfun, self.Predicate, max_retries=1, errback=None, interval_max=14, )
def test_retry_once(self): with self.assertRaises(self.Predicate): retry_over_time( self.myfun, self.Predicate, max_retries=1, errback=self.errback, interval_max=14, ) self.assertEqual(self.index, 1) # no errback with self.assertRaises(self.Predicate): retry_over_time( self.myfun, self.Predicate, max_retries=1, errback=None, interval_max=14, )
def test_retry_zero(self, sleepdeprived): with pytest.raises(self.Predicate): retry_over_time( self.myfun, self.Predicate, max_retries=0, errback=self.errback, interval_max=14, ) assert self.index == 0 # no errback with pytest.raises(self.Predicate): retry_over_time( self.myfun, self.Predicate, max_retries=0, errback=None, interval_max=14, )
def test_retry_timeout(self): with pytest.raises(self.Predicate): retry_over_time( self.myfun, self.Predicate, errback=self.errback, interval_max=14, timeout=1 ) assert self.index == 1 # no errback with pytest.raises(self.Predicate): retry_over_time( self.myfun, self.Predicate, errback=None, timeout=1, )
def ensure(self, fun, args, **policy): retry_policy = dict(self.retry_policy, **policy) max_retries = retry_policy.get('max_retries') return retry_over_time( fun, self.connection_errors, args, {}, partial(self.on_connection_error, max_retries), **retry_policy)
def test_retry_always(self): Predicate = self.Predicate class Fun(object): def __init__(self): self.calls = 0 def __call__(self, *args, **kwargs): try: if self.calls >= 10: return 42 raise Predicate() finally: self.calls += 1 fun = Fun() self.assertEqual( retry_over_time( fun, self.Predicate, max_retries=0, errback=None, interval_max=14, ), 42, ) self.assertEqual(fun.calls, 11)
def test_simple(self): prev_count, utils.count = utils.count, Mock() try: utils.count.return_value = list(range(1)) x = retry_over_time(self.myfun, self.Predicate, errback=None, interval_max=14) assert x is None utils.count.return_value = list(range(10)) cb = Mock() x = retry_over_time(self.myfun, self.Predicate, errback=self.errback, callback=cb, interval_max=14) assert x == 42 assert self.index == 9 cb.assert_called_with() finally: utils.count = prev_count
def _try_receiver_over_time(*args, **kwargs): def on_error(exc, intervals, retries): interval = next(intervals) err_msg = RECEIVER_RETRY_ERROR % \ {'receiver': retry_fun, 'when': humanize_seconds(interval, 'in', ' ')} logger.error(err_msg) return interval return retry_over_time(retry_fun, Exception, args, kwargs, on_error)
def _try_receiver_over_time(*args, **kwargs): def on_error(exc, intervals, retries): interval = next(intervals) err_msg = RECEIVER_RETRY_ERROR % { "receiver": retry_fun, "when": humanize_seconds(interval, "in", " "), } logger.error(err_msg) return interval return retry_over_time(retry_fun, Exception, args, kwargs, on_error)
def test_retry_always(self): Predicate = self.Predicate class Fun(object): def __init__(self): self.calls = 0 def __call__(self, *args, **kwargs): try: if self.calls >= 10: return 42 raise Predicate() finally: self.calls += 1 fun = Fun() assert retry_over_time( fun, self.Predicate, max_retries=None, errback=None, interval_max=14) == 42 assert fun.calls == 11
def retry_over_time(self, *args, **kwargs): return retry_over_time(*args, **kwargs)