@Test public void should_not_throw_exception_if_number_of_retries_not_set() { AtomicInteger counter = new AtomicInteger(); DelegateRetry<Void, VitamRuntimeException> delegate = () -> { counter.incrementAndGet(); if (counter.get() < 3) { throw new VitamRuntimeException("throw"); } return null; }; RetryableParameters parameters = new RetryableParameters(5, 0, 0, 1, MILLISECONDS); RetryableOnException<Void, VitamRuntimeException> retryable = new RetryableOnException<>(parameters); ThrowingCallable runRetryable = () -> retryable.exec(delegate); assertThatCode(runRetryable).doesNotThrowAnyException(); }
@Override public T exec(DelegateRetry<T, E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { try { return delegate.call(); } catch (Exception e) { boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(e); if (attemptExceedNbRetry || shouldStopRetry) { throw e; } sleep(counter.get(), delegate.toString(), param, randomSleep, onException, e); } } throw new IllegalStateException("Unreachable statement."); }
RetryableOnException implements Retryable<T, E> { @Override public T exec(DelegateRetry<T, E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { try { return delegate.call(); } catch (Exception e) { boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(e); if (attemptExceedNbRetry || shouldStopRetry) { throw e; } sleep(counter.get(), delegate.toString(), param, randomSleep, onException, e); } } throw new IllegalStateException("Unreachable statement."); } }
RetryableOnException implements Retryable<T, E> { @Override public T exec(DelegateRetry<T, E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { try { return delegate.call(); } catch (Exception e) { boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(e); if (attemptExceedNbRetry || shouldStopRetry) { throw e; } sleep(counter.get(), delegate.toString(), param, randomSleep, onException, e); } } throw new IllegalStateException("Unreachable statement."); } RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn, Consumer<Exception> onException);  RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn);  RetryableOnException(RetryableParameters param); }
RetryableOnException implements Retryable<T, E> { @Override public T exec(DelegateRetry<T, E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { try { return delegate.call(); } catch (Exception e) { boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(e); if (attemptExceedNbRetry || shouldStopRetry) { throw e; } sleep(counter.get(), delegate.toString(), param, randomSleep, onException, e); } } throw new IllegalStateException("Unreachable statement."); } RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn, Consumer<Exception> onException);  RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn);  RetryableOnException(RetryableParameters param); @Override T exec(DelegateRetry<T, E> delegate); @Override void execute(DelegateRetryVoid<E> delegate); }
RetryableOnException implements Retryable<T, E> { @Override public T exec(DelegateRetry<T, E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { try { return delegate.call(); } catch (Exception e) { boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(e); if (attemptExceedNbRetry || shouldStopRetry) { throw e; } sleep(counter.get(), delegate.toString(), param, randomSleep, onException, e); } } throw new IllegalStateException("Unreachable statement."); } RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn, Consumer<Exception> onException);  RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn);  RetryableOnException(RetryableParameters param); @Override T exec(DelegateRetry<T, E> delegate); @Override void execute(DelegateRetryVoid<E> delegate);  }