INT-1411 added test for cancellation

This commit is contained in:
Mark Fisher
2010-09-03 00:13:03 +00:00
parent c8b9c618d5
commit e600243ea8

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@@ -34,6 +35,7 @@ import org.springframework.integration.MessagingException;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.util.Assert;
/**
* @author Mark Fisher
@@ -154,6 +156,26 @@ public class AsyncMessagingTemplateTests {
}
}
@Test(expected = CancellationException.class)
public void cancellationException() throws Throwable {
DirectChannel channel = new DirectChannel();
EchoHandler handler = new EchoHandler(10000);
channel.subscribe(handler);
AsyncMessagingTemplate template = new AsyncMessagingTemplate();
template.setDefaultChannel(channel);
Future<Message<?>> result = template.asyncSendAndReceive(MessageBuilder.withPayload("test").build());
try {
Thread.sleep(200);
result.cancel(true);
result.get();
fail();
}
catch (ExecutionException e) {
Assert.isTrue(handler.interrupted, "handler should have been interrupted");
throw e.getCause();
}
}
private static class EchoHandler extends AbstractReplyProducingMessageHandler {
@@ -161,6 +183,8 @@ public class AsyncMessagingTemplateTests {
private final boolean shouldFail;
private volatile boolean interrupted;
private EchoHandler(long delay) {
this.delay = delay;
this.shouldFail = (this.delay < 0);
@@ -175,7 +199,9 @@ public class AsyncMessagingTemplateTests {
Thread.sleep(this.delay);
}
catch (InterruptedException e) {
// ignore
Thread.currentThread().interrupt();
this.interrupted = true;
return null;
}
return requestMessage.getPayload().toString().toUpperCase();
}