RetryInterceptor implements Interceptor { boolean shouldRetry(int attempts, int statusCode) { return attempts < maxRetries && (statusCode == HTTPStatus.SERVICE_UNAVAILABLE.code || statusCode == HTTPStatus.TOO_MANY_REQUESTS.code); } RetryInterceptor(int maxRetries, Duration retryWaitTime, ThreadSleep sleep); @Override Response intercept(Chain chain);  }
@Test void shouldRetryWithConnectionException() { assertThat(interceptor.shouldRetry(1, "GET", new HttpTimeoutException("read timeout"))).isTrue(); assertThat(interceptor.shouldRetry(2, "GET", new ConnectException("connection failed"))).isTrue(); assertThat(interceptor.shouldRetry(3, "GET", new ConnectException("connection failed"))).isFalse(); assertThat(interceptor.shouldRetry(1, "GET", new SSLException("Connection reset"))).isTrue(); assertThat(interceptor.shouldRetry(1, "POST", new HttpConnectTimeoutException("connection timeout"))).isTrue(); assertThat(interceptor.shouldRetry(1, "POST", new ConnectException("connection refused"))).isTrue(); assertThat(interceptor.shouldRetry(1, "POST", new SocketTimeoutException("connect timed out"))).isTrue(); }
@Test void shouldRetryWithReadTimeout() { var timeout = new SocketTimeoutException("timeout"); timeout.initCause(new SocketTimeoutException("Read timed out")); assertThat(interceptor.shouldRetry(1, "POST", timeout)).isFalse(); assertThat(interceptor.shouldRetry(2, "PUT", timeout)).isTrue(); timeout = new SocketTimeoutException("timeout"); assertThat(interceptor.shouldRetry(1, "POST", timeout)).isFalse(); assertThat(interceptor.shouldRetry(2, "PUT", timeout)).isTrue(); timeout = new SocketTimeoutException("timeout"); timeout.initCause(new SocketException("Socket closed")); assertThat(interceptor.shouldRetry(1, "POST", timeout)).isFalse(); assertThat(interceptor.shouldRetry(2, "PUT", timeout)).isTrue(); var cancelled = new IOException("Canceled"); assertThat(interceptor.shouldRetry(1, "POST", cancelled)).isFalse(); assertThat(interceptor.shouldRetry(2, "PUT", cancelled)).isTrue(); }
@Test void shouldRetryWithUnknownHost() { assertThat(interceptor.shouldRetry(1, "POST", new UnknownHostException("unknown.host: System error"))).isTrue(); }
@Test void shouldRetryWithServiceUnavailable() { assertThat(interceptor.shouldRetry(1, HTTPStatus.OK.code)).isFalse(); assertThat(interceptor.shouldRetry(1, HTTPStatus.SERVICE_UNAVAILABLE.code)).isTrue(); assertThat(interceptor.shouldRetry(3, HTTPStatus.SERVICE_UNAVAILABLE.code)).isFalse(); }
@Test void shouldRetryWithTooManyRequest() { assertThat(interceptor.shouldRetry(1, HTTPStatus.TOO_MANY_REQUESTS.code)).isTrue(); assertThat(interceptor.shouldRetry(3, HTTPStatus.TOO_MANY_REQUESTS.code)).isFalse(); }