GoogleApiClientUtil { public static <T> T executeWithRetries(AbstractGoogleClientRequest<T> request) throws IOException { return executeWithRetries(request, DEFAULT_RETRY_WAIT_STRATEGY, DEFAULT_RETRY_STOP_STRATEGY); } private  GoogleApiClientUtil(); static T executeWithRetries(AbstractGoogleClientRequest<T> request); static T executeWithRetries(AbstractGoogleClientRequest<T> request,
                                         WaitStrategy waitStrategy,
                                         StopStrategy stopStrategy);  }
@Test public void shouldRetry429() throws IOException { when(response.getStatusCode()).thenReturn(429).thenReturn(200); GoogleApiClientUtil.executeWithRetries(request, WaitStrategies.noWait(), StopStrategies.stopAfterAttempt(3)); verify(request, times(2)).execute(); }
@Test @Parameters({ "500", "503", "599" }) public void shouldRetry5xx(int statusCode) throws IOException { when(response.getStatusCode()).thenReturn(statusCode).thenReturn(200); GoogleApiClientUtil.executeWithRetries(request, WaitStrategies.noWait(), StopStrategies.stopAfterAttempt(3)); verify(request, times(2)).execute(); }
@Test public void shouldRetryIOException() throws IOException { response.setStatusCode(200); doThrow(new IOException()).doCallRealMethod().when(request).execute(); GoogleApiClientUtil.executeWithRetries(request, WaitStrategies.noWait(), StopStrategies.stopAfterAttempt(3)); verify(request, times(2)).execute(); }
@Test @Parameters({ "400", "401", "403", "404" }) public void shouldNotRetry4xx(int statusCode) throws IOException { when(response.getStatusCode()).thenReturn(statusCode).thenReturn(200); try { GoogleApiClientUtil.executeWithRetries(request, WaitStrategies.noWait(), StopStrategies.stopAfterAttempt(3)); fail(); } catch (HttpResponseException e) { assertThat(e.getStatusCode(), is(statusCode)); } verify(request, times(1)).execute(); }
@Test public void testRetriesByDefault() throws IOException { when(response.getStatusCode()).thenReturn(429).thenReturn(200); GoogleApiClientUtil.executeWithRetries(request); verify(request, times(2)).execute(); }