Search in sources :

Example 1 with MapRetryContextCache

use of cn.taketoday.retry.policy.MapRetryContextCache 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 2 with MapRetryContextCache

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

the class StatefulRecoveryRetryTests method testCacheCapacityNotReachedIfRecovered.

@Test
public void testCacheCapacityNotReachedIfRecovered() throws Throwable {
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(1);
    this.retryTemplate.setRetryPolicy(retryPolicy);
    this.retryTemplate.setRetryContextCache(new MapRetryContextCache(2));
    final StringHolder item = new StringHolder("foo");
    RetryState state = new DefaultRetryState(item);
    RetryCallback<Object, Exception> callback = new RetryCallback<Object, Exception>() {

        @Override
        public Object doWithRetry(RetryContext context) throws Exception {
            StatefulRecoveryRetryTests.this.count++;
            throw new RuntimeException("Barf!");
        }
    };
    RecoveryCallback<Object> recoveryCallback = new RecoveryCallback<Object>() {

        @Override
        public Object recover(RetryContext context) throws Exception {
            return null;
        }
    };
    try {
        this.retryTemplate.execute(callback, recoveryCallback, state);
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    this.retryTemplate.execute(callback, recoveryCallback, state);
    RetryContext context = this.retryTemplate.open(retryPolicy, state);
    // True after exhausted - the history is reset...
    assertEquals(0, context.getRetryCount());
}
Also used : RecoveryCallback(cn.taketoday.retry.RecoveryCallback) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Example 3 with MapRetryContextCache

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

the class StatefulRecoveryRetryTests method testCacheCapacity.

@Test
public void testCacheCapacity() throws Throwable {
    this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    this.retryTemplate.setRetryContextCache(new MapRetryContextCache(1));
    RetryCallback<Object, Exception> callback = context -> {
        StatefulRecoveryRetryTests.this.count++;
        throw new RuntimeException("Barf!");
    };
    try {
        this.retryTemplate.execute(callback, new DefaultRetryState("foo"));
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    try {
        this.retryTemplate.execute(callback, new DefaultRetryState("bar"));
        fail("Expected RetryException");
    } catch (RetryException e) {
        String message = e.getNestedMessage();
        assertTrue("Message does not contain 'capacity': " + message, message.contains("capacity"));
    }
}
Also used : RetryCallback(cn.taketoday.retry.RetryCallback) Assert.assertNotNull(org.junit.Assert.assertNotNull) RetryPolicy(cn.taketoday.retry.RetryPolicy) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) ArrayList(java.util.ArrayList) RetryState(cn.taketoday.retry.RetryState) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) List(java.util.List) RetryException(cn.taketoday.retry.RetryException) RetryContext(cn.taketoday.retry.RetryContext) Assert.assertFalse(org.junit.Assert.assertFalse) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Assert.fail(org.junit.Assert.fail) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) Test(org.junit.Test)

Example 4 with MapRetryContextCache

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

the class StatefulRecoveryRetryTests method testCacheCapacityNotReachedIfRecovered.

@Test
public void testCacheCapacityNotReachedIfRecovered() throws Throwable {
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(1);
    this.retryTemplate.setRetryPolicy(retryPolicy);
    this.retryTemplate.setRetryContextCache(new MapRetryContextCache(2));
    final StringHolder item = new StringHolder("foo");
    RetryState state = new DefaultRetryState(item);
    RetryCallback<Object, Exception> callback = new RetryCallback<Object, Exception>() {

        @Override
        public Object doWithRetry(RetryContext context) throws Exception {
            StatefulRecoveryRetryTests.this.count++;
            throw new RuntimeException("Barf!");
        }
    };
    RecoveryCallback<Object> recoveryCallback = new RecoveryCallback<Object>() {

        @Override
        public Object recover(RetryContext context) throws Exception {
            return null;
        }
    };
    try {
        this.retryTemplate.execute(callback, recoveryCallback, state);
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    this.retryTemplate.execute(callback, recoveryCallback, state);
    RetryContext context = this.retryTemplate.open(retryPolicy, state);
    // True after exhausted - the history is reset...
    assertEquals(0, context.getRetryCount());
}
Also used : RecoveryCallback(cn.taketoday.retry.RecoveryCallback) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Example 5 with MapRetryContextCache

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

the class StatefulRecoveryRetryTests method testCacheCapacity.

@Test
public void testCacheCapacity() throws Throwable {
    this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    this.retryTemplate.setRetryContextCache(new MapRetryContextCache(1));
    RetryCallback<Object, Exception> callback = context -> {
        StatefulRecoveryRetryTests.this.count++;
        throw new RuntimeException("Barf!");
    };
    try {
        this.retryTemplate.execute(callback, new DefaultRetryState("foo"));
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    try {
        this.retryTemplate.execute(callback, new DefaultRetryState("bar"));
        fail("Expected RetryException");
    } catch (RetryException e) {
        String message = e.getNestedMessage();
        assertTrue("Message does not contain 'capacity': " + message, message.contains("capacity"));
    }
}
Also used : RetryCallback(cn.taketoday.retry.RetryCallback) Assert.assertNotNull(org.junit.Assert.assertNotNull) RetryPolicy(cn.taketoday.retry.RetryPolicy) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) ArrayList(java.util.ArrayList) RetryState(cn.taketoday.retry.RetryState) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) List(java.util.List) RetryException(cn.taketoday.retry.RetryException) RetryContext(cn.taketoday.retry.RetryContext) Assert.assertFalse(org.junit.Assert.assertFalse) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Assert.fail(org.junit.Assert.fail) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) Test(org.junit.Test)

Aggregations

ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)6 RetryContext (cn.taketoday.retry.RetryContext)6 MapRetryContextCache (cn.taketoday.retry.policy.MapRetryContextCache)6 BinaryExceptionClassifier (cn.taketoday.classify.BinaryExceptionClassifier)4 DataAccessException (cn.taketoday.dao.DataAccessException)4 RecoveryCallback (cn.taketoday.retry.RecoveryCallback)4 RetryCallback (cn.taketoday.retry.RetryCallback)4 RetryException (cn.taketoday.retry.RetryException)4 RetryState (cn.taketoday.retry.RetryState)4 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)4 Test (org.junit.Test)4 RetryPolicy (cn.taketoday.retry.RetryPolicy)2 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)2 DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)2 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2