Search in sources :

Example 21 with RetryTemplate

use of cn.taketoday.retry.support.RetryTemplate in project today-framework 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)

Example 22 with RetryTemplate

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

the class StatisticsListenerTests method testStatelessUnsuccessful.

@Test
public void testStatelessUnsuccessful() 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));
        try {
            retryTemplate.execute(callback);
        } catch (Exception e) {
        // not interested
        }
        assertEquals(x, callback.attempts);
        RetryStatistics stats = repository.findOne("test");
        assertNotNull(stats);
        assertEquals(x, stats.getAbortCount());
        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) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Test(org.junit.Test)

Example 23 with RetryTemplate

use of cn.taketoday.retry.support.RetryTemplate in project today-framework 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 24 with RetryTemplate

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

the class StatefulRetryOperationsInterceptorTests method testTransformerRecoveryAfterTooManyAttempts.

@Test
public void testTransformerRecoveryAfterTooManyAttempts() throws Exception {
    ((Advised) transformer).addAdvice(interceptor);
    interceptor.setRetryOperations(retryTemplate);
    retryTemplate.setRetryPolicy(new NeverRetryPolicy());
    try {
        transformer.transform("foo");
        fail("Expected Exception.");
    } catch (Exception e) {
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
    }
    assertEquals(1, count);
    interceptor.setRecoverer((MethodInvocationRecoverer<Collection<String>>) (data, cause) -> {
        count++;
        return Collections.singleton((String) data[0]);
    });
    Collection<String> result = transformer.transform("foo");
    assertEquals(2, count);
    assertEquals(1, result.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) Collection(java.util.Collection) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.jupiter.api.Test)

Example 25 with RetryTemplate

use of cn.taketoday.retry.support.RetryTemplate in project today-framework 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)

Aggregations

RetryTemplate (cn.taketoday.retry.support.RetryTemplate)52 Test (org.junit.Test)38 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)26 DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)26 RetryContext (cn.taketoday.retry.RetryContext)22 RetryState (cn.taketoday.retry.RetryState)16 Advised (cn.taketoday.aop.framework.Advised)12 RetryStatistics (cn.taketoday.retry.RetryStatistics)12 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)10 RecoveryCallback (cn.taketoday.retry.RecoveryCallback)8 ArrayList (java.util.ArrayList)8 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)8 SingletonTargetSource (cn.taketoday.aop.target.SingletonTargetSource)6 RetryListener (cn.taketoday.retry.RetryListener)6 RetryOperations (cn.taketoday.retry.RetryOperations)6 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)6 HashMap (java.util.HashMap)6 MethodInvocation (org.aopalliance.intercept.MethodInvocation)6 Before (org.junit.Before)5 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)4