Search in sources :

Example 11 with RetryTemplate

use of cn.taketoday.retry.support.RetryTemplate 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 12 with RetryTemplate

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

Example 13 with RetryTemplate

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

the class StatefulRetryIntegrationTests method testExponentialBackOffIsExponential.

@Test
public void testExponentialBackOffIsExponential() throws Throwable {
    ExponentialBackOffPolicy policy = new ExponentialBackOffPolicy();
    policy.setInitialInterval(100);
    policy.setMultiplier(1.5);
    RetryTemplate template = new RetryTemplate();
    template.setBackOffPolicy(policy);
    final List<Long> times = new ArrayList<Long>();
    RetryState retryState = new DefaultRetryState("bar");
    for (int i = 0; i < 3; i++) {
        try {
            template.execute(new RetryCallback<String, Exception>() {

                public String doWithRetry(RetryContext context) throws Exception {
                    times.add(System.currentTimeMillis());
                    throw new Exception("Fail");
                }
            }, new RecoveryCallback<String>() {

                public String recover(RetryContext context) throws Exception {
                    return null;
                }
            }, retryState);
        } catch (Exception e) {
            assertTrue(e.getMessage().equals("Fail"));
        }
    }
    assertEquals(3, times.size());
    assertTrue(times.get(1) - times.get(0) >= 100);
    assertTrue(times.get(2) - times.get(1) >= 150);
}
Also used : ExponentialBackOffPolicy(cn.taketoday.retry.backoff.ExponentialBackOffPolicy) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) RetryContext(cn.taketoday.retry.RetryContext) ArrayList(java.util.ArrayList) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) RetryState(cn.taketoday.retry.RetryState) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) Test(org.junit.Test)

Example 14 with RetryTemplate

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

the class StatefulRetryIntegrationTests method testExternalRetryWithSuccessOnRetryAndSerializedContext.

@Test
public void testExternalRetryWithSuccessOnRetryAndSerializedContext() throws Throwable {
    MockRetryCallback callback = new MockRetryCallback();
    RetryState retryState = new DefaultRetryState("foo");
    RetryTemplate retryTemplate = new RetryTemplate();
    RetryContextCache cache = new SerializedMapRetryContextCache();
    retryTemplate.setRetryContextCache(cache);
    retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2));
    assertFalse(cache.containsKey("foo"));
    Object result = "start_foo";
    try {
        result = retryTemplate.execute(callback, retryState);
        // The first failed attempt we expect to retry...
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertNull(e.getMessage());
    }
    assertTrue(cache.containsKey("foo"));
    result = retryTemplate.execute(callback, retryState);
    assertFalse(cache.containsKey("foo"));
    assertEquals(2, callback.attempts);
    assertEquals(1, callback.context.getRetryCount());
    assertEquals("bar", result);
}
Also used : RetryTemplate(cn.taketoday.retry.support.RetryTemplate) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) RetryState(cn.taketoday.retry.RetryState) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) Test(org.junit.Test)

Example 15 with RetryTemplate

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

the class FatalExceptionRetryPolicyTests method testFatalExceptionWithState.

@Test
public void testFatalExceptionWithState() throws Throwable {
    MockRetryCallback callback = new MockRetryCallback();
    callback.setExceptionToThrow(new IllegalArgumentException());
    RetryTemplate retryTemplate = new RetryTemplate();
    Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>();
    map.put(IllegalArgumentException.class, false);
    map.put(IllegalStateException.class, false);
    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 {
        retryTemplate.execute(callback, recoveryCallback, new DefaultRetryState("foo"));
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // If stateful we have to always rethrow. Clients who want special
    // cases have to implement them in the callback
    }
    result = retryTemplate.execute(callback, recoveryCallback, new DefaultRetryState("foo"));
    // 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) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) RetryContext(cn.taketoday.retry.RetryContext) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) Test(org.junit.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