use of cn.taketoday.retry.support.RetryTemplate in project today-infrastructure 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-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);
}
}
use of cn.taketoday.retry.support.RetryTemplate 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);
}
use of cn.taketoday.retry.support.RetryTemplate in project today-infrastructure 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 FatalExceptionRetryPolicyTests method testFatalExceptionWithState.
@Test
public void testFatalExceptionWithState() throws Throwable {
MockRetryCallback callback = new MockRetryCallback();
callback.setExceptionToThrow(new IllegalArgumentException());
RetryTemplate retryTemplate = new RetryTemplate();
Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>();
map.put(IllegalArgumentException.class, false);
map.put(IllegalStateException.class, false);
SimpleRetryPolicy policy = new SimpleRetryPolicy(3, map);
retryTemplate.setRetryPolicy(policy);
RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {
public String recover(RetryContext context) throws Exception {
return "bar";
}
};
Object result = null;
try {
retryTemplate.execute(callback, recoveryCallback, new DefaultRetryState("foo"));
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
// If stateful we have to always rethrow. Clients who want special
// cases have to implement them in the callback
}
result = retryTemplate.execute(callback, recoveryCallback, new DefaultRetryState("foo"));
// Callback is called once: the recovery path should also be called
assertEquals(1, callback.attempts);
assertEquals("bar", result);
}
Aggregations