use of cn.taketoday.retry.support.RetryTemplate in project today-infrastructure by TAKETODAY.
the class StatefulRetryOperationsInterceptorTests method testTransformerRecoveryAfterTooManyAttempts.
@Test
public void testTransformerRecoveryAfterTooManyAttempts() throws Exception {
((Advised) transformer).addAdvice(interceptor);
interceptor.setRetryOperations(retryTemplate);
retryTemplate.setRetryPolicy(new NeverRetryPolicy());
try {
transformer.transform("foo");
fail("Expected Exception.");
} catch (Exception e) {
String message = e.getMessage();
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
}
assertEquals(1, count);
interceptor.setRecoverer((MethodInvocationRecoverer<Collection<String>>) (data, cause) -> {
count++;
return Collections.singleton((String) data[0]);
});
Collection<String> result = transformer.transform("foo");
assertEquals(2, count);
assertEquals(1, result.size());
}
use of cn.taketoday.retry.support.RetryTemplate in project today-infrastructure by TAKETODAY.
the class StatefulRetryOperationsInterceptorTests method testInterceptorChainWithRetry.
@Test
public void testInterceptorChainWithRetry() throws Exception {
((Advised) service).addAdvice(interceptor);
final List<String> list = new ArrayList<>();
((Advised) service).addAdvice((MethodInterceptor) invocation -> {
list.add("chain");
return invocation.proceed();
});
interceptor.setRetryOperations(retryTemplate);
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2));
try {
service.service("foo");
fail("Expected Exception.");
} catch (Exception e) {
String message = e.getMessage();
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
}
assertEquals(1, count);
service.service("foo");
assertEquals(2, count);
assertEquals(2, list.size());
}
use of cn.taketoday.retry.support.RetryTemplate 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.support.RetryTemplate in project today-infrastructure by TAKETODAY.
the class CircuitBreakerStatisticsTests method init.
@Before
public void init() {
this.callback = new MockRetryCallback();
this.recovery = new RecoveryCallback<Object>() {
@Override
public Object recover(RetryContext context) throws Exception {
return RECOVERED;
}
};
this.retryTemplate = new RetryTemplate();
this.cache = new MapRetryContextCache();
this.retryTemplate.setRetryContextCache(this.cache);
retryTemplate.setListeners(new RetryListener[] { listener });
this.callback.setAttemptsBeforeSuccess(1);
// No rollback by default (so exceptions are not rethrown)
this.state = new DefaultRetryState("retry", new BinaryExceptionClassifier(false));
}
use of cn.taketoday.retry.support.RetryTemplate in project today-infrastructure by TAKETODAY.
the class StatisticsListenerTests method testStatelessRecovery.
@Test
public void testStatelessRecovery() 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 + 1);
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
retryTemplate.execute(callback, new RecoveryCallback<Object>() {
@Override
public Object recover(RetryContext context) throws Exception {
return null;
}
});
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