Search in sources :

Example 1 with SimpleRetryPolicy

use of cn.taketoday.retry.policy.SimpleRetryPolicy 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 2 with SimpleRetryPolicy

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

the class StatefulRetryOperationsInterceptorTests method testInterceptorChainWithRetry.

@Test
public void testInterceptorChainWithRetry() throws Exception {
    ((Advised) service).addAdvice(interceptor);
    final List<String> list = new ArrayList<>();
    ((Advised) service).addAdvice((MethodInterceptor) invocation -> {
        list.add("chain");
        return invocation.proceed();
    });
    interceptor.setRetryOperations(retryTemplate);
    retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2));
    try {
        service.service("foo");
        fail("Expected Exception.");
    } catch (Exception e) {
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
    }
    assertEquals(1, count);
    service.service("foo");
    assertEquals(2, count);
    assertEquals(2, list.size());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) AlwaysRetryPolicy(cn.taketoday.retry.policy.AlwaysRetryPolicy) RetryListener(cn.taketoday.retry.RetryListener) Advised(cn.taketoday.aop.framework.Advised) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ArgumentCaptor(org.mockito.ArgumentCaptor) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) RetryContext(cn.taketoday.retry.RetryContext) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Assert.fail(org.junit.Assert.fail) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) RetryCallback(cn.taketoday.retry.RetryCallback) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Collection(java.util.Collection) RetryOperations(cn.taketoday.retry.RetryOperations) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.when(org.mockito.Mockito.when) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Advised(cn.taketoday.aop.framework.Advised) ArrayList(java.util.ArrayList) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.jupiter.api.Test)

Example 3 with SimpleRetryPolicy

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

the class StatisticsListenerTests method testStatelessRecovery.

@Test
public void testStatelessRecovery() throws Throwable {
    RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setListeners(new RetryListener[] { listener });
    for (int x = 1; x <= 10; x++) {
        MockRetryCallback callback = new MockRetryCallback();
        callback.setAttemptsBeforeSuccess(x + 1);
        retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
        retryTemplate.execute(callback, new RecoveryCallback<Object>() {

            @Override
            public Object recover(RetryContext context) throws Exception {
                return null;
            }
        });
        assertEquals(x, callback.attempts);
        RetryStatistics stats = repository.findOne("test");
        // System.err.println(stats);
        assertNotNull(stats);
        assertEquals(x, stats.getRecoveryCount());
        assertEquals((x + 1) * x / 2, stats.getStartedCount());
        assertEquals(stats.getStartedCount(), stats.getErrorCount());
    }
}
Also used : RetryStatistics(cn.taketoday.retry.RetryStatistics) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Test(org.junit.Test)

Example 4 with SimpleRetryPolicy

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

the class StatisticsListenerTests method testStatelessSuccessful.

@Test
public void testStatelessSuccessful() throws Throwable {
    RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setListeners(new RetryListener[] { listener });
    for (int x = 1; x <= 10; x++) {
        MockRetryCallback callback = new MockRetryCallback();
        callback.setAttemptsBeforeSuccess(x);
        retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
        retryTemplate.execute(callback);
        assertEquals(x, callback.attempts);
        RetryStatistics stats = repository.findOne("test");
        // System.err.println(stats);
        assertNotNull(stats);
        assertEquals(x, stats.getCompleteCount());
        assertEquals((x + 1) * x / 2, stats.getStartedCount());
        assertEquals(stats.getStartedCount(), stats.getErrorCount() + x);
    }
}
Also used : RetryStatistics(cn.taketoday.retry.RetryStatistics) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Test(org.junit.Test)

Example 5 with SimpleRetryPolicy

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

the class StatisticsListenerTests method testStatefulSuccessful.

@Test
public void testStatefulSuccessful() throws Throwable {
    RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setListeners(new RetryListener[] { listener });
    RetryState state = new DefaultRetryState("foo");
    for (int x = 1; x <= 10; x++) {
        MockRetryCallback callback = new MockRetryCallback();
        callback.setAttemptsBeforeSuccess(x);
        retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
        for (int i = 0; i < x; i++) {
            try {
                retryTemplate.execute(callback, state);
            } catch (Exception e) {
            // don't care
            }
        }
        assertEquals(x, callback.attempts);
        RetryStatistics stats = repository.findOne("test");
        // System.err.println(stats);
        assertNotNull(stats);
        assertEquals(x, stats.getCompleteCount());
        assertEquals((x + 1) * x / 2, stats.getStartedCount());
        assertEquals(stats.getStartedCount(), stats.getErrorCount() + x);
    }
}
Also used : RetryStatistics(cn.taketoday.retry.RetryStatistics) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) RetryState(cn.taketoday.retry.RetryState) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) Test(org.junit.Test)

Aggregations

SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)64 Test (org.junit.Test)56 RetryContext (cn.taketoday.retry.RetryContext)26 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)22 RetryState (cn.taketoday.retry.RetryState)18 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)16 RetryCallback (cn.taketoday.retry.RetryCallback)16 DataAccessException (cn.taketoday.dao.DataAccessException)12 RetryException (cn.taketoday.retry.RetryException)12 RetryStatistics (cn.taketoday.retry.RetryStatistics)12 Advised (cn.taketoday.aop.framework.Advised)10 RecoveryCallback (cn.taketoday.retry.RecoveryCallback)10 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)8 DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)8 BinaryExceptionClassifier (cn.taketoday.classify.BinaryExceptionClassifier)6 ArrayList (java.util.ArrayList)6 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)6 RetryPolicy (cn.taketoday.retry.RetryPolicy)5 TerminatedRetryException (cn.taketoday.retry.TerminatedRetryException)5 BackOffInterruptedException (cn.taketoday.retry.backoff.BackOffInterruptedException)5