RetryHttpRequestInitializer implements HttpRequestInitializer { public void setWriteTimeout(int writeTimeout) { this.writeTimeout = writeTimeout; } RetryHttpRequestInitializer();  RetryHttpRequestInitializer(Collection<Integer> additionalIgnoredResponseCodes);  RetryHttpRequestInitializer(
      Collection<Integer> additionalIgnoredResponseCodes,
      @Nullable HttpResponseInterceptor responseInterceptor);   RetryHttpRequestInitializer(
      NanoClock nanoClock,
      Sleeper sleeper,
      Collection<Integer> additionalIgnoredResponseCodes,
      HttpResponseInterceptor responseInterceptor); @Override void initialize(HttpRequest request); void setCustomErrors(CustomHttpErrors customErrors); void setWriteTimeout(int writeTimeout);  }
@Test public void testBasicOperation() throws IOException { when(mockLowLevelRequest.execute()).thenReturn(mockLowLevelResponse); when(mockLowLevelResponse.getStatusCode()).thenReturn(200); Storage.Buckets.Get result = storage.buckets().get("test"); HttpResponse response = result.executeUnparsed(); assertNotNull(response); verify(mockHttpResponseInterceptor).interceptResponse(any(HttpResponse.class)); verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString()); verify(mockLowLevelRequest).setTimeout(anyInt(), anyInt()); verify(mockLowLevelRequest).setWriteTimeout(anyInt()); verify(mockLowLevelRequest).execute(); verify(mockLowLevelResponse).getStatusCode(); expectedLogs.verifyNotLogged("Request failed"); }
@Test public void testErrorCodeForbidden() throws IOException { when(mockLowLevelRequest.execute()).thenReturn(mockLowLevelResponse); when(mockLowLevelResponse.getStatusCode()) .thenReturn(403) .thenReturn(200); try { Storage.Buckets.Get result = storage.buckets().get("test"); HttpResponse response = result.executeUnparsed(); assertNotNull(response); } catch (HttpResponseException e) { assertThat(e.getMessage(), Matchers.containsString("403")); } verify(mockHttpResponseInterceptor).interceptResponse(any(HttpResponse.class)); verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString()); verify(mockLowLevelRequest).setTimeout(anyInt(), anyInt()); verify(mockLowLevelRequest).setWriteTimeout(anyInt()); verify(mockLowLevelRequest).execute(); verify(mockLowLevelResponse).getStatusCode(); expectedLogs.verifyWarn("Request failed with code 403"); }
@Test public void testRetryableError() throws IOException { when(mockLowLevelRequest.execute()) .thenReturn(mockLowLevelResponse) .thenReturn(mockLowLevelResponse) .thenReturn(mockLowLevelResponse); when(mockLowLevelResponse.getStatusCode()) .thenReturn(503) .thenReturn(429) .thenReturn(200); Storage.Buckets.Get result = storage.buckets().get("test"); HttpResponse response = result.executeUnparsed(); assertNotNull(response); verify(mockHttpResponseInterceptor).interceptResponse(any(HttpResponse.class)); verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString()); verify(mockLowLevelRequest, times(3)).setTimeout(anyInt(), anyInt()); verify(mockLowLevelRequest, times(3)).setWriteTimeout(anyInt()); verify(mockLowLevelRequest, times(3)).execute(); verify(mockLowLevelResponse, times(3)).getStatusCode(); expectedLogs.verifyDebug("Request failed with code 503"); }
@Test public void testThrowIOException() throws IOException { when(mockLowLevelRequest.execute()) .thenThrow(new IOException("Fake Error")) .thenReturn(mockLowLevelResponse); when(mockLowLevelResponse.getStatusCode()).thenReturn(200); Storage.Buckets.Get result = storage.buckets().get("test"); HttpResponse response = result.executeUnparsed(); assertNotNull(response); verify(mockHttpResponseInterceptor).interceptResponse(any(HttpResponse.class)); verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString()); verify(mockLowLevelRequest, times(2)).setTimeout(anyInt(), anyInt()); verify(mockLowLevelRequest, times(2)).setWriteTimeout(anyInt()); verify(mockLowLevelRequest, times(2)).execute(); verify(mockLowLevelResponse).getStatusCode(); expectedLogs.verifyDebug("Request failed with IOException"); }
@Test public void testRetryableErrorRetryEnoughTimes() throws IOException { when(mockLowLevelRequest.execute()).thenReturn(mockLowLevelResponse); final int retries = 10; when(mockLowLevelResponse.getStatusCode()) .thenAnswer( new Answer<Integer>() { int n = 0; @Override public Integer answer(InvocationOnMock invocation) { return n++ < retries ? 503 : 9999; } }); Storage.Buckets.Get result = storage.buckets().get("test"); try { result.executeUnparsed(); fail(); } catch (IOException e) { } verify(mockHttpResponseInterceptor).interceptResponse(any(HttpResponse.class)); verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString()); verify(mockLowLevelRequest, times(retries + 1)).setTimeout(anyInt(), anyInt()); verify(mockLowLevelRequest, times(retries + 1)).setWriteTimeout(anyInt()); verify(mockLowLevelRequest, times(retries + 1)).execute(); verify(mockLowLevelResponse, times(retries + 1)).getStatusCode(); expectedLogs.verifyWarn("performed 10 retries due to unsuccessful status codes"); }