@Test(timeOut = 60000) public void testFirstTrySuccess() throws Exception { TestingMethodInvocationStat stat = new TestingMethodInvocationStat(); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation(RetryPolicy.NO_RETRY_POLICY, stat, () -> immediateFuture(SUCCESS)); assertEquals(methodInvocation.get(), SUCCESS); stat.assertSuccess(0); }
private static void testExceptionFromInvokerInvoke(int expectedRetries) throws Exception { RetryPolicy retryPolicy = new RetryPolicy(new DriftClientConfig().setMaxRetries(expectedRetries + 10), new TestingExceptionClassifier()); TestingMethodInvocationStat stat = new TestingMethodInvocationStat(); AtomicInteger attempts = new AtomicInteger(); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation( retryPolicy, stat, () -> { attempts.getAndIncrement(); if (attempts.get() > expectedRetries) { throw UNEXPECTED_EXCEPTION; } return immediateFailedFuture(createClassifiedException(true, NORMAL)); }); try { methodInvocation.get(); fail("Expected exception"); } catch (ExecutionException e) { assertEquals(attempts.get(), expectedRetries + 1); assertUnexpectedException(e.getCause()); } }
@Test(timeOut = 60000) public void testMaxRetries() throws Exception { int maxRetries = 5; RetryPolicy retryPolicy = new RetryPolicy(new DriftClientConfig().setMaxRetries(maxRetries), new TestingExceptionClassifier()); TestingMethodInvocationStat stat = new TestingMethodInvocationStat(); AtomicInteger attempts = new AtomicInteger(); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation(retryPolicy, stat, () -> { attempts.getAndIncrement(); return immediateFailedFuture(createClassifiedException(true, NORMAL)); }); try { methodInvocation.get(); fail("Expected exception"); } catch (ExecutionException e) { assertEquals(attempts.get(), maxRetries + 1); assertClassifiedException(e.getCause(), new ExceptionClassification(Optional.of(true), NORMAL), maxRetries); } stat.assertFailure(maxRetries); }
methodInvocation.get(); fail("Expected exception");
@Test(timeOut = 60000) public void testBasicRetriesToNoHosts() throws Exception { RetryPolicy retryPolicy = new RetryPolicy(new DriftClientConfig().setMaxRetries(10), new TestingExceptionClassifier()); TestingMethodInvocationStat stat = new TestingMethodInvocationStat(); AtomicInteger attempts = new AtomicInteger(); int expectedRetries = 3; DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation( retryPolicy, stat, new MockMethodInvoker(() -> { attempts.getAndIncrement(); return immediateFailedFuture(createClassifiedException(true, NORMAL)); }), new TestingAddressSelector(expectedRetries + 1), systemTicker()); try { methodInvocation.get(); fail("Expected exception"); } catch (ExecutionException e) { assertEquals(attempts.get(), expectedRetries + 1); assertClassifiedException(e.getCause(), new ExceptionClassification(Optional.of(true), NORMAL), expectedRetries); } stat.assertFailure(expectedRetries); }
methodInvocation.get(); fail("Expected exception");
methodInvocation.get(); fail("Expected exception");
methodInvocation.get(); fail("Expected exception");
methodInvocation.get(); fail("Expected exception");
methodInvocation.get(); fail("Expected exception");
private static void testBasicRetriesToSuccess(int expectedRetries, boolean wrapWithApplicationException) throws Exception { RetryPolicy retryPolicy = new RetryPolicy( new DriftClientConfig() .setMaxRetries(expectedRetries + 10) .setMinBackoffDelay(new Duration(1, SECONDS)) .setMaxBackoffDelay(new Duration(1, DAYS)) .setBackoffScaleFactor(2.0), new TestingExceptionClassifier()); TestingMethodInvocationStat stat = new TestingMethodInvocationStat(); AtomicInteger attempts = new AtomicInteger(); MockMethodInvoker invoker = new MockMethodInvoker(() -> { int currentAttempts = attempts.getAndIncrement(); if (currentAttempts < expectedRetries) { return immediateFailedFuture(createClassifiedException(true, NORMAL, wrapWithApplicationException)); } return immediateFuture(SUCCESS); }); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation(retryPolicy, stat, invoker, new TestingAddressSelector(100), systemTicker()); assertEquals(methodInvocation.get(), SUCCESS); assertEquals(attempts.get(), expectedRetries + 1); stat.assertSuccess(expectedRetries); assertDelays(invoker, retryPolicy, expectedRetries); }
methodInvocation.get(); fail("Expected exception");
methodInvocation.get(); fail("Expected exception");