@Test public void should_retry_on_predicate() { AtomicInteger counter = new AtomicInteger(); DelegateRetry<Integer, VitamRuntimeException> delegate = () -> { counter.incrementAndGet(); return counter.get(); }; RetryableParameters parameters = new RetryableParameters(3, 1, 1, 1, MILLISECONDS); RetryableOnResult<Integer, VitamRuntimeException> retryableOnResult = new RetryableOnResult<>(parameters, r -> r <= 3); Integer result = retryableOnResult.exec(delegate); assertThat(result).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);  }