From 0e02b8939c452a7cccbcef7e4332abdfba1564e1 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Wed, 21 Nov 2007 17:28:41 +0000 Subject: [PATCH] Added tests for interruption of timeout methods. --- .../channel/PointToPointChannelTests.java | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/spring-eai-core/src/test/java/org/springframework/integration/channel/PointToPointChannelTests.java b/spring-eai-core/src/test/java/org/springframework/integration/channel/PointToPointChannelTests.java index f561c5530b..18697b76bf 100644 --- a/spring-eai-core/src/test/java/org/springframework/integration/channel/PointToPointChannelTests.java +++ b/spring-eai-core/src/test/java/org/springframework/integration/channel/PointToPointChannelTests.java @@ -94,7 +94,7 @@ public class PointToPointChannelTests { } @Test - public void testBlockingReceive() throws Exception{ + public void testBlockingReceiveWithNoTimeout() throws Exception{ final PointToPointChannel channel = new PointToPointChannel(); final AtomicBoolean receiveInterrupted = new AtomicBoolean(false); final CountDownLatch latch = new CountDownLatch(1); @@ -113,6 +113,26 @@ public class PointToPointChannelTests { assertTrue(receiveInterrupted.get()); } + @Test + public void testBlockingReceiveWithTimeout() throws Exception{ + final PointToPointChannel channel = new PointToPointChannel(); + final AtomicBoolean receiveInterrupted = new AtomicBoolean(false); + final CountDownLatch latch = new CountDownLatch(1); + Thread t = new Thread(new Runnable() { + public void run() { + Message message = channel.receive(10000); + receiveInterrupted.set(true); + assertTrue(message == null); + latch.countDown(); + } + }); + t.start(); + assertFalse(receiveInterrupted.get()); + t.interrupt(); + latch.await(); + assertTrue(receiveInterrupted.get()); + } + @Test public void testImmediateSend() { PointToPointChannel channel = new PointToPointChannel(3); @@ -127,7 +147,7 @@ public class PointToPointChannelTests { } @Test - public void testBlockingSend() throws Exception{ + public void testBlockingSendWithNoTimeout() throws Exception{ final PointToPointChannel channel = new PointToPointChannel(1); boolean result1 = channel.send(new DocumentMessage(1, "test-1")); assertTrue(result1); @@ -147,4 +167,25 @@ public class PointToPointChannelTests { assertTrue(sendInterrupted.get()); } + @Test + public void testBlockingSendWithTimeout() throws Exception{ + final PointToPointChannel channel = new PointToPointChannel(1); + boolean result1 = channel.send(new DocumentMessage(1, "test-1")); + assertTrue(result1); + final AtomicBoolean sendInterrupted = new AtomicBoolean(false); + final CountDownLatch latch = new CountDownLatch(1); + Thread t = new Thread(new Runnable() { + public void run() { + channel.send(new DocumentMessage(2, "test-2"), 10000); + sendInterrupted.set(true); + latch.countDown(); + } + }); + t.start(); + assertFalse(sendInterrupted.get()); + t.interrupt(); + latch.await(); + assertTrue(sendInterrupted.get()); + } + }