use of cn.taketoday.retry.policy.SimpleRetryPolicy in project today-infrastructure by TAKETODAY.
the class RetryOperationsInterceptorTests method testDefaultInterceptorWithRetryListenerInspectingTheMethodInvocation.
@Test
public void testDefaultInterceptorWithRetryListenerInspectingTheMethodInvocation() throws Exception {
final String label = "FOO";
final String classTagName = "class";
final String methodTagName = "method";
final String labelTagName = "label";
final Map<String, String> monitoringTags = new HashMap<String, String>();
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(new SimpleRetryPolicy(2));
template.registerListener(new MethodInvocationRetryListenerSupport() {
@Override
protected <T, E extends Throwable> void doClose(RetryContext context, MethodInvocationRetryCallback<T, E> callback, Throwable throwable) {
monitoringTags.put(labelTagName, callback.getLabel());
Method method = callback.getInvocation().getMethod();
monitoringTags.put(classTagName, method.getDeclaringClass().getSimpleName());
monitoringTags.put(methodTagName, method.getName());
}
});
this.interceptor.setLabel(label);
this.interceptor.setRetryOperations(template);
((Advised) this.service).addAdvice(this.interceptor);
this.service.service();
assertEquals(2, count);
assertEquals(3, monitoringTags.entrySet().size());
assertThat(monitoringTags.get(labelTagName), equalTo(label));
assertThat(monitoringTags.get(classTagName), equalTo(Service.class.getSimpleName()));
assertThat(monitoringTags.get(methodTagName), equalTo("service"));
}
use of cn.taketoday.retry.policy.SimpleRetryPolicy 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.policy.SimpleRetryPolicy 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());
}
}
use of cn.taketoday.retry.policy.SimpleRetryPolicy 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.policy.SimpleRetryPolicy 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);
}
}
Aggregations