@Test public void should_retry_on_predicate_times() { AtomicInteger counter = new AtomicInteger(); int nbRetry = 10; Predicate<Integer> shouldRetryOnPredicate = r -> r < 3; RetryableParameters parameters = new RetryableParameters(nbRetry, 1, 1, 1, MILLISECONDS); RetryableOnResult<Integer, VitamRuntimeException> retryableOnResult = new RetryableOnResult<>(parameters, shouldRetryOnPredicate); Integer nbRetryDone = retryableOnResult.exec(counter::getAndIncrement); assertThat(nbRetryDone).isEqualTo(3); }
@Override public T exec(DelegateRetry<T, E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { T result = delegate.call(); boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(result); if (attemptExceedNbRetry || shouldStopRetry) { return result; } sleep(counter.get(), delegate.toString(), param, randomSleep, onResult, result); } throw new IllegalStateException("Unreachable statement."); }
RetryableOnResult implements Retryable<T, E> { @Override public T exec(DelegateRetry<T, E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { T result = delegate.call(); boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(result); if (attemptExceedNbRetry || shouldStopRetry) { return result; } sleep(counter.get(), delegate.toString(), param, randomSleep, onResult, result); } throw new IllegalStateException("Unreachable statement."); } }
RetryableOnResult implements Retryable<T, E> { @Override public T exec(DelegateRetry<T, E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { T result = delegate.call(); boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(result); if (attemptExceedNbRetry || shouldStopRetry) { return result; } sleep(counter.get(), delegate.toString(), param, randomSleep, onResult, result); } throw new IllegalStateException("Unreachable statement."); } RetryableOnResult(RetryableParameters param, Predicate<T> retryOn, Consumer<T> onResult);  RetryableOnResult(RetryableParameters param, Predicate<T> retryOn); }
RetryableOnResult implements Retryable<T, E> { @Override public T exec(DelegateRetry<T, E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { T result = delegate.call(); boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(result); if (attemptExceedNbRetry || shouldStopRetry) { return result; } sleep(counter.get(), delegate.toString(), param, randomSleep, onResult, result); } throw new IllegalStateException("Unreachable statement."); } RetryableOnResult(RetryableParameters param, Predicate<T> retryOn, Consumer<T> onResult);  RetryableOnResult(RetryableParameters param, Predicate<T> retryOn); @Override T exec(DelegateRetry<T, E> delegate); @Override void execute(DelegateRetryVoid<E> delegate); }
RetryableOnResult implements Retryable<T, E> { @Override public T exec(DelegateRetry<T, E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { T result = delegate.call(); boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(result); if (attemptExceedNbRetry || shouldStopRetry) { return result; } sleep(counter.get(), delegate.toString(), param, randomSleep, onResult, result); } throw new IllegalStateException("Unreachable statement."); } RetryableOnResult(RetryableParameters param, Predicate<T> retryOn, Consumer<T> onResult);  RetryableOnResult(RetryableParameters param, Predicate<T> retryOn); @Override T exec(DelegateRetry<T, E> delegate); @Override void execute(DelegateRetryVoid<E> delegate);  }