use of cn.taketoday.retry.support.DefaultRetryState in project today-infrastructure by TAKETODAY.
the class StatefulRetryOperationsInterceptorTests method testKeyGeneratorReturningNull.
@SuppressWarnings("unchecked")
@Test
public void testKeyGeneratorReturningNull() throws Throwable {
this.interceptor.setKeyGenerator(mock(MethodArgumentsKeyGenerator.class));
this.interceptor.setLabel("foo");
RetryOperations template = mock(RetryOperations.class);
this.interceptor.setRetryOperations(template);
MethodInvocation invocation = mock(MethodInvocation.class);
when(invocation.getArguments()).thenReturn(new Object[] { new Object() });
this.interceptor.invoke(invocation);
ArgumentCaptor<DefaultRetryState> captor = ArgumentCaptor.forClass(DefaultRetryState.class);
verify(template).execute(any(RetryCallback.class), any(RecoveryCallback.class), captor.capture());
assertNull(captor.getValue().getKey());
}
use of cn.taketoday.retry.support.DefaultRetryState in project today-infrastructure by TAKETODAY.
the class StatefulRetryOperationsInterceptorTests method testKeyGeneratorAndRawKey.
@SuppressWarnings("unchecked")
@Test
public void testKeyGeneratorAndRawKey() throws Throwable {
this.interceptor.setKeyGenerator(item -> "bar");
this.interceptor.setLabel("foo");
this.interceptor.setUseRawKey(true);
RetryOperations template = mock(RetryOperations.class);
this.interceptor.setRetryOperations(template);
MethodInvocation invocation = mock(MethodInvocation.class);
when(invocation.getArguments()).thenReturn(new Object[] { new Object() });
this.interceptor.invoke(invocation);
ArgumentCaptor<DefaultRetryState> captor = ArgumentCaptor.forClass(DefaultRetryState.class);
verify(template).execute(any(RetryCallback.class), any(RecoveryCallback.class), captor.capture());
assertEquals("bar", captor.getValue().getKey());
}
use of cn.taketoday.retry.support.DefaultRetryState 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.DefaultRetryState 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);
}
}
use of cn.taketoday.retry.support.DefaultRetryState in project today-infrastructure by TAKETODAY.
the class StatefulRetryIntegrationTests method testExponentialBackOffIsExponential.
@Test
public void testExponentialBackOffIsExponential() throws Throwable {
ExponentialBackOffPolicy policy = new ExponentialBackOffPolicy();
policy.setInitialInterval(100);
policy.setMultiplier(1.5);
RetryTemplate template = new RetryTemplate();
template.setBackOffPolicy(policy);
final List<Long> times = new ArrayList<Long>();
RetryState retryState = new DefaultRetryState("bar");
for (int i = 0; i < 3; i++) {
try {
template.execute(new RetryCallback<String, Exception>() {
public String doWithRetry(RetryContext context) throws Exception {
times.add(System.currentTimeMillis());
throw new Exception("Fail");
}
}, new RecoveryCallback<String>() {
public String recover(RetryContext context) throws Exception {
return null;
}
}, retryState);
} catch (Exception e) {
assertTrue(e.getMessage().equals("Fail"));
}
}
assertEquals(3, times.size());
assertTrue(times.get(1) - times.get(0) >= 100);
assertTrue(times.get(2) - times.get(1) >= 150);
}
Aggregations