protected void verifyRedirectRoutes(HttpRequest request, HttpResponse response, HttpRequest expected) { HttpCommand command = createMock(HttpCommand.class); expect(command.isReplayable()).andReturn(true); expect(command.incrementRedirectCount()).andReturn(0); expect(command.getCurrentRequest()).andReturn(request); command.setCurrentRequest(expected); replay(command); RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class); assert retry.shouldRetryRequest(command, response); verify(command); } }
protected void verifyRedirectRoutes(HttpRequest request, HttpResponse response, HttpRequest expected) { HttpCommand command = createMock(HttpCommand.class); expect(command.isReplayable()).andReturn(true); expect(command.incrementRedirectCount()).andReturn(0); expect(command.getCurrentRequest()).andReturn(request); command.setCurrentRequest(expected); replay(command); RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class); assert retry.shouldRetryRequest(command, response); verify(command); } }
@Test public void test302DoesNotRetry() { HttpCommand command = createMock(HttpCommand.class); HttpResponse response = HttpResponse.builder() .statusCode(302) .message("HTTP/1.1 302 Found").build(); expect(command.isReplayable()).andReturn(true); expect(command.incrementRedirectCount()).andReturn(0); replay(command); RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class); assert !retry.shouldRetryRequest(command, response); verify(command); }
@Override public boolean shouldRetryRequest(final HttpCommand command, final HttpResponse response) { command.incrementFailureCount(); // Do not retry client errors that are not rate limit errors if (response.getStatusCode() != rateLimitErrorStatus()) { return false; } else if (!command.isReplayable()) { logger.error("Cannot retry after rate limit error, command is not replayable: %1$s", command); return false; } else if (command.getFailureCount() > retryCountLimit) { logger.error("Cannot retry after rate limit error, command has exceeded retry limit %1$d: %2$s", retryCountLimit, command); return false; } else { return delayRequestUntilAllowed(command, response); } }
@Override public boolean shouldRetryRequest(final HttpCommand command, final HttpResponse response) { command.incrementFailureCount(); // Do not retry client errors that are not rate limit errors if (response.getStatusCode() != rateLimitErrorStatus()) { return false; } else if (!command.isReplayable()) { logger.error("Cannot retry after rate limit error, command is not replayable: %1$s", command); return false; } else if (command.getFailureCount() > retryCountLimit) { logger.error("Cannot retry after rate limit error, command has exceeded retry limit %1$d: %2$s", retryCountLimit, command); return false; } else { return delayRequestUntilAllowed(command, response); } }
@Test public void test302DoesNotRetry() { HttpCommand command = createMock(HttpCommand.class); HttpResponse response = HttpResponse.builder() .statusCode(302) .message("HTTP/1.1 302 Found").build(); expect(command.isReplayable()).andReturn(true); expect(command.incrementRedirectCount()).andReturn(0); replay(command); RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class); assert !retry.shouldRetryRequest(command, response); verify(command); }
@Test public void test302DoesNotRetryAfterLimit() { HttpCommand command = createMock(HttpCommand.class); HttpResponse response = HttpResponse.builder() .statusCode(302) .message("HTTP/1.1 302 Found") .addHeader(LOCATION, "/api/v0.8b-ext2.5/Error.aspx?aspxerrorpath=/api/v0.8b-ext2.5/org.svc/1906645").build(); expect(command.isReplayable()).andReturn(true); expect(command.incrementRedirectCount()).andReturn(6); replay(command); RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class); assert !retry.shouldRetryRequest(command, response); verify(command); }
@SuppressWarnings("unchecked") @Test public void testUnauthorizedShouldInvalidateSessionAndRetry() { HttpCommand command = createMock(HttpCommand.class); Supplier<Credentials> creds = createMock(Supplier.class); LoadingCache<Credentials, Session> sessionCache = createMock(LoadingCache.class); SessionApi sessionApi = createMock(SessionApi.class); sessionCache.invalidateAll(); expectLastCall(); expect(command.incrementFailureCount()).andReturn(1); expect(command.isReplayable()).andReturn(true); expect(command.getFailureCount()).andReturn(1).atLeastOnce(); replay(creds, sessionCache, sessionApi, command); HttpResponse response = HttpResponse.builder().statusCode(UNAUTHORIZED.getStatusCode()).build(); SessionManager retry = new SessionManager(creds, sessionCache, sessionApi); assertTrue(retry.shouldRetryRequest(command, response)); verify(creds, sessionCache, sessionApi, command); }
@Test public void test504DoesRetry() { AWSUtils utils = createMock(AWSUtils.class); HttpCommand command = createMock(HttpCommand.class); expect(command.getFailureCount()).andReturn(1).anyTimes(); expect(command.incrementFailureCount()).andReturn(1); expect(command.isReplayable()).andReturn(true); replay(utils, command); AWSServerErrorRetryHandler retry = new AWSServerErrorRetryHandler(utils, ImmutableSet.<String> of()); assertTrue(retry.shouldRetryRequest(command, HttpResponse.builder().statusCode(504).build())); verify(utils, command); } }
@Override public boolean shouldRetryRequest(final HttpCommand command, final HttpResponse response) { command.incrementFailureCount(); // Do not retry client errors that are not rate limit errors if (response.getStatusCode() != rateLimitErrorStatus()) { return false; } else if (!command.isReplayable()) { logger.error("Cannot retry after rate limit error, command is not replayable: %1$s", command); return false; } else if (command.getFailureCount() > retryCountLimit) { logger.error("Cannot retry after rate limit error, command has exceeded retry limit %1$d: %2$s", retryCountLimit, command); return false; } else { return delayRequestUntilAllowed(command, response); } }
@Test public void test302DoesNotRetryAfterLimit() { HttpCommand command = createMock(HttpCommand.class); HttpResponse response = HttpResponse.builder() .statusCode(302) .message("HTTP/1.1 302 Found") .addHeader(LOCATION, "/api/v0.8b-ext2.5/Error.aspx?aspxerrorpath=/api/v0.8b-ext2.5/org.svc/1906645").build(); expect(command.isReplayable()).andReturn(true); expect(command.incrementRedirectCount()).andReturn(6); replay(command); RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class); assert !retry.shouldRetryRequest(command, response); verify(command); }
@SuppressWarnings("unchecked") @Test public void testIPMismatchShouldInvalidateSessionAndRetry() throws IOException { HttpCommand command = createMock(HttpCommand.class); Supplier<Credentials> creds = createMock(Supplier.class); LoadingCache<Credentials, Session> sessionCache = createMock(LoadingCache.class); SessionApi sessionApi = createMock(SessionApi.class); sessionCache.invalidateAll(); expectLastCall(); expect(command.incrementFailureCount()).andReturn(1); expect(command.isReplayable()).andReturn(true); expect(command.getFailureCount()).andReturn(1).atLeastOnce(); replay(creds, sessionCache, sessionApi, command); HttpResponse response = HttpResponse.builder() .statusCode(BAD_REQUEST.getStatusCode()) .payload(getResource("ip_mismatch.json").openStream()) .build(); SessionManager retry = new SessionManager(creds, sessionCache, sessionApi); assertTrue(retry.shouldRetryRequest(command, response)); verify(creds, sessionCache, sessionApi, command); }
@SuppressWarnings("unchecked") @Test public void testUnauthorizedShouldInvalidateSessionAndRetry() { HttpCommand command = createMock(HttpCommand.class); Supplier<Credentials> creds = createMock(Supplier.class); LoadingCache<Credentials, Session> sessionCache = createMock(LoadingCache.class); SessionApi sessionApi = createMock(SessionApi.class); sessionCache.invalidateAll(); expectLastCall(); expect(command.incrementFailureCount()).andReturn(1); expect(command.isReplayable()).andReturn(true); expect(command.getFailureCount()).andReturn(1).atLeastOnce(); replay(creds, sessionCache, sessionApi, command); HttpResponse response = HttpResponse.builder().statusCode(UNAUTHORIZED.getStatusCode()).build(); SessionManager retry = new SessionManager(creds, sessionCache, sessionApi); assertTrue(retry.shouldRetryRequest(command, response)); verify(creds, sessionCache, sessionApi, command); }
@SuppressWarnings("unchecked") @Test public void testIPMismatchShouldInvalidateSessionAndRetry() throws IOException { HttpCommand command = createMock(HttpCommand.class); Supplier<Credentials> creds = createMock(Supplier.class); LoadingCache<Credentials, Session> sessionCache = createMock(LoadingCache.class); SessionApi sessionApi = createMock(SessionApi.class); sessionCache.invalidateAll(); expectLastCall(); expect(command.incrementFailureCount()).andReturn(1); expect(command.isReplayable()).andReturn(true); expect(command.getFailureCount()).andReturn(1).atLeastOnce(); replay(creds, sessionCache, sessionApi, command); HttpResponse response = HttpResponse.builder() .statusCode(BAD_REQUEST.getStatusCode()) .payload(getResource("ip_mismatch.json").openStream()) .build(); SessionManager retry = new SessionManager(creds, sessionCache, sessionApi); assertTrue(retry.shouldRetryRequest(command, response)); verify(creds, sessionCache, sessionApi, command); }
private boolean ifReplayableBackoffAndReturnTrue(HttpCommand command) { command.incrementFailureCount(); if (!command.isReplayable()) { logger.error("Cannot retry after server error, command is not replayable: %1$s", command); return false; } else if (command.getFailureCount() > retryCountLimit) { logger.error("Cannot retry after server error, command has exceeded retry limit %1$d: %2$s", retryCountLimit, command); return false; } else { imposeBackoffExponentialDelay(command.getFailureCount(), "server error: " + command.toString()); return true; } }
private boolean ifReplayableBackoffAndReturnTrue(HttpCommand command) { command.incrementFailureCount(); if (!command.isReplayable()) { logger.error("Cannot retry after server error, command is not replayable: %1$s", command); return false; } else if (command.getFailureCount() > retryCountLimit) { logger.error("Cannot retry after server error, command has exceeded retry limit %1$d: %2$s", retryCountLimit, command); return false; } else { imposeBackoffExponentialDelay(command.getFailureCount(), "server error: " + command.toString()); return true; } }
private boolean ifReplayableBackoffAndReturnTrue(HttpCommand command) { command.incrementFailureCount(); if (!command.isReplayable()) { logger.error("Cannot retry after server error, command is not replayable: %1$s", command); return false; } else if (command.getFailureCount() > retryCountLimit) { logger.error("Cannot retry after server error, command has exceeded retry limit %1$d: %2$s", retryCountLimit, command); return false; } else { imposeBackoffExponentialDelay(command.getFailureCount(), "server error: " + command.toString()); return true; } }
private boolean ifReplayableBackoffAndReturnTrue(HttpCommand command) { command.incrementFailureCount(); if (!command.isReplayable()) { logger.error("Cannot retry after server error, command is not replayable: %1$s", command); return false; } else if (command.getFailureCount() > retryCountLimit) { logger.error("Cannot retry after server error, command has exceeded retry limit %1$d: %2$s", retryCountLimit, command); return false; } else { imposeBackoffExponentialDelay(command.getFailureCount(), "server error: " + command.toString()); return true; } }
private boolean ifReplayableBackoffAndReturnTrue(HttpCommand command) { command.incrementFailureCount(); if (!command.isReplayable()) { logger.error("Cannot retry after server error, command is not replayable: %1$s", command); return false; } else if (command.getFailureCount() > retryCountLimit) { logger.error("Cannot retry after server error, command has exceeded retry limit %1$d: %2$s", retryCountLimit, command); return false; } else { imposeBackoffExponentialDelay(command.getFailureCount(), "server error: " + command.toString()); return true; } }
private boolean ifReplayableBackoffAndReturnTrue(HttpCommand command) { command.incrementFailureCount(); if (!command.isReplayable()) { logger.error("Cannot retry after server error, command is not replayable: %1$s", command); return false; } else if (command.getFailureCount() > retryCountLimit) { logger.error("Cannot retry after server error, command has exceeded retry limit %1$d: %2$s", retryCountLimit, command); return false; } else { imposeBackoffExponentialDelay(command.getFailureCount(), "server error: " + command.toString()); return true; } }