Search in sources :

Example 11 with RetryStatistics

use of cn.taketoday.retry.RetryStatistics in project today-infrastructure 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)

Example 12 with RetryStatistics

use of cn.taketoday.retry.RetryStatistics in project today-infrastructure 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 13 with RetryStatistics

use of cn.taketoday.retry.RetryStatistics in project today-infrastructure 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 14 with RetryStatistics

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

the class CircuitBreakerInterceptorStatisticsTests method testCircuitOpenWhenNotRetryable.

@Test
public void testCircuitOpenWhenNotRetryable() throws Throwable {
    Object result = callback.service("one");
    RetryStatistics stats = repository.findOne("test");
    // System.err.println(stats);
    assertEquals(1, stats.getStartedCount());
    assertEquals(RECOVERED, result);
    result = callback.service("two");
    assertEquals(RECOVERED, result);
    assertEquals("There should be two recoveries", 2, stats.getRecoveryCount());
    assertEquals("There should only be one error because the circuit is now open", 1, stats.getErrorCount());
}
Also used : RetryStatistics(cn.taketoday.retry.RetryStatistics) Test(org.junit.jupiter.api.Test)

Aggregations

RetryStatistics (cn.taketoday.retry.RetryStatistics)14 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)12 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)12 Test (org.junit.Test)12 RetryState (cn.taketoday.retry.RetryState)6 DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)6 RetryContext (cn.taketoday.retry.RetryContext)4 Test (org.junit.jupiter.api.Test)2