use of cn.taketoday.retry.RetryPolicy in project today-framework by TAKETODAY.
the class ExceptionClassifierRetryPolicy method registerThrowable.
/**
* Delegate to the policy currently activated in the context.
*
* @see RetryPolicy#registerThrowable(RetryContext,
* Throwable)
*/
public void registerThrowable(RetryContext context, Throwable throwable) {
RetryPolicy policy = (RetryPolicy) context;
policy.registerThrowable(context, throwable);
((RetryContextSupport) context).registerThrowable(throwable);
}
use of cn.taketoday.retry.RetryPolicy in project today-framework by TAKETODAY.
the class StatefulRecoveryRetryTests method testExhaustedClearsHistoryAfterLastAttempt.
@Test
public void testExhaustedClearsHistoryAfterLastAttempt() throws Throwable {
RetryPolicy retryPolicy = new SimpleRetryPolicy(1);
this.retryTemplate.setRetryPolicy(retryPolicy);
final String input = "foo";
RetryState state = new DefaultRetryState(input);
RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {
@Override
public String doWithRetry(RetryContext context) throws Exception {
throw new RuntimeException("Barf!");
}
};
try {
this.retryTemplate.execute(callback, state);
fail("Expected ExhaustedRetryException");
} catch (RuntimeException e) {
assertEquals("Barf!", e.getMessage());
}
try {
this.retryTemplate.execute(callback, state);
fail("Expected ExhaustedRetryException");
} catch (ExhaustedRetryException e) {
// expected
}
RetryContext context = this.retryTemplate.open(retryPolicy, state);
// True after exhausted - the history is reset...
assertTrue(retryPolicy.canRetry(context));
}
use of cn.taketoday.retry.RetryPolicy in project today-framework by TAKETODAY.
the class StatefulRecoveryRetryTests method testKeyGeneratorNotConsistentAfterFailure.
@Test
public void testKeyGeneratorNotConsistentAfterFailure() throws Throwable {
RetryPolicy retryPolicy = new SimpleRetryPolicy(3);
this.retryTemplate.setRetryPolicy(retryPolicy);
final StringHolder item = new StringHolder("bar");
RetryState state = new DefaultRetryState(item);
RetryCallback<StringHolder, Exception> callback = new RetryCallback<StringHolder, Exception>() {
@Override
public StringHolder doWithRetry(RetryContext context) throws Exception {
// This simulates what happens if someone uses a primary key
// for hashCode and equals and then relies on default key
// generator
item.string = item.string + (StatefulRecoveryRetryTests.this.count++);
throw new RuntimeException("Barf!");
}
};
try {
this.retryTemplate.execute(callback, state);
fail("Expected RuntimeException");
} catch (RuntimeException ex) {
String message = ex.getMessage();
assertEquals("Barf!", message);
}
// item already...
try {
this.retryTemplate.execute(callback, state);
fail("Expected RetryException");
} catch (RetryException ex) {
String message = ex.getNestedMessage();
assertTrue("Message doesn't contain 'inconsistent': " + message, message.contains("inconsistent"));
}
RetryContext context = this.retryTemplate.open(retryPolicy, state);
// True after exhausted - the history is reset...
assertEquals(0, context.getRetryCount());
}
use of cn.taketoday.retry.RetryPolicy in project today-framework by TAKETODAY.
the class RetryTemplateBuilderTests method testCustomPolicy.
@Test
public void testCustomPolicy() {
RetryPolicy customPolicy = mock(RetryPolicy.class);
RetryTemplate template = RetryTemplate.builder().customPolicy(customPolicy).build();
PolicyTuple policyTuple = PolicyTuple.extractWithAsserts(template);
assertDefaultClassifier(policyTuple);
Assert.assertEquals(customPolicy, policyTuple.baseRetryPolicy);
}
use of cn.taketoday.retry.RetryPolicy in project today-framework by TAKETODAY.
the class ExceptionClassifierRetryPolicyTests method testClose.
@SuppressWarnings("serial")
@Test
public void testClose() throws Exception {
policy.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {
public RetryPolicy classify(Throwable throwable) {
return new MockRetryPolicySupport() {
public void close(RetryContext context) {
count++;
}
};
}
});
RetryContext context = policy.open(null);
// The mapped (child) policy hasn't been used yet, so if we close now
// we don't incur the possible expense of creating the child context.
policy.close(context);
// not classified yet
assertEquals(0, count);
// This forces a child context to be created and the child policy is
// then closed
policy.registerThrowable(context, new IllegalStateException());
policy.close(context);
// now classified
assertEquals(1, count);
}
Aggregations