@Test void shouldNotRetry_whenConditionFails_onInitialAttempt() throws Throwable { RetryCondition condition = mock(RetryCondition.class); when(condition.test(any(RetryContext.class))).thenReturn(false); when(client.request(any(Request.class))).thenReturn(this.response); this.conditionalRetry = new ConditionalRetry(3, condition, retryContext -> 1L, logger, exceptionHandler); Response response = this.conditionalRetry.execute("test", this.request, client::request); verify(this.client, times(1)).request(any(Request.class)); verifyZeroInteractions(this.logger); assertThat(response).isNotNull(); }
@Override public Response execute( String methodName, Request request, Function<Request, Response> callback) throws Throwable { int attempts = 0; Response response = null; Throwable lastException = null; boolean shouldRetry = true; while (shouldRetry) { attempts++; try { response = callback.apply(request); } catch (Exception ex) { lastException = this.exceptionHandler.apply(ex); } RetryContext context = new RetryContext(attempts, lastException, response); shouldRetry = this.condition.test(context) && (attempts < this.maxAttempts); if (shouldRetry) { this.logger.logRetry(methodName, context); long interval = this.interval.getInterval(context); if (interval > 0) { try { Thread.sleep(interval); } catch (InterruptedException ie) { log.info("Retry interval was interrupted. Starting next retry attempt."); } } } } if (lastException != null) { throw lastException; } return response; }
ConditionalRetry implements Retry { @Override public Response execute( String methodName, Request request, Function<Request, Response> callback) throws Throwable { int attempts = 0; Response response = null; Throwable lastException = null; boolean shouldRetry = true; while (shouldRetry) { attempts++; try { response = callback.apply(request); } catch (Exception ex) { lastException = this.exceptionHandler.apply(ex); } RetryContext context = new RetryContext(attempts, lastException, response); shouldRetry = this.condition.test(context) && (attempts < this.maxAttempts); if (shouldRetry) { this.logger.logRetry(methodName, context); long interval = this.interval.getInterval(context); if (interval > 0) { try { Thread.sleep(interval); } catch (InterruptedException ie) { log.info("Retry interval was interrupted. Starting next retry attempt."); } } } } if (lastException != null) { throw lastException; } return response; } }
ConditionalRetry implements Retry { @Override public Response execute( String methodName, Request request, Function<Request, Response> callback) throws Throwable { int attempts = 0; Response response = null; Throwable lastException = null; boolean shouldRetry = true; while (shouldRetry) { attempts++; try { response = callback.apply(request); } catch (Exception ex) { lastException = this.exceptionHandler.apply(ex); } RetryContext context = new RetryContext(attempts, lastException, response); shouldRetry = this.condition.test(context) && (attempts < this.maxAttempts); if (shouldRetry) { this.logger.logRetry(methodName, context); long interval = this.interval.getInterval(context); if (interval > 0) { try { Thread.sleep(interval); } catch (InterruptedException ie) { log.info("Retry interval was interrupted. Starting next retry attempt."); } } } } if (lastException != null) { throw lastException; } return response; } ConditionalRetry(
      int maxAttempts, RetryCondition condition, RetryInterval interval, Logger logger,
      ExceptionHandler exceptionHandler); }
ConditionalRetry implements Retry { @Override public Response execute( String methodName, Request request, Function<Request, Response> callback) throws Throwable { int attempts = 0; Response response = null; Throwable lastException = null; boolean shouldRetry = true; while (shouldRetry) { attempts++; try { response = callback.apply(request); } catch (Exception ex) { lastException = this.exceptionHandler.apply(ex); } RetryContext context = new RetryContext(attempts, lastException, response); shouldRetry = this.condition.test(context) && (attempts < this.maxAttempts); if (shouldRetry) { this.logger.logRetry(methodName, context); long interval = this.interval.getInterval(context); if (interval > 0) { try { Thread.sleep(interval); } catch (InterruptedException ie) { log.info("Retry interval was interrupted. Starting next retry attempt."); } } } } if (lastException != null) { throw lastException; } return response; } ConditionalRetry(
      int maxAttempts, RetryCondition condition, RetryInterval interval, Logger logger,
      ExceptionHandler exceptionHandler); static ConditionalRetryBuilder builder(
      int maxAttempts, Logger logger, ExceptionHandler exceptionHandler); @Override Response execute(
      String methodName, Request request, Function<Request, Response> callback); }
ConditionalRetry implements Retry { @Override public Response execute( String methodName, Request request, Function<Request, Response> callback) throws Throwable { int attempts = 0; Response response = null; Throwable lastException = null; boolean shouldRetry = true; while (shouldRetry) { attempts++; try { response = callback.apply(request); } catch (Exception ex) { lastException = this.exceptionHandler.apply(ex); } RetryContext context = new RetryContext(attempts, lastException, response); shouldRetry = this.condition.test(context) && (attempts < this.maxAttempts); if (shouldRetry) { this.logger.logRetry(methodName, context); long interval = this.interval.getInterval(context); if (interval > 0) { try { Thread.sleep(interval); } catch (InterruptedException ie) { log.info("Retry interval was interrupted. Starting next retry attempt."); } } } } if (lastException != null) { throw lastException; } return response; } ConditionalRetry(
      int maxAttempts, RetryCondition condition, RetryInterval interval, Logger logger,
      ExceptionHandler exceptionHandler); static ConditionalRetryBuilder builder(
      int maxAttempts, Logger logger, ExceptionHandler exceptionHandler); @Override Response execute(
      String methodName, Request request, Function<Request, Response> callback);  }