use of cn.taketoday.retry.RecoveryCallback in project today-infrastructure by TAKETODAY.
the class FatalExceptionRetryPolicyTests method testFatalExceptionWithoutState.
@Test
public void testFatalExceptionWithoutState() throws Throwable {
MockRetryCallback callback = new MockRetryCallback();
callback.setExceptionToThrow(new IllegalArgumentException());
RetryTemplate retryTemplate = new RetryTemplate();
// Make sure certain exceptions are fatal...
Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>();
map.put(IllegalArgumentException.class, false);
map.put(IllegalStateException.class, false);
// ... and allow multiple attempts
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 {
result = retryTemplate.execute(callback, recoveryCallback);
} catch (IllegalArgumentException e) {
// We should swallow the exception when recovery is possible
fail("Did not expect IllegalArgumentException");
}
// Callback is called once: the recovery path should also be called
assertEquals(1, callback.attempts);
assertEquals("bar", result);
}
use of cn.taketoday.retry.RecoveryCallback 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.RecoveryCallback 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));
}
use of cn.taketoday.retry.RecoveryCallback in project today-framework 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));
}
use of cn.taketoday.retry.RecoveryCallback in project today-framework by TAKETODAY.
the class StatefulRecoveryRetryTests method testRecover.
@Test
public void testRecover() throws Throwable {
this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
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!");
}
};
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;
try {
result = this.retryTemplate.execute(callback, recoveryCallback, state);
fail("Expected exception on first try");
} catch (Exception e) {
// expected...
}
// 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