use of cn.taketoday.retry.RetryListener in project today-framework by TAKETODAY.
the class RetryListenerTests method testCloseInterceptorsAfterRetry.
@Test
public void testCloseInterceptorsAfterRetry() throws Throwable {
template.registerListener(new RetryListener() {
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable t) {
list.add("" + count);
// The last attempt should have been successful:
assertNull(t);
}
});
template.execute(new RetryCallback<String, Exception>() {
public String doWithRetry(RetryContext context) throws Exception {
if (count++ < 1)
throw new RuntimeException("Retry!");
return null;
}
});
assertEquals(2, count);
// The close interceptor was only called once:
assertEquals(1, list.size());
// We succeeded on the second try:
assertEquals("2", list.get(0));
}
use of cn.taketoday.retry.RetryListener in project today-framework by TAKETODAY.
the class RetryListenerTests method testOpenCanVetoRetry.
@Test
public void testOpenCanVetoRetry() throws Throwable {
template.registerListener(new RetryListener() {
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
list.add("1");
return false;
}
});
try {
template.execute(new RetryCallback<String, Exception>() {
public String doWithRetry(RetryContext context) throws Exception {
count++;
return null;
}
});
fail("Expected TerminatedRetryException");
} catch (TerminatedRetryException e) {
// expected
}
assertEquals(0, count);
assertEquals(1, list.size());
assertEquals("1", list.get(0));
}
use of cn.taketoday.retry.RetryListener in project today-infrastructure by TAKETODAY.
the class RetryListenerTests method testCloseInterceptorsAfterRetry.
@Test
public void testCloseInterceptorsAfterRetry() throws Throwable {
template.registerListener(new RetryListener() {
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable t) {
list.add("" + count);
// The last attempt should have been successful:
assertNull(t);
}
});
template.execute(new RetryCallback<String, Exception>() {
public String doWithRetry(RetryContext context) throws Exception {
if (count++ < 1)
throw new RuntimeException("Retry!");
return null;
}
});
assertEquals(2, count);
// The close interceptor was only called once:
assertEquals(1, list.size());
// We succeeded on the second try:
assertEquals("2", list.get(0));
}
use of cn.taketoday.retry.RetryListener in project today-infrastructure by TAKETODAY.
the class RetryListenerTests method testOnError.
@Test
public void testOnError() throws Throwable {
template.setRetryPolicy(new NeverRetryPolicy());
template.setListeners(new RetryListener[] { new RetryListener() {
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
list.add("1");
}
}, new RetryListener() {
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
list.add("2");
}
} });
try {
template.execute(new RetryCallback<String, Exception>() {
public String doWithRetry(RetryContext context) throws Exception {
count++;
throw new IllegalStateException("foo");
}
});
fail("Expected IllegalStateException");
} catch (IllegalStateException e) {
assertEquals("foo", e.getMessage());
}
// never retry so callback is executed once
assertEquals(1, count);
assertEquals(2, list.size());
// interceptors are called in reverse order on error...
assertEquals("2", list.get(0));
}
use of cn.taketoday.retry.RetryListener in project today-infrastructure by TAKETODAY.
the class RetryOperationsInterceptorTests method setUp.
@Before
public void setUp() throws Exception {
this.interceptor = new RetryOperationsInterceptor();
RetryTemplate retryTemplate = new RetryTemplate();
final AtomicBoolean calledFirst = new AtomicBoolean();
retryTemplate.registerListener(new RetryListener() {
@Override
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
calledFirst.set(true);
return true;
}
@Override
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
RetryOperationsInterceptorTests.this.context = context;
}
});
retryTemplate.registerListener(new RetryListener() {
@Override
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
assertFalse(calledFirst.get());
return true;
}
}, 0);
this.interceptor.setRetryOperations(retryTemplate);
this.target = new ServiceImpl();
this.service = ProxyFactory.getProxy(Service.class, new SingletonTargetSource(this.target));
count = 0;
transactionCount = 0;
}
Aggregations