Search in sources :

Example 1 with RetryState

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

use of cn.taketoday.retry.RetryState 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 3 with RetryState

use of cn.taketoday.retry.RetryState 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 4 with RetryState

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

the class StatefulRecoveryRetryTests method testCacheCapacityNotReachedIfRecovered.

@Test
public void testCacheCapacityNotReachedIfRecovered() throws Throwable {
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(1);
    this.retryTemplate.setRetryPolicy(retryPolicy);
    this.retryTemplate.setRetryContextCache(new MapRetryContextCache(2));
    final StringHolder item = new StringHolder("foo");
    RetryState state = new DefaultRetryState(item);
    RetryCallback<Object, Exception> callback = new RetryCallback<Object, Exception>() {

        @Override
        public Object doWithRetry(RetryContext context) throws Exception {
            StatefulRecoveryRetryTests.this.count++;
            throw new RuntimeException("Barf!");
        }
    };
    RecoveryCallback<Object> recoveryCallback = new RecoveryCallback<Object>() {

        @Override
        public Object recover(RetryContext context) throws Exception {
            return null;
        }
    };
    try {
        this.retryTemplate.execute(callback, recoveryCallback, state);
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    this.retryTemplate.execute(callback, recoveryCallback, state);
    RetryContext context = this.retryTemplate.open(retryPolicy, state);
    // True after exhausted - the history is reset...
    assertEquals(0, context.getRetryCount());
}
Also used : RecoveryCallback(cn.taketoday.retry.RecoveryCallback) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Example 5 with RetryState

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

the class StatefulRecoveryRetryTests method testSwitchToStatelessForNoRollback.

@Test
public void testSwitchToStatelessForNoRollback() throws Throwable {
    this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    // Roll back for these:
    BinaryExceptionClassifier classifier = new BinaryExceptionClassifier(Collections.<Class<? extends Throwable>>singleton(DataAccessException.class));
    // ...but not these:
    assertFalse(classifier.classify(new RuntimeException()));
    final String input = "foo";
    RetryState state = new DefaultRetryState(input, classifier);
    RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {

        @Override
        public String doWithRetry(RetryContext context) throws Exception {
            throw new RuntimeException("Barf!");
        }
    };
    RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {

        @Override
        public String recover(RetryContext context) {
            StatefulRecoveryRetryTests.this.count++;
            StatefulRecoveryRetryTests.this.list.add(input);
            return input;
        }
    };
    Object result = null;
    // On the second retry, the recovery path is taken...
    result = this.retryTemplate.execute(callback, recoveryCallback, state);
    // default result is the item
    assertEquals(input, result);
    assertEquals(1, this.count);
    assertEquals(input, this.list.get(0));
}
Also used : BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) DataAccessException(cn.taketoday.dao.DataAccessException) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Aggregations

RetryState (cn.taketoday.retry.RetryState)30 Test (org.junit.Test)30 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)18 RetryContext (cn.taketoday.retry.RetryContext)18 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)16 DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)16 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)16 DataAccessException (cn.taketoday.dao.DataAccessException)14 RetryException (cn.taketoday.retry.RetryException)14 RetryCallback (cn.taketoday.retry.RetryCallback)10 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)8 RecoveryCallback (cn.taketoday.retry.RecoveryCallback)6 RetryStatistics (cn.taketoday.retry.RetryStatistics)6 RetryPolicy (cn.taketoday.retry.RetryPolicy)4 BinaryExceptionClassifier (cn.taketoday.classify.BinaryExceptionClassifier)2 ExponentialBackOffPolicy (cn.taketoday.retry.backoff.ExponentialBackOffPolicy)2 MapRetryContextCache (cn.taketoday.retry.policy.MapRetryContextCache)2 ArrayList (java.util.ArrayList)2