Search in sources :

Example 6 with ExhaustedRetryException

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

use of cn.taketoday.retry.ExhaustedRetryException in project today-framework by TAKETODAY.

the class StatefulRecoveryRetryTests method testExhaustedClearsHistoryAfterLastAttempt.

@Test
public void testExhaustedClearsHistoryAfterLastAttempt() throws Throwable {
    RetryPolicy retryPolicy = new SimpleRetryPolicy(1);
    this.retryTemplate.setRetryPolicy(retryPolicy);
    final String input = "foo";
    RetryState state = new DefaultRetryState(input);
    RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {

        @Override
        public String doWithRetry(RetryContext context) throws Exception {
            throw new RuntimeException("Barf!");
        }
    };
    try {
        this.retryTemplate.execute(callback, state);
        fail("Expected ExhaustedRetryException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    try {
        this.retryTemplate.execute(callback, state);
        fail("Expected ExhaustedRetryException");
    } catch (ExhaustedRetryException e) {
    // expected
    }
    RetryContext context = this.retryTemplate.open(retryPolicy, state);
    // True after exhausted - the history is reset...
    assertTrue(retryPolicy.canRetry(context));
}
Also used : ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) RetryPolicy(cn.taketoday.retry.RetryPolicy) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Example 8 with ExhaustedRetryException

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

the class CircuitBreakerStatisticsTests method testFailedRecoveryCountsAsAbort.

@Test
public void testFailedRecoveryCountsAsAbort() throws Throwable {
    this.retryTemplate.setRetryPolicy(new CircuitBreakerRetryPolicy(new NeverRetryPolicy()));
    this.recovery = new RecoveryCallback<Object>() {

        @Override
        public Object recover(RetryContext context) throws Exception {
            throw new ExhaustedRetryException("Planned exhausted");
        }
    };
    try {
        this.retryTemplate.execute(this.callback, this.recovery, this.state);
        fail("Expected ExhaustedRetryException");
    } catch (ExhaustedRetryException e) {
    // Fine
    }
    MutableRetryStatistics stats = (MutableRetryStatistics) repository.findOne("test");
    assertEquals(1, stats.getStartedCount());
    assertEquals(1, stats.getAbortCount());
    assertEquals(0, stats.getRecoveryCount());
}
Also used : ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryContext(cn.taketoday.retry.RetryContext) CircuitBreakerRetryPolicy(cn.taketoday.retry.policy.CircuitBreakerRetryPolicy) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.Test)

Example 9 with ExhaustedRetryException

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

the class StatefulRetryIntegrationTests method testExternalRetryWithFailAndNoRetry.

@Test
public void testExternalRetryWithFailAndNoRetry() throws Throwable {
    MockRetryCallback callback = new MockRetryCallback();
    RetryState retryState = new DefaultRetryState("foo");
    RetryTemplate retryTemplate = new RetryTemplate();
    MapRetryContextCache cache = new MapRetryContextCache();
    retryTemplate.setRetryContextCache(cache);
    retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    assertFalse(cache.containsKey("foo"));
    try {
        retryTemplate.execute(callback, retryState);
        // The first failed attempt we expect to retry...
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertEquals(null, e.getMessage());
    }
    assertTrue(cache.containsKey("foo"));
    try {
        retryTemplate.execute(callback, retryState);
        // We don't get a second attempt...
        fail("Expected ExhaustedRetryException");
    } catch (ExhaustedRetryException e) {
        // This is now the "exhausted" message:
        assertNotNull(e.getMessage());
    }
    assertFalse(cache.containsKey("foo"));
    // Callback is called once: the recovery path should be called in
    // handleRetryExhausted (so not in this test)...
    assertEquals(1, callback.attempts);
}
Also used : ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) 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 10 with ExhaustedRetryException

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

the class StatefulRecoveryRetryTests method testExhaustedClearsHistoryAfterLastAttempt.

@Test
public void testExhaustedClearsHistoryAfterLastAttempt() throws Throwable {
    RetryPolicy retryPolicy = new SimpleRetryPolicy(1);
    this.retryTemplate.setRetryPolicy(retryPolicy);
    final String input = "foo";
    RetryState state = new DefaultRetryState(input);
    RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {

        @Override
        public String doWithRetry(RetryContext context) throws Exception {
            throw new RuntimeException("Barf!");
        }
    };
    try {
        this.retryTemplate.execute(callback, state);
        fail("Expected ExhaustedRetryException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    try {
        this.retryTemplate.execute(callback, state);
        fail("Expected ExhaustedRetryException");
    } catch (ExhaustedRetryException e) {
    // expected
    }
    RetryContext context = this.retryTemplate.open(retryPolicy, state);
    // True after exhausted - the history is reset...
    assertTrue(retryPolicy.canRetry(context));
}
Also used : ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) RetryPolicy(cn.taketoday.retry.RetryPolicy) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Aggregations

ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)10 RetryContext (cn.taketoday.retry.RetryContext)6 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)6 Test (org.junit.Test)6 RetryException (cn.taketoday.retry.RetryException)4 RetryPolicy (cn.taketoday.retry.RetryPolicy)4 RetryState (cn.taketoday.retry.RetryState)4 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)4 Advised (cn.taketoday.aop.framework.Advised)2 DataAccessException (cn.taketoday.dao.DataAccessException)2 RetryCallback (cn.taketoday.retry.RetryCallback)2 TerminatedRetryException (cn.taketoday.retry.TerminatedRetryException)2 BackOffContext (cn.taketoday.retry.backoff.BackOffContext)2 BackOffInterruptedException (cn.taketoday.retry.backoff.BackOffInterruptedException)2 BackOffPolicy (cn.taketoday.retry.backoff.BackOffPolicy)2 NoBackOffPolicy (cn.taketoday.retry.backoff.NoBackOffPolicy)2 CircuitBreakerRetryPolicy (cn.taketoday.retry.policy.CircuitBreakerRetryPolicy)2 DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)2 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)2 Test (org.junit.jupiter.api.Test)2