Search in sources :

Example 11 with RetryContext

use of cn.taketoday.retry.RetryContext in project today-infrastructure by TAKETODAY.

the class CompositeRetryPolicyTests method testEmptyPolicies.

@Test
public void testEmptyPolicies() throws Exception {
    CompositeRetryPolicy policy = new CompositeRetryPolicy();
    RetryContext context = policy.open(null);
    assertNotNull(context);
    assertTrue(policy.canRetry(context));
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) Test(org.junit.Test)

Example 12 with RetryContext

use of cn.taketoday.retry.RetryContext in project today-infrastructure by TAKETODAY.

the class CompositeRetryPolicyTests method testOptimistic.

@SuppressWarnings("serial")
@Test
public void testOptimistic() throws Exception {
    CompositeRetryPolicy policy = new CompositeRetryPolicy();
    policy.setOptimistic(true);
    policy.setPolicies(new RetryPolicy[] { new MockRetryPolicySupport() {

        public boolean canRetry(RetryContext context) {
            return false;
        }
    }, new MockRetryPolicySupport() });
    RetryContext context = policy.open(null);
    assertNotNull(context);
    assertTrue(policy.canRetry(context));
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) Test(org.junit.Test)

Example 13 with RetryContext

use of cn.taketoday.retry.RetryContext in project today-infrastructure by TAKETODAY.

the class CompositeRetryPolicyTests method testNonTrivialPoliciesWithThrowable.

@SuppressWarnings("serial")
@Test
public void testNonTrivialPoliciesWithThrowable() throws Exception {
    CompositeRetryPolicy policy = new CompositeRetryPolicy();
    policy.setPolicies(new RetryPolicy[] { new MockRetryPolicySupport(), new MockRetryPolicySupport() {

        boolean errorRegistered = false;

        public boolean canRetry(RetryContext context) {
            return !errorRegistered;
        }

        public void registerThrowable(RetryContext context, Throwable throwable) {
            errorRegistered = true;
        }
    } });
    RetryContext context = policy.open(null);
    assertNotNull(context);
    assertTrue(policy.canRetry(context));
    policy.registerThrowable(context, null);
    assertFalse("Should be still able to retry", policy.canRetry(context));
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) Test(org.junit.Test)

Example 14 with RetryContext

use of cn.taketoday.retry.RetryContext in project today-infrastructure by TAKETODAY.

the class FatalExceptionRetryPolicyTests method testFatalExceptionWithoutState.

@Test
public void testFatalExceptionWithoutState() throws Throwable {
    MockRetryCallback callback = new MockRetryCallback();
    callback.setExceptionToThrow(new IllegalArgumentException());
    RetryTemplate retryTemplate = new RetryTemplate();
    // Make sure certain exceptions are fatal...
    Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>();
    map.put(IllegalArgumentException.class, false);
    map.put(IllegalStateException.class, false);
    // ... and allow multiple attempts
    SimpleRetryPolicy policy = new SimpleRetryPolicy(3, map);
    retryTemplate.setRetryPolicy(policy);
    RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {

        public String recover(RetryContext context) throws Exception {
            return "bar";
        }
    };
    Object result = null;
    try {
        result = retryTemplate.execute(callback, recoveryCallback);
    } catch (IllegalArgumentException e) {
        // We should swallow the exception when recovery is possible
        fail("Did not expect IllegalArgumentException");
    }
    // Callback is called once: the recovery path should also be called
    assertEquals(1, callback.attempts);
    assertEquals("bar", result);
}
Also used : RecoveryCallback(cn.taketoday.retry.RecoveryCallback) HashMap(java.util.HashMap) RetryContext(cn.taketoday.retry.RetryContext) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) Test(org.junit.Test)

Example 15 with RetryContext

use of cn.taketoday.retry.RetryContext in project today-infrastructure by TAKETODAY.

the class SimpleRetryPolicyTests method testEmptyExceptionsNeverRetry.

@Test
public void testEmptyExceptionsNeverRetry() throws Exception {
    // We can't retry any exceptions...
    SimpleRetryPolicy policy = new SimpleRetryPolicy(3, Collections.<Class<? extends Throwable>, Boolean>emptyMap());
    RetryContext context = policy.open(null);
    // ...so we can't retry this one...
    policy.registerThrowable(context, new IllegalStateException());
    assertFalse(policy.canRetry(context));
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) Test(org.junit.Test)

Aggregations

RetryContext (cn.taketoday.retry.RetryContext)160 Test (org.junit.Test)140 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)24 TerminatedRetryException (cn.taketoday.retry.TerminatedRetryException)24 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)24 RetryException (cn.taketoday.retry.RetryException)18 RetryState (cn.taketoday.retry.RetryState)18 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)18 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)18 DataAccessException (cn.taketoday.dao.DataAccessException)14 RetryCallback (cn.taketoday.retry.RetryCallback)14 RetryListener (cn.taketoday.retry.RetryListener)14 RetryPolicy (cn.taketoday.retry.RetryPolicy)12 BackOffInterruptedException (cn.taketoday.retry.backoff.BackOffInterruptedException)12 RecoveryCallback (cn.taketoday.retry.RecoveryCallback)10 DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)10 HashMap (java.util.HashMap)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 MethodInvocationRetryCallback (cn.taketoday.retry.interceptor.MethodInvocationRetryCallback)8 Before (org.junit.Before)8