use of cn.taketoday.retry.RetryPolicy in project today-infrastructure by TAKETODAY.
the class ExceptionClassifierRetryPolicy method close.
/**
* Delegate to the policy currently activated in the context.
*
* @see RetryPolicy#close(RetryContext)
*/
public void close(RetryContext context) {
RetryPolicy policy = (RetryPolicy) context;
policy.close(context);
}
use of cn.taketoday.retry.RetryPolicy in project today-infrastructure by TAKETODAY.
the class ExceptionClassifierRetryPolicyTests method testClassifierOperates.
@SuppressWarnings("serial")
@Test
public void testClassifierOperates() throws Exception {
RetryContext context = policy.open(null);
assertNotNull(context);
assertTrue(policy.canRetry(context));
policy.registerThrowable(context, new IllegalArgumentException());
// NeverRetryPolicy is the
assertFalse(policy.canRetry(context));
// default
policy.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {
public RetryPolicy classify(Throwable throwable) {
if (throwable != null) {
return new AlwaysRetryPolicy();
}
return new NeverRetryPolicy();
}
});
// The context saves the classifier, so changing it now has no effect
assertFalse(policy.canRetry(context));
policy.registerThrowable(context, new IllegalArgumentException());
assertFalse(policy.canRetry(context));
// But now the classifier will be active in the new context...
context = policy.open(null);
assertTrue(policy.canRetry(context));
policy.registerThrowable(context, new IllegalArgumentException());
assertTrue(policy.canRetry(context));
}
use of cn.taketoday.retry.RetryPolicy in project today-infrastructure 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);
}
use of cn.taketoday.retry.RetryPolicy in project today-infrastructure 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-infrastructure 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));
}
Aggregations