### Question:
BackoffLimitedRetryHandler implements HttpRetryHandler, IOExceptionRetryHandler { public boolean shouldRetryRequest(HttpCommand command, IOException error) { return ifReplayableBackoffAndReturnTrue(command); }  boolean shouldRetryRequest(HttpCommand command, IOException error); boolean shouldRetryRequest(HttpCommand command, HttpResponse response); void imposeBackoffExponentialDelay(int failureCount, String commandDescription); void imposeBackoffExponentialDelay(long period, int pow, int failureCount, int max, String commandDescription); void imposeBackoffExponentialDelay(long period, long maxPeriod, int pow, int failureCount, int max,
            String commandDescription); static final BackoffLimitedRetryHandler INSTANCE; }### Answer:
@Test void testInputStreamIsNotClosed() throws SecurityException, NoSuchMethodException, IOException { HttpCommand command = createCommand(); HttpResponse response = HttpResponse.builder().statusCode(500).build(); InputStream inputStream = new InputStream() { int count = 2; @Override public void close() { fail("The retry handler should not close the response stream"); } @Override public int read() throws IOException { return count < 0 ? -1 : --count; } @Override public int available() throws IOException { return count < 0 ? 0 : count; } }; response.setPayload(Payloads.newInputStreamPayload(inputStream)); response.getPayload().getContentMetadata().setContentLength(1L); assertEquals(response.getPayload().openStream().available(), 2); assertEquals(response.getPayload().openStream().read(), 1); handler.shouldRetryRequest(command, response); assertEquals(response.getPayload().openStream().available(), 1); assertEquals(response.getPayload().openStream().read(), 0); }
@Test void testClosesInputStream() throws InterruptedException, IOException, SecurityException, NoSuchMethodException { HttpCommand command = createCommand(); HttpResponse response = HttpResponse.builder().statusCode(400).build(); InputStream inputStream = new InputStream() { boolean isOpen = true; @Override public void close() { this.isOpen = false; } int count = 1; @Override public int read() throws IOException { if (this.isOpen) return (count > -1) ? count-- : -1; else return -1; } @Override public int available() throws IOException { if (this.isOpen) return count; else return 0; } }; response.setPayload(Payloads.newInputStreamPayload(inputStream)); response.getPayload().getContentMetadata().setContentLength(1l); assertEquals(response.getPayload().getInput().available(), 1); assertEquals(response.getPayload().getInput().read(), 1); handler.shouldRetryRequest(command, response); assertEquals(response.getPayload().getInput().available(), 0); assertEquals(response.getPayload().getInput().read(), -1); }
@Test void testIncrementsFailureCount() throws InterruptedException, IOException, SecurityException, NoSuchMethodException { HttpCommand command = createCommand(); HttpResponse response = HttpResponse.builder().statusCode(400).build(); handler.shouldRetryRequest(command, response); assertEquals(command.getFailureCount(), 1); handler.shouldRetryRequest(command, response); assertEquals(command.getFailureCount(), 2); handler.shouldRetryRequest(command, response); assertEquals(command.getFailureCount(), 3); }
@Test void testDisallowsExcessiveRetries() throws InterruptedException, IOException, SecurityException, NoSuchMethodException { HttpCommand command = createCommand(); HttpResponse response = HttpResponse.builder().statusCode(400).build(); assertEquals(handler.shouldRetryRequest(command, response), true); assertEquals(handler.shouldRetryRequest(command, response), true); assertEquals(handler.shouldRetryRequest(command, response), true); assertEquals(handler.shouldRetryRequest(command, response), true); assertEquals(handler.shouldRetryRequest(command, response), true); assertEquals(handler.shouldRetryRequest(command, response), false); }