Search in sources :

Example 1 with DefaultRetryState

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

the class StatefulRetryOperationsInterceptorTests method testKeyGeneratorReturningNull.

@SuppressWarnings("unchecked")
@Test
public void testKeyGeneratorReturningNull() throws Throwable {
    this.interceptor.setKeyGenerator(mock(MethodArgumentsKeyGenerator.class));
    this.interceptor.setLabel("foo");
    RetryOperations template = mock(RetryOperations.class);
    this.interceptor.setRetryOperations(template);
    MethodInvocation invocation = mock(MethodInvocation.class);
    when(invocation.getArguments()).thenReturn(new Object[] { new Object() });
    this.interceptor.invoke(invocation);
    ArgumentCaptor<DefaultRetryState> captor = ArgumentCaptor.forClass(DefaultRetryState.class);
    verify(template).execute(any(RetryCallback.class), any(RecoveryCallback.class), captor.capture());
    assertNull(captor.getValue().getKey());
}
Also used : RecoveryCallback(cn.taketoday.retry.RecoveryCallback) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) RetryOperations(cn.taketoday.retry.RetryOperations) MethodInvocation(org.aopalliance.intercept.MethodInvocation) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultRetryState

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

the class StatefulRetryOperationsInterceptorTests method testKeyGeneratorAndRawKey.

@SuppressWarnings("unchecked")
@Test
public void testKeyGeneratorAndRawKey() throws Throwable {
    this.interceptor.setKeyGenerator(item -> "bar");
    this.interceptor.setLabel("foo");
    this.interceptor.setUseRawKey(true);
    RetryOperations template = mock(RetryOperations.class);
    this.interceptor.setRetryOperations(template);
    MethodInvocation invocation = mock(MethodInvocation.class);
    when(invocation.getArguments()).thenReturn(new Object[] { new Object() });
    this.interceptor.invoke(invocation);
    ArgumentCaptor<DefaultRetryState> captor = ArgumentCaptor.forClass(DefaultRetryState.class);
    verify(template).execute(any(RetryCallback.class), any(RecoveryCallback.class), captor.capture());
    assertEquals("bar", captor.getValue().getKey());
}
Also used : RecoveryCallback(cn.taketoday.retry.RecoveryCallback) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) RetryOperations(cn.taketoday.retry.RetryOperations) MethodInvocation(org.aopalliance.intercept.MethodInvocation) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultRetryState

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

use of cn.taketoday.retry.support.DefaultRetryState 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 5 with DefaultRetryState

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

Aggregations

DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)26 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)22 Test (org.junit.Test)18 RetryState (cn.taketoday.retry.RetryState)16 RetryContext (cn.taketoday.retry.RetryContext)10 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)6 RecoveryCallback (cn.taketoday.retry.RecoveryCallback)6 RetryStatistics (cn.taketoday.retry.RetryStatistics)6 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)6 BinaryExceptionClassifier (cn.taketoday.classify.BinaryExceptionClassifier)4 RetryCallback (cn.taketoday.retry.RetryCallback)4 RetryOperations (cn.taketoday.retry.RetryOperations)4 MethodInvocation (org.aopalliance.intercept.MethodInvocation)4 Before (org.junit.Before)4 Test (org.junit.jupiter.api.Test)4 ExponentialBackOffPolicy (cn.taketoday.retry.backoff.ExponentialBackOffPolicy)2 CircuitBreakerRetryContext (cn.taketoday.retry.policy.CircuitBreakerRetryPolicy.CircuitBreakerRetryContext)2 MapRetryContextCache (cn.taketoday.retry.policy.MapRetryContextCache)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2