@Test public void retryOnceThenFail() { final HttpException expectedException = new HttpException(new HttpResponse(403)); final ServiceCallback callback = mock(ServiceCallback.class); HttpClient httpClient = mock(HttpClient.class); doAnswer(new Answer<ServiceCall>() { @Override public ServiceCall answer(InvocationOnMock invocationOnMock) { ((ServiceCallback) invocationOnMock.getArguments()[4]).onCallFailed(new UnknownHostException()); return mock(ServiceCall.class); } }).doAnswer(new Answer<ServiceCall>() { @Override public ServiceCall answer(InvocationOnMock invocationOnMock) { ((ServiceCallback) invocationOnMock.getArguments()[4]).onCallFailed(expectedException); return mock(ServiceCall.class); } }).when(httpClient).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class)); Handler handler = mock(Handler.class); HttpClient retryer = new HttpClientRetryer(httpClient, handler); simulateRetryAfterDelay(handler); retryer.callAsync(null, null, null, null, callback); verifyDelay(handler, 0); verifyNoMoreInteractions(handler); verify(callback).onCallFailed(any(Exception.class)); verify(callback).onCallFailed(expectedException); verifyNoMoreInteractions(callback); }
@Override public ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, ServiceCallback serviceCallback) { RetryableCall retryableCall = new RetryableCall(mDecoratedApi, url, method, headers, callTemplate, serviceCallback); retryableCall.run(); return retryableCall; }
HttpClientRetryer extends HttpClientDecorator { @Override public ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, ServiceCallback serviceCallback) { RetryableCall retryableCall = new RetryableCall(mDecoratedApi, url, method, headers, callTemplate, serviceCallback); retryableCall.run(); return retryableCall; } }
HttpClientRetryer extends HttpClientDecorator { @Override public ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, ServiceCallback serviceCallback) { RetryableCall retryableCall = new RetryableCall(mDecoratedApi, url, method, headers, callTemplate, serviceCallback); retryableCall.run(); return retryableCall; } HttpClientRetryer(HttpClient decoratedApi); @VisibleForTesting  HttpClientRetryer(HttpClient decoratedApi, Handler handler); }
HttpClientRetryer extends HttpClientDecorator { @Override public ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, ServiceCallback serviceCallback) { RetryableCall retryableCall = new RetryableCall(mDecoratedApi, url, method, headers, callTemplate, serviceCallback); retryableCall.run(); return retryableCall; } HttpClientRetryer(HttpClient decoratedApi); @VisibleForTesting  HttpClientRetryer(HttpClient decoratedApi, Handler handler); @Override ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, ServiceCallback serviceCallback); }
HttpClientRetryer extends HttpClientDecorator { @Override public ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, ServiceCallback serviceCallback) { RetryableCall retryableCall = new RetryableCall(mDecoratedApi, url, method, headers, callTemplate, serviceCallback); retryableCall.run(); return retryableCall; } HttpClientRetryer(HttpClient decoratedApi); @VisibleForTesting  HttpClientRetryer(HttpClient decoratedApi, Handler handler); @Override ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, ServiceCallback serviceCallback);  }