Search in sources :

Example 1 with NeverRetryPolicy

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

the class RetryOperationsInterceptorTests method testRetryExceptionAfterTooManyAttempts.

@Test
public void testRetryExceptionAfterTooManyAttempts() throws Exception {
    ((Advised) this.service).addAdvice(this.interceptor);
    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(new NeverRetryPolicy());
    this.interceptor.setRetryOperations(template);
    try {
        this.service.service();
        fail("Expected Exception.");
    } catch (Exception e) {
        assertTrue(e.getMessage().startsWith("Not enough calls"));
    }
    assertEquals(1, count);
}
Also used : RetryTemplate(cn.taketoday.retry.support.RetryTemplate) Advised(cn.taketoday.aop.framework.Advised) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Test(org.junit.Test)

Example 2 with NeverRetryPolicy

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

the class StatefulRetryOperationsInterceptorTests method testRetryExceptionAfterTooManyAttemptsWithNoRecovery.

@Test
public void testRetryExceptionAfterTooManyAttemptsWithNoRecovery() throws Exception {
    ((Advised) service).addAdvice(interceptor);
    interceptor.setRetryOperations(retryTemplate);
    retryTemplate.setRetryPolicy(new NeverRetryPolicy());
    try {
        service.service("foo");
        fail("Expected Exception.");
    } catch (Exception e) {
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
    }
    assertEquals(1, count);
    try {
        service.service("foo");
        fail("Expected ExhaustedRetryException");
    } catch (ExhaustedRetryException e) {
        // expected
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.startsWith("Retry exhausted"));
    }
    assertEquals(1, count);
}
Also used : ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Advised(cn.taketoday.aop.framework.Advised) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.jupiter.api.Test)

Example 3 with NeverRetryPolicy

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

the class StatefulRetryOperationsInterceptorTests method testTransformerRecoveryAfterTooManyAttempts.

@Test
public void testTransformerRecoveryAfterTooManyAttempts() throws Exception {
    ((Advised) transformer).addAdvice(interceptor);
    interceptor.setRetryOperations(retryTemplate);
    retryTemplate.setRetryPolicy(new NeverRetryPolicy());
    try {
        transformer.transform("foo");
        fail("Expected Exception.");
    } catch (Exception e) {
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
    }
    assertEquals(1, count);
    interceptor.setRecoverer((MethodInvocationRecoverer<Collection<String>>) (data, cause) -> {
        count++;
        return Collections.singleton((String) data[0]);
    });
    Collection<String> result = transformer.transform("foo");
    assertEquals(2, count);
    assertEquals(1, result.size());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) AlwaysRetryPolicy(cn.taketoday.retry.policy.AlwaysRetryPolicy) RetryListener(cn.taketoday.retry.RetryListener) Advised(cn.taketoday.aop.framework.Advised) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ArgumentCaptor(org.mockito.ArgumentCaptor) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) RetryContext(cn.taketoday.retry.RetryContext) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Assert.fail(org.junit.Assert.fail) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) RetryCallback(cn.taketoday.retry.RetryCallback) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Collection(java.util.Collection) RetryOperations(cn.taketoday.retry.RetryOperations) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.when(org.mockito.Mockito.when) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Advised(cn.taketoday.aop.framework.Advised) Collection(java.util.Collection) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.jupiter.api.Test)

Example 4 with NeverRetryPolicy

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

the class RetryTemplateTests method testRethrowError.

@Test
public void testRethrowError() throws Throwable {
    RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setRetryPolicy(new NeverRetryPolicy());
    try {
        retryTemplate.execute(new RetryCallback<Object, Exception>() {

            @Override
            public Object doWithRetry(RetryContext context) throws Exception {
                throw new Error("Realllly bad!");
            }
        });
        fail("Expected Error");
    } catch (Error e) {
        assertEquals("Realllly bad!", e.getMessage());
    }
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) TerminatedRetryException(cn.taketoday.retry.TerminatedRetryException) BackOffInterruptedException(cn.taketoday.retry.backoff.BackOffInterruptedException) Test(org.junit.Test)

Example 5 with NeverRetryPolicy

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

the class RetryTemplateTests method testFailedPolicy.

@SuppressWarnings("serial")
@Test
public void testFailedPolicy() throws Throwable {
    RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setRetryPolicy(new NeverRetryPolicy() {

        @Override
        public void registerThrowable(RetryContext context, Throwable throwable) {
            throw new RuntimeException("Planned");
        }
    });
    try {
        retryTemplate.execute(new RetryCallback<Object, Exception>() {

            @Override
            public Object doWithRetry(RetryContext context) throws Exception {
                throw new RuntimeException("Realllly bad!");
            }
        });
        fail("Expected Error");
    } catch (TerminatedRetryException e) {
        assertEquals("Planned", e.getCause().getMessage());
    }
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) TerminatedRetryException(cn.taketoday.retry.TerminatedRetryException) BackOffInterruptedException(cn.taketoday.retry.backoff.BackOffInterruptedException) TerminatedRetryException(cn.taketoday.retry.TerminatedRetryException) Test(org.junit.Test)

Aggregations

NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)28 Test (org.junit.Test)22 RetryContext (cn.taketoday.retry.RetryContext)16 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)14 Advised (cn.taketoday.aop.framework.Advised)8 TerminatedRetryException (cn.taketoday.retry.TerminatedRetryException)6 CircuitBreakerRetryPolicy (cn.taketoday.retry.policy.CircuitBreakerRetryPolicy)6 Test (org.junit.jupiter.api.Test)6 DataAccessException (cn.taketoday.dao.DataAccessException)4 RetryException (cn.taketoday.retry.RetryException)4 RetryListener (cn.taketoday.retry.RetryListener)4 RetryState (cn.taketoday.retry.RetryState)4 BackOffInterruptedException (cn.taketoday.retry.backoff.BackOffInterruptedException)4 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)4 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)2 SingletonTargetSource (cn.taketoday.aop.target.SingletonTargetSource)2 RecoveryCallback (cn.taketoday.retry.RecoveryCallback)2 RetryCallback (cn.taketoday.retry.RetryCallback)2 RetryOperations (cn.taketoday.retry.RetryOperations)2 AlwaysRetryPolicy (cn.taketoday.retry.policy.AlwaysRetryPolicy)2