Search in sources :

Example 16 with RetryContext

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

the class SimpleRetryPolicyTests method testRetryCount.

@Test
public void testRetryCount() throws Exception {
    SimpleRetryPolicy policy = new SimpleRetryPolicy();
    RetryContext context = policy.open(null);
    assertNotNull(context);
    policy.registerThrowable(context, null);
    assertEquals(0, context.getRetryCount());
    policy.registerThrowable(context, new RuntimeException("foo"));
    assertEquals(1, context.getRetryCount());
    assertEquals("foo", context.getLastThrowable().getMessage());
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) Test(org.junit.Test)

Example 17 with RetryContext

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

the class TimeoutRetryPolicyTests method testTimeoutPreventsRetry.

@Test
public void testTimeoutPreventsRetry() throws Exception {
    TimeoutRetryPolicy policy = new TimeoutRetryPolicy();
    policy.setTimeout(100);
    RetryContext context = policy.open(null);
    policy.registerThrowable(context, new Exception());
    assertTrue(policy.canRetry(context));
    Thread.sleep(200);
    assertFalse(policy.canRetry(context));
    policy.close(context);
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) Test(org.junit.Test)

Example 18 with RetryContext

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

the class TimeoutRetryPolicyTests method testRetryCount.

@Test
public void testRetryCount() throws Exception {
    TimeoutRetryPolicy policy = new TimeoutRetryPolicy();
    RetryContext context = policy.open(null);
    assertNotNull(context);
    policy.registerThrowable(context, null);
    assertEquals(0, context.getRetryCount());
    policy.registerThrowable(context, new RuntimeException("foo"));
    assertEquals(1, context.getRetryCount());
    assertEquals("foo", context.getLastThrowable().getMessage());
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) Test(org.junit.Test)

Example 19 with RetryContext

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

the class CircuitBreakerStatisticsTests method init.

@Before
public void init() {
    this.callback = new MockRetryCallback();
    this.recovery = new RecoveryCallback<Object>() {

        @Override
        public Object recover(RetryContext context) throws Exception {
            return RECOVERED;
        }
    };
    this.retryTemplate = new RetryTemplate();
    this.cache = new MapRetryContextCache();
    this.retryTemplate.setRetryContextCache(this.cache);
    retryTemplate.setListeners(new RetryListener[] { listener });
    this.callback.setAttemptsBeforeSuccess(1);
    // No rollback by default (so exceptions are not rethrown)
    this.state = new DefaultRetryState("retry", new BinaryExceptionClassifier(false));
}
Also used : MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) RetryContext(cn.taketoday.retry.RetryContext) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Before(org.junit.Before)

Example 20 with RetryContext

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

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