diff options
author | 2016-11-21 12:37:46 +0000 | |
---|---|---|
committer | 2016-11-21 19:40:54 +0000 | |
commit | 480a2cf4d3684ad9171077a279f1743192096d7b (patch) | |
tree | 46c080bdc320ecf6166a800e9602a8c9f1b32bde /src/test/java/com/google/devtools/build | |
parent | af25a98a7af734926f6d29187818ce4140b4f3e8 (diff) |
Make LineBufferedOutputStream resistant to exceptions thrown while flushing its buffer.
--
MOS_MIGRATED_REVID=139771073
Diffstat (limited to 'src/test/java/com/google/devtools/build')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/runtime/LineBufferedOutputStreamTest.java | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/runtime/LineBufferedOutputStreamTest.java b/src/test/java/com/google/devtools/build/lib/runtime/LineBufferedOutputStreamTest.java index bc136b41fb..8cf7c155d7 100644 --- a/src/test/java/com/google/devtools/build/lib/runtime/LineBufferedOutputStreamTest.java +++ b/src/test/java/com/google/devtools/build/lib/runtime/LineBufferedOutputStreamTest.java @@ -14,6 +14,7 @@ package com.google.devtools.build.lib.runtime; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; import com.google.common.base.Strings; import com.google.devtools.build.lib.testutil.Suite; @@ -35,6 +36,7 @@ import org.junit.runners.JUnit4; public class LineBufferedOutputStreamTest { private static class MockOutputStream extends OutputStream { private final List<String> writes = new ArrayList<>(); + private boolean throwException = false; @Override public void write(int byteAsInt) throws IOException { @@ -45,6 +47,10 @@ public class LineBufferedOutputStreamTest { @Override public synchronized void write(byte[] b, int off, int inlen) throws IOException { writes.add(new String(b, off, inlen, StandardCharsets.UTF_8)); + if (throwException) { + throwException = false; + throw new IOException("thrown"); + } } } @@ -73,6 +79,21 @@ public class LineBufferedOutputStreamTest { assertThat(lineBuffer("a", "a", large, large, "a")).containsExactly( "aa", large, large, "a"); + } + @Test + public void testIOErrorOnWrappedStream() throws Exception { + MockOutputStream mos = new MockOutputStream(); + LineBufferedOutputStream cut = new LineBufferedOutputStream(mos, 4); + mos.throwException = true; + try { + cut.write("aaaa".getBytes(StandardCharsets.UTF_8)); + fail("IOException expected"); + } catch (IOException e) { + // Expected. + } + cut.write("a".getBytes(StandardCharsets.UTF_8)); + cut.close(); + assertThat(mos.writes).containsExactly("aaaa", "a"); } } |