Search in sources :

Example 16 with RetryTemplate

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

the class StatefulRetryIntegrationTests method testExternalRetryWithFailAndNoRetry.

@Test
public void testExternalRetryWithFailAndNoRetry() throws Throwable {
    MockRetryCallback callback = new MockRetryCallback();
    RetryState retryState = new DefaultRetryState("foo");
    RetryTemplate retryTemplate = new RetryTemplate();
    MapRetryContextCache cache = new MapRetryContextCache();
    retryTemplate.setRetryContextCache(cache);
    retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    assertFalse(cache.containsKey("foo"));
    try {
        retryTemplate.execute(callback, retryState);
        // The first failed attempt we expect to retry...
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertEquals(null, e.getMessage());
    }
    assertTrue(cache.containsKey("foo"));
    try {
        retryTemplate.execute(callback, retryState);
        // We don't get a second attempt...
        fail("Expected ExhaustedRetryException");
    } catch (ExhaustedRetryException e) {
        // This is now the "exhausted" message:
        assertNotNull(e.getMessage());
    }
    assertFalse(cache.containsKey("foo"));
    // Callback is called once: the recovery path should be called in
    // handleRetryExhausted (so not in this test)...
    assertEquals(1, callback.attempts);
}
Also used : ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) 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 17 with RetryTemplate

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

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

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

the class StatisticsListenerTests method testStatefulUnsuccessful.

@Test
public void testStatefulUnsuccessful() 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 + 1);
        retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
        for (int i = 0; i < x + 1; 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.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) 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 20 with RetryTemplate

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

the class StatisticsListenerTests method testStatefulRecovery.

@Test
public void testStatefulRecovery() 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 + 1);
        retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
        for (int i = 0; i < x + 1; i++) {
            try {
                retryTemplate.execute(callback, new RecoveryCallback<Object>() {

                    @Override
                    public Object recover(RetryContext context) throws Exception {
                        return null;
                    }
                }, 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.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) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) RetryState(cn.taketoday.retry.RetryState) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) 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