Search in sources :

Example 1 with RetryContext

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

the class SimpleRetryPolicyTests method testRetryLimitInitialState.

@Test
public void testRetryLimitInitialState() throws Exception {
    SimpleRetryPolicy policy = new SimpleRetryPolicy();
    RetryContext context = policy.open(null);
    assertTrue(policy.canRetry(context));
    policy.setMaxAttempts(0);
    context = policy.open(null);
    assertFalse(policy.canRetry(context));
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) Test(org.junit.Test)

Example 2 with RetryContext

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

the class SimpleRetryPolicyTests method testRetryableWithCause.

@Test
public void testRetryableWithCause() throws Exception {
    Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>();
    map.put(RuntimeException.class, true);
    SimpleRetryPolicy policy = new SimpleRetryPolicy(3, map, true);
    RetryContext context = policy.open(null);
    assertNotNull(context);
    policy.registerThrowable(context, new Exception(new RuntimeException("foo")));
    assertTrue(policy.canRetry(context));
}
Also used : HashMap(java.util.HashMap) RetryContext(cn.taketoday.retry.RetryContext) Test(org.junit.Test)

Example 3 with RetryContext

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

the class RetryOperationsInterceptorTests method testDefaultInterceptorWithRetryListenerInspectingTheMethodInvocation.

@Test
public void testDefaultInterceptorWithRetryListenerInspectingTheMethodInvocation() throws Exception {
    final String label = "FOO";
    final String classTagName = "class";
    final String methodTagName = "method";
    final String labelTagName = "label";
    final Map<String, String> monitoringTags = new HashMap<String, String>();
    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(new SimpleRetryPolicy(2));
    template.registerListener(new MethodInvocationRetryListenerSupport() {

        @Override
        protected <T, E extends Throwable> void doClose(RetryContext context, MethodInvocationRetryCallback<T, E> callback, Throwable throwable) {
            monitoringTags.put(labelTagName, callback.getLabel());
            Method method = callback.getInvocation().getMethod();
            monitoringTags.put(classTagName, method.getDeclaringClass().getSimpleName());
            monitoringTags.put(methodTagName, method.getName());
        }
    });
    this.interceptor.setLabel(label);
    this.interceptor.setRetryOperations(template);
    ((Advised) this.service).addAdvice(this.interceptor);
    this.service.service();
    assertEquals(2, count);
    assertEquals(3, monitoringTags.entrySet().size());
    assertThat(monitoringTags.get(labelTagName), equalTo(label));
    assertThat(monitoringTags.get(classTagName), equalTo(Service.class.getSimpleName()));
    assertThat(monitoringTags.get(methodTagName), equalTo("service"));
}
Also used : MethodInvocationRetryListenerSupport(cn.taketoday.retry.listener.MethodInvocationRetryListenerSupport) HashMap(java.util.HashMap) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Method(java.lang.reflect.Method) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.Test)

Example 4 with RetryContext

use of cn.taketoday.retry.RetryContext in project today-infrastructure 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 5 with RetryContext

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

the class MethodInvocationRetryListenerSupportTests method testOpenWithMethodInvocationRetryCallbackShouldCallDoCloseMethod.

@Test
public void testOpenWithMethodInvocationRetryCallbackShouldCallDoCloseMethod() {
    final AtomicInteger callsOnDoOpenMethod = new AtomicInteger(0);
    MethodInvocationRetryListenerSupport support = new MethodInvocationRetryListenerSupport() {

        @Override
        protected <T, E extends Throwable> boolean doOpen(RetryContext context, MethodInvocationRetryCallback<T, E> callback) {
            callsOnDoOpenMethod.incrementAndGet();
            return true;
        }
    };
    RetryContext context = mock(RetryContext.class);
    MethodInvocationRetryCallback callback = mock(MethodInvocationRetryCallback.class);
    assertTrue(support.open(context, callback));
    assertEquals(1, callsOnDoOpenMethod.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryContext(cn.taketoday.retry.RetryContext) MethodInvocationRetryCallback(cn.taketoday.retry.interceptor.MethodInvocationRetryCallback) 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