@SuppressWarnings("unchecked") @Test public void test401ShouldRetryAndFailAfterFiveAttempts() { HttpCommand command = createMock(HttpCommand.class); SessionApi sessionClient = createMock(SessionApi.class); LoadingCache<Credentials, LoginResponse> cache = createMock(LoadingCache.class); cache.invalidateAll(); expectLastCall().anyTimes(); final AtomicInteger counter = new AtomicInteger(); expect(command.incrementFailureCount()).andAnswer(new IAnswer<Integer>() { @Override public Integer answer() throws Throwable { return counter.incrementAndGet(); } }).anyTimes(); expect(command.isReplayable()).andReturn(true).anyTimes(); expect(command.getFailureCount()).andAnswer(new IAnswer<Integer>() { @Override public Integer answer() throws Throwable { return counter.get(); } }).anyTimes(); replay(cache, command); HttpResponse response = HttpResponse.builder().payload( Payloads.newStringPayload("Not relevant")).statusCode(401).build(); InvalidateSessionAndRetryOn401AndLogoutOnClose retry = new InvalidateSessionAndRetryOn401AndLogoutOnClose(cache, sessionClient); for (int i = 0; i < 5; i++) { assertTrue(retry.shouldRetryRequest(command, response)); } assertFalse(retry.shouldRetryRequest(command, response)); verify(cache, command); }
@Override public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { try { switch (response.getStatusCode()) { case 401: authenticationResponseCache.invalidateAll(); return super.shouldRetryRequest(command, response); } return false; } finally { releasePayload(response); } }
InvalidateSessionAndRetryOn401AndLogoutOnClose extends BackoffLimitedRetryHandler { @Override public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { try { switch (response.getStatusCode()) { case 401: authenticationResponseCache.invalidateAll(); return super.shouldRetryRequest(command, response); } return false; } finally { releasePayload(response); } } }
InvalidateSessionAndRetryOn401AndLogoutOnClose extends BackoffLimitedRetryHandler { @Override public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { try { switch (response.getStatusCode()) { case 401: authenticationResponseCache.invalidateAll(); return super.shouldRetryRequest(command, response); } return false; } finally { releasePayload(response); } } @Inject protected  InvalidateSessionAndRetryOn401AndLogoutOnClose(LoadingCache<Credentials, LoginResponse> authenticationResponseCache,
                                                            SessionApi sessionClient); }
InvalidateSessionAndRetryOn401AndLogoutOnClose extends BackoffLimitedRetryHandler { @Override public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { try { switch (response.getStatusCode()) { case 401: authenticationResponseCache.invalidateAll(); return super.shouldRetryRequest(command, response); } return false; } finally { releasePayload(response); } } @Inject protected  InvalidateSessionAndRetryOn401AndLogoutOnClose(LoadingCache<Credentials, LoginResponse> authenticationResponseCache,
                                                            SessionApi sessionClient); @Override boolean shouldRetryRequest(HttpCommand command, HttpResponse response); @PreDestroy void logoutOnClose(); }
InvalidateSessionAndRetryOn401AndLogoutOnClose extends BackoffLimitedRetryHandler { @Override public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { try { switch (response.getStatusCode()) { case 401: authenticationResponseCache.invalidateAll(); return super.shouldRetryRequest(command, response); } return false; } finally { releasePayload(response); } } @Inject protected  InvalidateSessionAndRetryOn401AndLogoutOnClose(LoadingCache<Credentials, LoginResponse> authenticationResponseCache,
                                                            SessionApi sessionClient); @Override boolean shouldRetryRequest(HttpCommand command, HttpResponse response); @PreDestroy void logoutOnClose();  }