Callables { @Deprecated @Nullable public static <T, EX extends Exception> T executeWithRetry(final TimeoutCallable<T, EX> what, final int nrImmediateRetries, final int maxRetryWaitMillis, final Class<EX> exceptionClass) throws InterruptedException, EX, TimeoutException { return executeWithRetry(what, nrImmediateRetries, maxRetryWaitMillis, (TimeoutRetryPredicate<? super T, T>) TimeoutRetryPredicate.NORETRY_FOR_RESULT, DEFAULT_EXCEPTION_RETRY, exceptionClass); } private  Callables(); @Deprecated @Nullable static T executeWithRetry(final TimeoutCallable<T, EX> what,
          final int nrImmediateRetries,
          final int maxRetryWaitMillis, final Class<EX> exceptionClass); @Deprecated @Nullable static T executeWithRetry(final TimeoutCallable<T, RuntimeException> what,
          final int nrImmediateRetries, final int maxRetryWaitMillis); @Deprecated @Nullable static T executeWithRetry(final TimeoutCallable<T, EX> what,
          final int nrImmediateRetries,
          final int maxRetryWaitMillis,
          final AdvancedRetryPredicate<Exception> retryOnException,
          final Class<EX> exceptionClass); @Deprecated @Nullable static T executeWithRetry(final TimeoutCallable<T, RuntimeException> what,
          final int nrImmediateRetries, final int maxRetryWaitMillis,
          final AdvancedRetryPredicate<Exception> retryOnException); @Deprecated @Nullable static T executeWithRetry(final TimeoutCallable<T, EX> what,
          final int nrImmediateRetries, final int maxWaitMillis,
          final TimeoutRetryPredicate<? super T, T> retryOnReturnVal,
          final AdvancedRetryPredicate<Exception> retryOnException,
          final Class<EX> exceptionClass); @Deprecated static Class rootClass(final Exception f); @Deprecated @Nullable static T executeWithRetry(final TimeoutCallable<T, EX> what,
          final TimeoutRetryPredicate<? super T, T> retryOnReturnVal,
          final TimeoutRetryPredicate<Exception, T> retryOnException, final Class<EX> exceptionClass); @Deprecated @Nullable static T executeWithRetry(final TimeoutCallable<T, EX> what,
          final TimeoutRetryPredicate<Exception, T> retryOnException, final Class<EX> exceptionClass); @Deprecated @SuppressFBWarnings({ "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", "MDM_THREAD_YIELD" }) @Nullable static T executeWithRetry(
          final CheckedCallable<T, EX> pwhat,
          final RetryPredicate<? super T, T> retryOnReturnVal,
          final RetryPredicate<Exception, T> retryOnException,
          final Class<EX> exceptionClass); static Callable<T> synchronize(final Callable<T> callable); static Callable<T> withName(final Callable<T> callable, final String name); static MemorizedCallable<V> memorized(final Callable<V> source); static Callable<V> constant(final V value); static Callable<Void> from(final Runnable value); @Deprecated
  static final SimpleRetryPredicate<?> RETRY_FOR_NULL_RESULT; @Deprecated
  static final AdvancedRetryPredicate<Exception> DEFAULT_EXCEPTION_RETRY; @Deprecated
  static final Predicate<Exception> DEFAULT_EXCEPTION_RETRY_PREDICATE; }
@Test(expected = TestException.class) public void testExceptionPropagation() throws Exception { Callables.executeWithRetry(new TimeoutCallable<Integer, TestException>(60000) { @Override public Integer call(final long deadline) throws TestException { throw new TestException(); } }, 3, 10, TestException.class); }
@Test(expected = TimeoutException.class) public void testExceptionPropagation2() throws InterruptedException, TimeoutException { Callables.executeWithRetry(new TimeoutCallable<Integer, RuntimeException>(3000) { @Override public Integer call(final long deadline) throws TimeoutException { throw new TimeoutException(); } }, 3, 10, RuntimeException.class); }
@Test public void testExecuteWithRetry4args1() throws Exception { Integer result = Callables.executeWithRetry(new TimeoutCallable<Integer, RuntimeException>(60000) { @Override public Integer call(final long deadline) { return 1; } }, 3, 10, RuntimeException.class); Assert.assertEquals(1L, result.longValue()); }
@Test public void testExecuteWithRetry4args2() throws Exception { long startTime = TimeSource.nanoTime(); Integer result = Callables.executeWithRetry(new TimeoutCallable<Integer, IOException>(60000) { private int count; @Override public Integer call(final long deadline) throws IOException { count++; if (count < 20) { throw new SocketException("Aaaaaaaaaaa" + count); } return 1; } }, 1, 10, IOException.class); long elapsedTime = TimeSource.nanoTime() - startTime; Assert.assertEquals(1L, result.longValue()); Assert.assertTrue("Operation has to take at least 10 ms", elapsedTime > TimeUnit.MILLISECONDS.toNanos(10)); }
@Test public void testExecuteWithRetryFailureTest() throws IOException, InterruptedException { try { Callables.executeWithRetry(new TimeoutCallable<Integer, IOException>(100) { @Override public Integer call(final long deadline) throws IOException { throw new SocketException("Aaaaaaaaaaa " + System.currentTimeMillis()); } }, 4, 100, IOException.class); Assert.fail("Should not get here"); } catch (IOException | TimeoutException e) { Assert.assertTrue("must have supressed exceptions: " + com.google.common.base.Throwables.getStackTraceAsString(e), Throwables.getSuppressed(e).length >= 1); } }
@Test @SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON") public void testSuppression() throws InterruptedException, IOException, TimeoutException { long startTime = System.currentTimeMillis(); Integer result = Callables.executeWithRetry(new TimeoutCallable<Integer, IOException>(60000) { private int count; @Override public Integer call(final long deadline) throws IOException { count++; if (count < 15) { throw new SocketException("Aaaaaaaaaaa" + count); } return 1; } }, 1, 20, new AdvancedRetryPredicate<Exception>() { @Override public AdvancedAction apply(final Exception input) { return AdvancedAction.RETRY; } }, IOException.class); long elapsedTime = System.currentTimeMillis() - startTime; Assert.assertEquals(1L, result.longValue()); Assert.assertTrue("Operation has to take at least 10 ms and not " + elapsedTime, elapsedTime > 10L); }
@Test public void testExecuteWithRetryTimeout() { try { Callables.executeWithRetry(new TimeoutCallable<Integer, IOException>(1000) { private int count; @Override @SuppressFBWarnings("MDM_THREAD_YIELD") public Integer call(final long deadline) throws IOException, InterruptedException { Thread.sleep(2000); count++; if (count < 5) { throw new SocketException("Aaaaaaaaaaa" + count); } return 1; } }, 1, 10, IOException.class); Assert.fail(); } catch (InterruptedException | IOException | TimeoutException ex) { Throwables.writeTo(ex, System.err, Throwables.PackageDetail.NONE); } }
@Test(expected = TimeoutException.class) public void testExecuteWithRetryTimeout2() throws InterruptedException, IOException, TimeoutException { Callables.executeWithRetry(new TimeoutCallable<Integer, IOException>(1000) { private int count = 0; @Override public Integer call(final long deadline) throws IOException { LOG.debug("Exec {}", this); count++; if (count < 200) { throw new SocketException("Aaaaaaaaaaa" + count); } return 1; } }, 0, 100, IOException.class); Assert.fail(); }