use of cn.taketoday.retry.RetryState 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.RetryState 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.RetryState 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.RetryState 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());
}
use of cn.taketoday.retry.RetryState in project today-infrastructure by TAKETODAY.
the class StatefulRecoveryRetryTests method testSwitchToStatelessForNoRollback.
@Test
public void testSwitchToStatelessForNoRollback() throws Throwable {
this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
// Roll back for these:
BinaryExceptionClassifier classifier = new BinaryExceptionClassifier(Collections.<Class<? extends Throwable>>singleton(DataAccessException.class));
// ...but not these:
assertFalse(classifier.classify(new RuntimeException()));
final String input = "foo";
RetryState state = new DefaultRetryState(input, classifier);
RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {
@Override
public String doWithRetry(RetryContext context) throws Exception {
throw new RuntimeException("Barf!");
}
};
RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {
@Override
public String recover(RetryContext context) {
StatefulRecoveryRetryTests.this.count++;
StatefulRecoveryRetryTests.this.list.add(input);
return input;
}
};
Object result = null;
// On the second retry, the recovery path is taken...
result = this.retryTemplate.execute(callback, recoveryCallback, state);
// default result is the item
assertEquals(input, result);
assertEquals(1, this.count);
assertEquals(input, this.list.get(0));
}
Aggregations