Retry policy needed to know if the callback was successful.
This commit is contained in:
@@ -29,7 +29,7 @@ public class AlwaysRetryPolicyTests extends TestCase {
|
||||
assertTrue(policy.canRetry(context));
|
||||
policy.registerThrowable(context, null);
|
||||
assertTrue(policy.canRetry(context));
|
||||
policy.close(context);
|
||||
policy.close(context, true);
|
||||
assertTrue(policy.canRetry(context));
|
||||
}
|
||||
|
||||
|
||||
@@ -78,17 +78,17 @@ public class CompositeRetryPolicyTests extends TestCase {
|
||||
final List<String> list = new ArrayList<String>();
|
||||
CompositeRetryPolicy policy = new CompositeRetryPolicy();
|
||||
policy.setPolicies(new RetryPolicy[] { new MockRetryPolicySupport() {
|
||||
public void close(RetryContext context) {
|
||||
public void close(RetryContext context, boolean succeeded) {
|
||||
list.add("1");
|
||||
}
|
||||
}, new MockRetryPolicySupport() {
|
||||
public void close(RetryContext context) {
|
||||
public void close(RetryContext context, boolean succeeded) {
|
||||
list.add("2");
|
||||
}
|
||||
} });
|
||||
RetryContext context = policy.open(null, null);
|
||||
assertNotNull(context);
|
||||
policy.close(context);
|
||||
policy.close(context, true);
|
||||
assertEquals(2, list.size());
|
||||
}
|
||||
|
||||
@@ -96,19 +96,19 @@ public class CompositeRetryPolicyTests extends TestCase {
|
||||
final List<String> list = new ArrayList<String>();
|
||||
CompositeRetryPolicy policy = new CompositeRetryPolicy();
|
||||
policy.setPolicies(new RetryPolicy[] { new MockRetryPolicySupport() {
|
||||
public void close(RetryContext context) {
|
||||
public void close(RetryContext context, boolean succeeded) {
|
||||
list.add("1");
|
||||
throw new RuntimeException("Pah!");
|
||||
}
|
||||
}, new MockRetryPolicySupport() {
|
||||
public void close(RetryContext context) {
|
||||
public void close(RetryContext context, boolean succeeded) {
|
||||
list.add("2");
|
||||
}
|
||||
} });
|
||||
RetryContext context = policy.open(null, null);
|
||||
assertNotNull(context);
|
||||
try {
|
||||
policy.close(context);
|
||||
policy.close(context, true);
|
||||
fail("Expected RuntimeException");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("Pah!", e.getMessage());
|
||||
|
||||
@@ -106,7 +106,7 @@ public class ExceptionClassifierRetryPolicyTests extends TestCase {
|
||||
public void testClose() throws Exception {
|
||||
policy.setPolicyMap(Collections.singletonMap(ExceptionClassifierSupport.DEFAULT,
|
||||
(RetryPolicy) new MockRetryPolicySupport() {
|
||||
public void close(RetryContext context) {
|
||||
public void close(RetryContext context, boolean succeeded) {
|
||||
count++;
|
||||
}
|
||||
}));
|
||||
@@ -114,12 +114,12 @@ public class ExceptionClassifierRetryPolicyTests extends TestCase {
|
||||
|
||||
// The mapped (child) policy hasn't been used yet, so if we close now
|
||||
// we don't incur the possible expense of ceating the child context.
|
||||
policy.close(context);
|
||||
policy.close(context, true);
|
||||
assertEquals(0, count); // not classified yet
|
||||
// This forces a child context to be created and the child policy is
|
||||
// then closed
|
||||
policy.registerThrowable(context, new IllegalStateException());
|
||||
policy.close(context);
|
||||
policy.close(context, true);
|
||||
assertEquals(1, count); // now classified
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright 2006-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.retry.policy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.retry.RetryCallback;
|
||||
import org.springframework.batch.retry.RetryContext;
|
||||
import org.springframework.batch.retry.callback.RecoveryRetryCallback;
|
||||
import org.springframework.batch.retry.support.RetryTemplate;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class ExternalRetryIntergrationTests {
|
||||
|
||||
@Test
|
||||
public void testExternalRetryWithFailAndNoRetry() throws Exception {
|
||||
MockRetryCallback callback = new MockRetryCallback();
|
||||
|
||||
RecoveryRetryCallback recoveryCallback = new RecoveryRetryCallback("foo", callback);
|
||||
|
||||
RetryTemplate retryTemplate = new RetryTemplate();
|
||||
RecoveryCallbackRetryPolicy retryPolicy = new RecoveryCallbackRetryPolicy(new SimpleRetryPolicy(1));
|
||||
MapRetryContextCache cache = new MapRetryContextCache();
|
||||
retryPolicy.setRetryContextCache(cache);
|
||||
retryTemplate.setRetryPolicy(retryPolicy);
|
||||
|
||||
assertFalse(cache.containsKey("foo"));
|
||||
|
||||
Object result = "start_foo";
|
||||
try {
|
||||
result = retryTemplate.execute(recoveryCallback);
|
||||
// The first failed attempt we expect to retry...
|
||||
fail("Expected RuntimeException");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
assertNull(e.getMessage());
|
||||
}
|
||||
|
||||
assertTrue(cache.containsKey("foo"));
|
||||
|
||||
try {
|
||||
result = retryTemplate.execute(recoveryCallback);
|
||||
// We always get a second attempt...
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// This is now the "exhausted" message:
|
||||
assertNotNull(e.getMessage());
|
||||
// But if template is external we should
|
||||
// swallow the exception when retry is impossible.
|
||||
fail("Did not expect IllegalArgumentException");
|
||||
}
|
||||
|
||||
assertFalse(cache.containsKey("foo"));
|
||||
|
||||
// Callback is called once: the recovery path should be called in
|
||||
// handleRetryExhausted (so not in this test)...
|
||||
assertEquals(1, callback.attempts);
|
||||
assertEquals(null, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExternalRetryWithSuccessOnRetry() throws Exception {
|
||||
MockRetryCallback callback = new MockRetryCallback();
|
||||
|
||||
RecoveryRetryCallback recoveryCallback = new RecoveryRetryCallback("foo", callback);
|
||||
|
||||
RetryTemplate retryTemplate = new RetryTemplate();
|
||||
RecoveryCallbackRetryPolicy retryPolicy = new RecoveryCallbackRetryPolicy(new SimpleRetryPolicy(2));
|
||||
MapRetryContextCache cache = new MapRetryContextCache();
|
||||
retryPolicy.setRetryContextCache(cache);
|
||||
retryTemplate.setRetryPolicy(retryPolicy);
|
||||
|
||||
assertFalse(cache.containsKey("foo"));
|
||||
|
||||
Object result = "start_foo";
|
||||
try {
|
||||
result = retryTemplate.execute(recoveryCallback);
|
||||
// The first failed attempt we expect to retry...
|
||||
fail("Expected RuntimeException");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
assertNull(e.getMessage());
|
||||
}
|
||||
|
||||
assertTrue(cache.containsKey("foo"));
|
||||
|
||||
result = retryTemplate.execute(recoveryCallback);
|
||||
|
||||
assertFalse(cache.containsKey("foo"));
|
||||
|
||||
assertEquals(2, callback.attempts);
|
||||
assertEquals("bar", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
private final class MockRetryCallback implements RetryCallback {
|
||||
int attempts = 0;
|
||||
|
||||
public Object doWithRetry(RetryContext context) throws Throwable {
|
||||
attempts++;
|
||||
if (attempts < 2) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
return "bar";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -94,7 +94,6 @@ public class ExternalRetryPolicyTests extends TestCase {
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// Expected
|
||||
System.err.println(e.getMessage());
|
||||
assertTrue(Pattern.matches(".*not.*Throwable.*", e.getMessage()));
|
||||
}
|
||||
|
||||
@@ -192,7 +191,7 @@ public class ExternalRetryPolicyTests extends TestCase {
|
||||
return attempts < retryLimit;
|
||||
}
|
||||
|
||||
public void close(RetryContext context) {
|
||||
public void close(RetryContext context, boolean succeeded) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ public class NeverRetryPolicyTests extends TestCase {
|
||||
assertTrue(policy.canRetry(context));
|
||||
policy.registerThrowable(context, null);
|
||||
assertFalse(policy.canRetry(context));
|
||||
policy.close(context);
|
||||
policy.close(context, true);
|
||||
assertFalse(policy.canRetry(context));
|
||||
}
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
|
||||
assertNotNull(context);
|
||||
policy.registerThrowable(context, new Exception());
|
||||
assertFalse(policy.canRetry(context));
|
||||
policy.close(context);
|
||||
policy.close(context, true);
|
||||
// still can't retry, even if policy is closed
|
||||
// (not that this would happen in practice)...
|
||||
assertFalse(policy.canRetry(context));
|
||||
@@ -129,14 +129,14 @@ public class RecoveryRetryPolicyTests extends TestCase {
|
||||
assertNotNull(context);
|
||||
policy.registerThrowable(context, new Exception());
|
||||
assertTrue(policy.canRetry(context));
|
||||
policy.close(context);
|
||||
policy.close(context, false);
|
||||
|
||||
// Second call...
|
||||
context = policy.open(callback, null);
|
||||
assertNotNull(context);
|
||||
policy.registerThrowable(context, new Exception());
|
||||
assertFalse(policy.canRetry(context));
|
||||
policy.close(context);
|
||||
policy.close(context, true);
|
||||
|
||||
}
|
||||
|
||||
@@ -229,7 +229,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
|
||||
|
||||
// False before close...
|
||||
assertFalse(policy.canRetry(context));
|
||||
policy.close(context);
|
||||
policy.close(context, true);
|
||||
Object result = policy.handleRetryExhausted(context);
|
||||
assertNull(result); // default result is null
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ public class TimeoutRetryPolicyTests extends TestCase {
|
||||
assertTrue(policy.canRetry(context));
|
||||
Thread.sleep(200);
|
||||
assertFalse(policy.canRetry(context));
|
||||
policy.close(context);
|
||||
policy.close(context, true);
|
||||
}
|
||||
|
||||
public void testRetryCount() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user