.retryIfException(Optional.ofNullable(throwablePredicate).orElse(exception -> true)) .retryIfResult(Optional.ofNullable(resultRetryPredicate).orElse(result -> false)) .withWaitStrategy(WaitStrategies.join( WaitStrategies.exponentialWait(1000, 90, TimeUnit.SECONDS), WaitStrategies.randomWait(100, TimeUnit.MILLISECONDS, 500, TimeUnit.MILLISECONDS)
.retryIfException(Optional.ofNullable(throwablePredicate).orElse(exception -> true)) .retryIfResult(Optional.ofNullable(resultRetryPredicate).orElse(result -> false)) .withWaitStrategy(WaitStrategies.join( WaitStrategies.exponentialWait(1000, 90, TimeUnit.SECONDS), WaitStrategies.randomWait(100, TimeUnit.MILLISECONDS, 500, TimeUnit.MILLISECONDS)
@Test public void testWhetherBuilderFailsForNullWaitStrategyWithCompositeStrategies() { try { RetryerBuilder.newBuilder() .withWaitStrategy(WaitStrategies.join(null, null)) .build(); fail("Exepcted to fail for null wait strategy"); } catch (IllegalStateException exception) { assertTrue(exception.getMessage().contains("Cannot have a null wait strategy")); } }
private <T> T retry(Callable<T> f) throws ExecutionException, RetryException { final Retryer<T> retryer = RetryerBuilder.<T>newBuilder() .retryIfException(Impl::isRetryableException) .withWaitStrategy(WaitStrategies.join(exponentialWait(), randomWait(1, SECONDS))) .withStopStrategy(retryStopStrategy) .withRetryListener(Impl::onRequestAttempt) .build(); return retryer.call(f); }
@Test public void testWithMoreThanOneWaitStrategyOneBeingIncremental() throws Exception { Callable<Boolean> callable = notNullAfter5Attempts(); Retryer retryer = RetryerBuilder.newBuilder() .withWaitStrategy(WaitStrategies.join( WaitStrategies.incrementingWait(10L, TimeUnit.MILLISECONDS, 10L, TimeUnit.MILLISECONDS), WaitStrategies.fibonacciWait(10, Long.MAX_VALUE, TimeUnit.MILLISECONDS))) .retryIfResult(Objects::isNull) .build(); long start = System.currentTimeMillis(); boolean result = retryer.call(callable); assertTrue(System.currentTimeMillis() - start >= 270L); assertTrue(result); }
@Test public void testWithMoreThanOneWaitStrategyOneBeingFixed() throws Exception { Callable<Boolean> callable = notNullAfter5Attempts(); Retryer retryer = RetryerBuilder.newBuilder() .withWaitStrategy(WaitStrategies.join( WaitStrategies.fixedWait(50L, TimeUnit.MILLISECONDS), WaitStrategies.fibonacciWait(10, Long.MAX_VALUE, TimeUnit.MILLISECONDS))) .retryIfResult(Objects::isNull) .build(); long start = System.currentTimeMillis(); boolean result = retryer.call(callable); assertTrue(System.currentTimeMillis() - start >= 370L); assertTrue(result); }