Improve robustness of retry tests

This commit is contained in:
dsyer
2008-12-02 19:26:08 +00:00
parent 0419b2139c
commit b168607d46
5 changed files with 69 additions and 29 deletions

View File

@@ -33,6 +33,10 @@ public class RepeatTransactionalPollingIntegrationTests implements ApplicationCo
private List<String> processed = new ArrayList<String>();
private List<String> expected;
private List<String> handled = new ArrayList<String>();
private List<String> list = new ArrayList<String>();
private Lifecycle bus;
@@ -47,7 +51,10 @@ public class RepeatTransactionalPollingIntegrationTests implements ApplicationCo
public String process(String message) {
String result = message + ": " + count;
logger.debug("Handling: " + message);
processed.add(message);
if (count<expected.size()) {
processed.add(message);
count++;
}
if ("fail".equals(message)) {
throw new RuntimeException("Planned failure");
}
@@ -66,7 +73,7 @@ public class RepeatTransactionalPollingIntegrationTests implements ApplicationCo
@ChannelAdapter("replies")
public void output(String message) {
count++;
handled.add(message);
logger.debug("Handled: " + message);
}
@@ -75,9 +82,10 @@ public class RepeatTransactionalPollingIntegrationTests implements ApplicationCo
public void testSunnyDay() throws Exception {
list = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,c,d,e,f,g,h,j,k")));
waitForResults(bus, 4, 60);
assertEquals(4,processed.size()); // a,b,c,d
assertEquals(4,count);
expected = Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,c,d"));
waitForResults(bus, expected.size(), 60);
assertEquals(expected,processed);
}
@Test
@@ -85,9 +93,11 @@ public class RepeatTransactionalPollingIntegrationTests implements ApplicationCo
public void testRollback() throws Exception {
list = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,fail,d,e,f,g,h,j,k")));
waitForResults(bus, 4, 30); // (a,b), (fail), (fail)
assertEquals(4,processed.size()); // a,b,fail,fail
assertEquals(2,count); // a,b
expected = Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,fail,fail"));
waitForResults(bus, expected.size(), 60);
assertEquals(expected,processed);
assertEquals(2, handled.size()); // a,b
}
private void waitForResults(Lifecycle lifecycle, int count, int maxTries) throws InterruptedException {

View File

@@ -68,9 +68,12 @@ public class RetryRepeatTransactionalPollingIntegrationTests implements Applicat
public void testSunnyDay() throws Exception {
list = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,c,d,e,f,g,h,j,k")));
waitForResults(bus, 4, 60);
List<String> expected = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,c,d")));
service.setExpected(expected);
waitForResults(bus, expected.size(), 60);
assertEquals(4,service.getProcessed().size()); // a,b,c,d
assertEquals(4,count);
assertEquals(expected, service.getProcessed());
}
@Test
@@ -78,12 +81,14 @@ public class RetryRepeatTransactionalPollingIntegrationTests implements Applicat
public void testRollback() throws Exception {
list = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,fail,d,e,f,g,h,j,k")));
List<String> expected = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,fail,fail,d,e,f")));
service.setExpected(expected);
waitForResults(bus, expected.size(), 60);
waitForResults(bus, 6, 100); // (a,b), (fail), (fail), ([fail],d), (e,f)
System.err.println(service.getProcessed());
System.err.println(recoverer.getRecovered());
assertEquals(7,service.getProcessed().size()); // a,b,fail,fail,d,e,f
assertEquals(1,recoverer.getRecovered().size()); // fail
assertEquals(5,count); // a,b,d,e,f
assertEquals(expected, service.getProcessed());
}
private void waitForResults(Lifecycle lifecycle, int count, int maxTries) throws InterruptedException {

View File

@@ -46,7 +46,7 @@ public class RetryTransactionalPollingIntegrationTests implements ApplicationCon
}
private volatile int count = 0;
@ChannelAdapter("requests")
@Poller(interval=10, transactionManager="transactionManager")
public String input() {
@@ -68,9 +68,12 @@ public class RetryTransactionalPollingIntegrationTests implements ApplicationCon
public void testSunnyDay() throws Exception {
list = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,c,d,e,f,g,h,j,k")));
waitForResults(bus, 4, 60);
List<String> expected = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,c,d")));
service.setExpected(expected);
waitForResults(bus, expected.size(), 60);
assertEquals(4,service.getProcessed().size()); // a,b,c,d
assertEquals(4,count);
assertEquals(expected, service.getProcessed());
}
@Test
@@ -78,12 +81,15 @@ public class RetryTransactionalPollingIntegrationTests implements ApplicationCon
public void testRollback() throws Exception {
list = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,fail,d,e,f,g,h,j,k")));
waitForResults(bus, 6, 200); // (a), (b), (fail), (fail), ...
System.err.println(service.getProcessed());
System.err.println(recoverer.getRecovered());
List<String> expected = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,fail,fail,d,e")));
service.setExpected(expected);
waitForResults(bus, expected.size(), 60);
waitForResults(bus, 6, 100); // (a,b), (fail), (fail), ([fail],d), (e,f)
assertEquals(6,service.getProcessed().size()); // a,b,fail,fail,d,e
assertEquals(1,recoverer.getRecovered().size()); // fail
assertEquals(4,count); // a,b,d,e
assertEquals(expected, service.getProcessed());
}
private void waitForResults(Lifecycle lifecycle, int count, int maxTries) throws InterruptedException {

View File

@@ -1,5 +1,6 @@
package org.springframework.batch.integration.retry;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -9,14 +10,20 @@ import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
@MessageEndpoint
public class SimpleService implements Service {
public class SimpleService implements Service {
private Log logger = LogFactory.getLog(getClass());
private List<String> processed = new CopyOnWriteArrayList<String>();
private List<String> expected = new ArrayList<String>();
private int count = 0;
public void setExpected(List<String> expected) {
this.expected = expected;
}
/**
* Public getter for the processed.
* @return the processed
@@ -29,7 +36,9 @@ public class SimpleService implements Service {
public String process(String message) {
String result = message + ": " + (count++);
logger.debug("Handling: " + message);
processed.add(message);
if (count <= expected.size()) {
processed.add(message);
}
if ("fail".equals(message)) {
throw new RuntimeException("Planned failure");
}

View File

@@ -33,6 +33,10 @@ public class TransactionalPollingIntegrationTests implements ApplicationContextA
private List<String> processed = new ArrayList<String>();
private List<String> handled = new ArrayList<String>();
private List<String> expected;
private List<String> list = new ArrayList<String>();
private Lifecycle bus;
@@ -47,7 +51,10 @@ public class TransactionalPollingIntegrationTests implements ApplicationContextA
public String process(String message) {
String result = message + ": " + count;
logger.debug("Handling: " + message);
processed.add(message);
if (count<expected.size()) {
processed.add(message);
count++;
}
if ("fail".equals(message)) {
throw new RuntimeException("Planned failure");
}
@@ -66,7 +73,7 @@ public class TransactionalPollingIntegrationTests implements ApplicationContextA
@ChannelAdapter("replies")
public void output(String message) {
count++;
handled.add(message);
logger.debug("Handled: " + message);
}
@@ -75,19 +82,22 @@ public class TransactionalPollingIntegrationTests implements ApplicationContextA
public void testSunnyDay() throws Exception {
list = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,c,d,e,f,g,h,j,k")));
expected = Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,c,d"));
waitForResults(bus, 4, 60);
assertEquals(4,count);
assertEquals(expected,processed);
}
@Test
@DirtiesContext
public void testRollback() throws Exception {
// when @Poller accepts transactional=@Transactional(propagation=Propagation.REQUIRED)...
list = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,fail,d,e,f,g,h,j,k")));
expected = Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,fail,fail"));
waitForResults(bus, 4, 30);
System.err.println(processed);
assertEquals(2,count);
assertEquals(expected,processed);
assertEquals(2, handled.size()); // a,b
}
private void waitForResults(Lifecycle lifecycle, int count, int maxTries) throws InterruptedException {