diff options
author | Brian Carlstrom <bdc@google.com> | 2012-10-16 10:04:38 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-10-16 10:04:39 -0700 |
commit | 45b4903de9cb4fbe25e3ec05bbc307c2c1aa34e3 (patch) | |
tree | 0bdf23c4b40a20babb9d807c80df36b6946b9295 | |
parent | 105068e3eb8dd7e38f3719ac37035a3e567006d4 (diff) | |
parent | 243111f8533820ea8d27bde6317f88f610aa5867 (diff) | |
download | libcore-45b4903de9cb4fbe25e3ec05bbc307c2c1aa34e3.zip libcore-45b4903de9cb4fbe25e3ec05bbc307c2c1aa34e3.tar.gz libcore-45b4903de9cb4fbe25e3ec05bbc307c2c1aa34e3.tar.bz2 |
Merge "Fix flakiness with InterruptedStreamTest DO NOT MERGE" into jb-mr1-dev
-rwxr-xr-x | luni/src/test/java/libcore/java/io/InterruptedStreamTest.java | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/luni/src/test/java/libcore/java/io/InterruptedStreamTest.java b/luni/src/test/java/libcore/java/io/InterruptedStreamTest.java index e1f51bd..e46df5d 100755 --- a/luni/src/test/java/libcore/java/io/InterruptedStreamTest.java +++ b/luni/src/test/java/libcore/java/io/InterruptedStreamTest.java @@ -46,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(); @@ -111,56 +117,66 @@ 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) { @@ -168,12 +184,14 @@ public final class InterruptedStreamTest extends TestCase { } } 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); @@ -181,6 +199,20 @@ public final class InterruptedStreamTest extends TestCase { } 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 + } } } |