summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/java/io/InterruptedStreamTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'luni/src/test/java/libcore/java/io/InterruptedStreamTest.java')
-rwxr-xr-x[-rw-r--r--]luni/src/test/java/libcore/java/io/InterruptedStreamTest.java58
1 files changed, 48 insertions, 10 deletions
diff --git a/luni/src/test/java/libcore/java/io/InterruptedStreamTest.java b/luni/src/test/java/libcore/java/io/InterruptedStreamTest.java
index e973b8f..e46df5d 100644..100755
--- a/luni/src/test/java/libcore/java/io/InterruptedStreamTest.java
+++ b/luni/src/test/java/libcore/java/io/InterruptedStreamTest.java
@@ -28,6 +28,7 @@ import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedByInterruptException;
+import java.nio.channels.ClosedChannelException;
import java.nio.channels.Pipe;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.ServerSocketChannel;
@@ -45,10 +46,16 @@ public final class InterruptedStreamTest extends TestCase {
private Socket[] sockets;
+ @Override protected void setUp() throws Exception {
+ Thread.interrupted(); // clear interrupted bit
+ super.tearDown();
+ }
+
@Override protected void tearDown() throws Exception {
if (sockets != null) {
sockets[0].close();
sockets[1].close();
+ sockets = null;
}
Thread.interrupted(); // clear interrupted bit
super.tearDown();
@@ -93,7 +100,7 @@ public final class InterruptedStreamTest extends TestCase {
public void testInterruptWritableSocketChannel() throws Exception {
sockets = newSocketChannelPair();
- testInterruptReadableChannel(sockets[0].getChannel());
+ testInterruptWritableChannel(sockets[0].getChannel());
}
/**
@@ -110,71 +117,102 @@ public final class InterruptedStreamTest extends TestCase {
}
private void testInterruptInputStream(final InputStream in) throws Exception {
- interruptMeLater();
+ Thread thread = interruptMeLater();
try {
in.read();
fail();
} catch (InterruptedIOException expected) {
+ } finally {
+ waitForInterrupt(thread);
}
}
private void testInterruptReader(final PipedReader reader) throws Exception {
- interruptMeLater();
+ Thread thread = interruptMeLater();
try {
reader.read();
fail();
} catch (InterruptedIOException expected) {
+ } finally {
+ waitForInterrupt(thread);
}
}
private void testInterruptReadableChannel(final ReadableByteChannel channel) throws Exception {
- interruptMeLater();
+ Thread thread = interruptMeLater();
try {
channel.read(ByteBuffer.allocate(BUFFER_SIZE));
fail();
} catch (ClosedByInterruptException expected) {
+ } finally {
+ waitForInterrupt(thread);
}
}
private void testInterruptOutputStream(final OutputStream out) throws Exception {
- interruptMeLater();
+ Thread thread = interruptMeLater();
try {
// this will block when the receiving buffer fills up
while (true) {
out.write(new byte[BUFFER_SIZE]);
}
} catch (InterruptedIOException expected) {
+ } finally {
+ waitForInterrupt(thread);
}
}
private void testInterruptWriter(final PipedWriter writer) throws Exception {
- interruptMeLater();
+ Thread thread = interruptMeLater();
try {
// this will block when the receiving buffer fills up
while (true) {
writer.write(new char[BUFFER_SIZE]);
}
} catch (InterruptedIOException expected) {
+ } finally {
+ waitForInterrupt(thread);
}
}
private void testInterruptWritableChannel(final WritableByteChannel channel) throws Exception {
- interruptMeLater();
+ Thread thread = interruptMeLater();
try {
// this will block when the receiving buffer fills up
while (true) {
channel.write(ByteBuffer.allocate(BUFFER_SIZE));
}
} catch (ClosedByInterruptException expected) {
+ } catch (ClosedChannelException expected) {
+ } finally {
+ waitForInterrupt(thread);
}
}
- private void interruptMeLater() throws Exception {
+ private Thread interruptMeLater() throws Exception {
final Thread toInterrupt = Thread.currentThread();
- new Thread(new Runnable () {
+ Thread thread = new Thread(new Runnable () {
@Override public void run() {
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException ex) {
+ }
toInterrupt.interrupt();
}
- }).start();
+ });
+ thread.start();
+ return thread;
+ }
+
+ private static void waitForInterrupt(Thread thread) throws Exception {
+ try {
+ thread.join();
+ } catch (InterruptedException ignore) {
+ // There is currently a race between Thread.interrupt in
+ // interruptMeLater and Thread.join here. Most of the time
+ // we won't get an InterruptedException, but occasionally
+ // we do, so for now ignore this exception.
+ // http://b/6951157
+ }
}
}