Search in sources :

Example 1 with RetryStatistics

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

Example 2 with RetryStatistics

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

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

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

Example 5 with RetryStatistics

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

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