From e600243ea8e53ae7ef3b283113b7ca7b33a53523 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 3 Sep 2010 00:13:03 +0000 Subject: [PATCH] INT-1411 added test for cancellation --- .../core/AsyncMessagingTemplateTests.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/core/AsyncMessagingTemplateTests.java b/spring-integration-core/src/test/java/org/springframework/integration/core/AsyncMessagingTemplateTests.java index 00ebfef296..ed0d3a3f8f 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/core/AsyncMessagingTemplateTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/core/AsyncMessagingTemplateTests.java @@ -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> 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(); }