use of cn.taketoday.retry.support.RetryTemplate in project today-framework 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);
}
use of cn.taketoday.retry.support.RetryTemplate in project today-framework by TAKETODAY.
the class StatefulRetryIntegrationTests method testExternalRetryWithSuccessOnRetryAndSerializedContext.
@Test
public void testExternalRetryWithSuccessOnRetryAndSerializedContext() throws Throwable {
MockRetryCallback callback = new MockRetryCallback();
RetryState retryState = new DefaultRetryState("foo");
RetryTemplate retryTemplate = new RetryTemplate();
RetryContextCache cache = new SerializedMapRetryContextCache();
retryTemplate.setRetryContextCache(cache);
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2));
assertFalse(cache.containsKey("foo"));
Object result = "start_foo";
try {
result = retryTemplate.execute(callback, retryState);
// The first failed attempt we expect to retry...
fail("Expected RuntimeException");
} catch (RuntimeException e) {
assertNull(e.getMessage());
}
assertTrue(cache.containsKey("foo"));
result = retryTemplate.execute(callback, retryState);
assertFalse(cache.containsKey("foo"));
assertEquals(2, callback.attempts);
assertEquals(1, callback.context.getRetryCount());
assertEquals("bar", result);
}
use of cn.taketoday.retry.support.RetryTemplate 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);
}
}
use of cn.taketoday.retry.support.RetryTemplate in project today-framework by TAKETODAY.
the class StatisticsListenerTests method testStatefulUnsuccessful.
@Test
public void testStatefulUnsuccessful() 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 + 1);
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
for (int i = 0; i < x + 1; 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.getAbortCount());
assertEquals((x + 1) * x / 2, stats.getStartedCount());
assertEquals(stats.getStartedCount(), stats.getErrorCount());
}
}
use of cn.taketoday.retry.support.RetryTemplate in project today-framework by TAKETODAY.
the class StatisticsListenerTests method testStatefulRecovery.
@Test
public void testStatefulRecovery() 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 + 1);
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
for (int i = 0; i < x + 1; i++) {
try {
retryTemplate.execute(callback, new RecoveryCallback<Object>() {
@Override
public Object recover(RetryContext context) throws Exception {
return null;
}
}, 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.getRecoveryCount());
assertEquals((x + 1) * x / 2, stats.getStartedCount());
assertEquals(stats.getStartedCount(), stats.getErrorCount());
}
}
Aggregations