def test_infinite_retry(self): backoff = BackoffMock() # specify infinite retries, but give up after 5 retry = Retry(backoff, -1) with pytest.raises(ConnectionError): retry.call_with_retry(self._do, self._fail_inf) assert self.actual_attempts == 5 assert self.actual_failures == 5
def test_retry(self, retries): backoff = BackoffMock() retry = Retry(backoff, retries) with pytest.raises(ConnectionError): retry.call_with_retry(self._do, self._fail) assert self.actual_attempts == 1 + retries assert self.actual_failures == 1 + retries assert backoff.reset_calls == 1 assert backoff.calls == retries
def test_retry_on_timeout_retry(self, Class, retries): retry_on_timeout = retries > 0 c = Class(retry_on_timeout=retry_on_timeout, retry=Retry(NoBackoff(), retries)) assert c.retry_on_timeout == retry_on_timeout assert isinstance(c.retry, Retry) assert c.retry._retries == retries
def test_connect_without_retry_on_os_error(self): """Test that the _connect function is not being retried in case of a OSError""" with patch.object(Connection, "_connect") as _connect: _connect.side_effect = OSError("") conn = Connection(retry_on_timeout=True, retry=Retry(NoBackoff(), 2)) with pytest.raises(ConnectionError): conn.connect() assert _connect.call_count == 1 self.clear(conn)
def test_client_retry_on_timeout(self, request): with patch.object(Redis, "parse_response") as parse_response: parse_response.side_effect = TimeoutError() retries = 3 r = _get_client(Redis, request, retry_on_timeout=True, retry=Retry(NoBackoff(), retries)) with pytest.raises(TimeoutError): try: r.get("foo") finally: assert parse_response.call_count == retries + 1
def test_client_retry_on_error_different_error_raised(self, request): with patch.object(Redis, "parse_response") as parse_response: parse_response.side_effect = TimeoutError() retries = 3 r = _get_client( Redis, request, retry_on_error=[ReadOnlyError], retry=Retry(NoBackoff(), retries), ) with pytest.raises(TimeoutError): try: r.get("foo") finally: assert parse_response.call_count == 1
def test_retry_connect_on_timeout_error(self): """Test that the _connect function is retried in case of a timeout""" conn = Connection(retry_on_timeout=True, retry=Retry(NoBackoff(), 3)) origin_connect = conn._connect conn._connect = mock.Mock() def mock_connect(): # connect only on the last retry if conn._connect.call_count <= 2: raise socket.timeout else: return origin_connect() conn._connect.side_effect = mock_connect conn.connect() assert conn._connect.call_count == 3 self.clear(conn)
def _gen_cluster_mock_resp(r, response): connection = Mock() connection.retry = Retry(NoBackoff(), 0) connection.read_response.return_value = response r.connection = connection return r
def test_retry_on_error_retry(self, Class, retries): c = Class(retry_on_error=[ReadOnlyError], retry=Retry(NoBackoff(), retries)) assert c.retry_on_error == [ReadOnlyError] assert isinstance(c.retry, Retry) assert c.retry._retries == retries
def __init__(self, client): self.client = client self._sock = self._socket() self.pid = 1234 self.retry = Retry(NoBackoff(), 0)