use of cn.taketoday.retry.RetryCallback in project today-infrastructure by TAKETODAY.
the class MethodInvocationRetryListenerSupportTests method testCloseWithRetryCallbackShouldntCallDoCloseMethod.
@Test
public void testCloseWithRetryCallbackShouldntCallDoCloseMethod() {
final AtomicInteger callsOnDoCloseMethod = new AtomicInteger(0);
MethodInvocationRetryListenerSupport support = new MethodInvocationRetryListenerSupport() {
@Override
protected <T, E extends Throwable> void doClose(RetryContext context, MethodInvocationRetryCallback<T, E> callback, Throwable throwable) {
callsOnDoCloseMethod.incrementAndGet();
}
};
RetryContext context = mock(RetryContext.class);
RetryCallback callback = mock(RetryCallback.class);
support.close(context, callback, null);
assertEquals(0, callsOnDoCloseMethod.get());
}
use of cn.taketoday.retry.RetryCallback in project today-infrastructure by TAKETODAY.
the class RetryTemplateTests method testSpecificExceptionRetry.
@Test
public void testSpecificExceptionRetry() throws Throwable {
for (int x = 1; x <= 10; x++) {
final int attemptsBeforeSuccess = x;
final AtomicInteger attempts = new AtomicInteger(0);
RetryCallback<String, IllegalStateException> callback = new RetryCallback<String, IllegalStateException>() {
@Override
public String doWithRetry(RetryContext context) throws IllegalStateException {
if (attempts.incrementAndGet() < attemptsBeforeSuccess) {
// can't really tell the difference between runtime exceptions.
throw new IllegalArgumentException("Planned");
}
return "foo";
}
};
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
retryTemplate.execute(callback);
assertEquals(x, attempts.get());
}
}
use of cn.taketoday.retry.RetryCallback 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.RetryCallback 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"));
}
}
use of cn.taketoday.retry.RetryCallback 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