Added tests for interruption of timeout methods.

This commit is contained in:
Mark Fisher
2007-11-21 17:28:41 +00:00
parent 98e6915da7
commit 0e02b8939c

View File

@@ -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());
}
}