Search in sources :

Example 6 with RetryCallback

use of cn.taketoday.retry.RetryCallback in project today-framework by TAKETODAY.

the class RetryTemplateTests method testSpecificExceptionRetry.

@Test
public void testSpecificExceptionRetry() throws Throwable {
    for (int x = 1; x <= 10; x++) {
        final int attemptsBeforeSuccess = x;
        final AtomicInteger attempts = new AtomicInteger(0);
        RetryCallback<String, IllegalStateException> callback = new RetryCallback<String, IllegalStateException>() {

            @Override
            public String doWithRetry(RetryContext context) throws IllegalStateException {
                if (attempts.incrementAndGet() < attemptsBeforeSuccess) {
                    // can't really tell the difference between runtime exceptions.
                    throw new IllegalArgumentException("Planned");
                }
                return "foo";
            }
        };
        RetryTemplate retryTemplate = new RetryTemplate();
        retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
        retryTemplate.execute(callback);
        assertEquals(x, attempts.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Example 7 with RetryCallback

use of cn.taketoday.retry.RetryCallback in project today-framework by TAKETODAY.

the class StatefulRecoveryRetryTests method testSwitchToStatelessForNoRollback.

@Test
public void testSwitchToStatelessForNoRollback() throws Throwable {
    this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    // Roll back for these:
    BinaryExceptionClassifier classifier = new BinaryExceptionClassifier(Collections.<Class<? extends Throwable>>singleton(DataAccessException.class));
    // ...but not these:
    assertFalse(classifier.classify(new RuntimeException()));
    final String input = "foo";
    RetryState state = new DefaultRetryState(input, classifier);
    RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {

        @Override
        public String doWithRetry(RetryContext context) throws Exception {
            throw new RuntimeException("Barf!");
        }
    };
    RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {

        @Override
        public String recover(RetryContext context) {
            StatefulRecoveryRetryTests.this.count++;
            StatefulRecoveryRetryTests.this.list.add(input);
            return input;
        }
    };
    Object result = null;
    // On the second retry, the recovery path is taken...
    result = this.retryTemplate.execute(callback, recoveryCallback, state);
    // default result is the item
    assertEquals(input, result);
    assertEquals(1, this.count);
    assertEquals(input, this.list.get(0));
}
Also used : BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) DataAccessException(cn.taketoday.dao.DataAccessException) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Example 8 with RetryCallback

use of cn.taketoday.retry.RetryCallback in project today-framework by TAKETODAY.

the class StatefulRecoveryRetryTests method testRecover.

@Test
public void testRecover() throws Throwable {
    this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    final String input = "foo";
    RetryState state = new DefaultRetryState(input);
    RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {

        @Override
        public String doWithRetry(RetryContext context) throws Exception {
            throw new RuntimeException("Barf!");
        }
    };
    RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {

        @Override
        public String recover(RetryContext context) {
            StatefulRecoveryRetryTests.this.count++;
            StatefulRecoveryRetryTests.this.list.add(input);
            return input;
        }
    };
    Object result = null;
    try {
        result = this.retryTemplate.execute(callback, recoveryCallback, state);
        fail("Expected exception on first try");
    } catch (Exception e) {
    // expected...
    }
    // On the second retry, the recovery path is taken...
    result = this.retryTemplate.execute(callback, recoveryCallback, state);
    // default result is the item
    assertEquals(input, result);
    assertEquals(1, this.count);
    assertEquals(input, this.list.get(0));
}
Also used : RecoveryCallback(cn.taketoday.retry.RecoveryCallback) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Example 9 with RetryCallback

use of cn.taketoday.retry.RetryCallback in project today-framework by TAKETODAY.

the class MethodInvocationRetryListenerSupportTests method testCloseWithRetryCallbackShouldntCallDoCloseMethod.

@Test
public void testCloseWithRetryCallbackShouldntCallDoCloseMethod() {
    final AtomicInteger callsOnDoCloseMethod = new AtomicInteger(0);
    MethodInvocationRetryListenerSupport support = new MethodInvocationRetryListenerSupport() {

        @Override
        protected <T, E extends Throwable> void doClose(RetryContext context, MethodInvocationRetryCallback<T, E> callback, Throwable throwable) {
            callsOnDoCloseMethod.incrementAndGet();
        }
    };
    RetryContext context = mock(RetryContext.class);
    RetryCallback callback = mock(RetryCallback.class);
    support.close(context, callback, null);
    assertEquals(0, callsOnDoCloseMethod.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryContext(cn.taketoday.retry.RetryContext) MethodInvocationRetryCallback(cn.taketoday.retry.interceptor.MethodInvocationRetryCallback) RetryCallback(cn.taketoday.retry.RetryCallback) MethodInvocationRetryCallback(cn.taketoday.retry.interceptor.MethodInvocationRetryCallback) Test(org.junit.Test)

Example 10 with RetryCallback

use of cn.taketoday.retry.RetryCallback in project today-framework by TAKETODAY.

the class StatefulRecoveryRetryTests method testExhaustedClearsHistoryAfterLastAttempt.

@Test
public void testExhaustedClearsHistoryAfterLastAttempt() throws Throwable {
    RetryPolicy retryPolicy = new SimpleRetryPolicy(1);
    this.retryTemplate.setRetryPolicy(retryPolicy);
    final String input = "foo";
    RetryState state = new DefaultRetryState(input);
    RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {

        @Override
        public String doWithRetry(RetryContext context) throws Exception {
            throw new RuntimeException("Barf!");
        }
    };
    try {
        this.retryTemplate.execute(callback, state);
        fail("Expected ExhaustedRetryException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    try {
        this.retryTemplate.execute(callback, state);
        fail("Expected ExhaustedRetryException");
    } catch (ExhaustedRetryException e) {
    // expected
    }
    RetryContext context = this.retryTemplate.open(retryPolicy, state);
    // True after exhausted - the history is reset...
    assertTrue(retryPolicy.canRetry(context));
}
Also used : ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) RetryPolicy(cn.taketoday.retry.RetryPolicy) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Aggregations

RetryCallback (cn.taketoday.retry.RetryCallback)16 RetryContext (cn.taketoday.retry.RetryContext)16 Test (org.junit.Test)16 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)14 DataAccessException (cn.taketoday.dao.DataAccessException)12 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)12 RetryException (cn.taketoday.retry.RetryException)12 RetryState (cn.taketoday.retry.RetryState)12 RecoveryCallback (cn.taketoday.retry.RecoveryCallback)8 RetryPolicy (cn.taketoday.retry.RetryPolicy)6 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)6 BinaryExceptionClassifier (cn.taketoday.classify.BinaryExceptionClassifier)4 MapRetryContextCache (cn.taketoday.retry.policy.MapRetryContextCache)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 MethodInvocationRetryCallback (cn.taketoday.retry.interceptor.MethodInvocationRetryCallback)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2