def test_cleanup_called_on_exception(self): calls = random.randint(1, 10) cleanup = mock.Mock() self.set_to_fail(times=calls) retry_call(self.attempt, retries=calls, cleanup=cleanup, trap=Exception) assert cleanup.call_count == calls assert cleanup.called_with()
def test_infinite_retries(self): self.set_to_fail(times=999) cleanup = mock.Mock() retry_call(self.attempt, retries=float('inf'), cleanup=cleanup, trap=Exception) assert cleanup.call_count == 999
def test_retry_call_fails(self): """ Failing more than the number of retries should raise the underlying error. """ self.set_to_fail(times=3) with pytest.raises(ValueError) as res: retry_call(self.attempt, retries=2, trap=ValueError) assert str(res.value) == 'Failed!'
def test_with_arg(self): self.set_to_fail(times=0) arg = mock.Mock() bound = functools.partial(self.attempt, arg) res = retry_call(bound) assert res == 'Success' assert arg.touch.called
def acquire(self): """ Attempt to acquire the lock every `delay` seconds until the lock is acquired or until `timeout` has expired. Raises FileLockTimeout if the timeout is exceeded. Errors opening the lock file (other than if it exists) are passed through. """ self.lock = retry_call( self._attempt, retries=float('inf'), trap=zc.lockfile.LockError, cleanup=functools.partial(self._check_timeout, timing.Stopwatch()), )
def test_retry_call_succeeds(self): self.set_to_fail(times=2) res = retry_call(self.attempt, retries=2, trap=ValueError) assert res == "Success"
def test_retry_exception_superclass(self): self.set_to_fail(times=2) res = retry_call(self.attempt, retries=2, trap=Exception) assert res == "Success"
def test_retry_multiple_exceptions(self): self.set_to_fail(times=2) errors = ValueError, NameError res = retry_call(self.attempt, retries=2, trap=errors) assert res == "Success"
def test_default_does_not_retry(self): self.set_to_fail(times=1) with pytest.raises(ValueError): retry_call(self.attempt, trap=Exception)
def test_default_traps_nothing(self): self.set_to_fail(times=1) with pytest.raises(ValueError): retry_call(self.attempt, retries=1)