Lukacs Berki | 83e7c71 | 2016-10-05 15:56:22 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.runtime; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 17 | import static org.junit.Assert.assertThrows; |
Lukacs Berki | 83e7c71 | 2016-10-05 15:56:22 +0000 | [diff] [blame] | 18 | |
| 19 | import com.google.common.base.Strings; |
| 20 | import com.google.devtools.build.lib.testutil.Suite; |
| 21 | import com.google.devtools.build.lib.testutil.TestSpec; |
| 22 | import java.io.IOException; |
| 23 | import java.io.OutputStream; |
| 24 | import java.nio.charset.StandardCharsets; |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.junit.runners.JUnit4; |
| 30 | |
| 31 | /** |
| 32 | * Unit tests for {@link LineBufferedOutputStream} . |
| 33 | */ |
| 34 | @TestSpec(size = Suite.SMALL_TESTS) |
| 35 | @RunWith(JUnit4.class) |
| 36 | public class LineBufferedOutputStreamTest { |
| 37 | private static class MockOutputStream extends OutputStream { |
| 38 | private final List<String> writes = new ArrayList<>(); |
Lukacs Berki | 480a2cf | 2016-11-21 12:37:46 +0000 | [diff] [blame] | 39 | private boolean throwException = false; |
Lukacs Berki | 83e7c71 | 2016-10-05 15:56:22 +0000 | [diff] [blame] | 40 | |
| 41 | @Override |
| 42 | public void write(int byteAsInt) throws IOException { |
| 43 | byte b = (byte) byteAsInt; // make sure we work with bytes in comparisons |
| 44 | write(new byte[] {b}, 0, 1); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public synchronized void write(byte[] b, int off, int inlen) throws IOException { |
| 49 | writes.add(new String(b, off, inlen, StandardCharsets.UTF_8)); |
Lukacs Berki | 480a2cf | 2016-11-21 12:37:46 +0000 | [diff] [blame] | 50 | if (throwException) { |
| 51 | throwException = false; |
| 52 | throw new IOException("thrown"); |
| 53 | } |
Lukacs Berki | 83e7c71 | 2016-10-05 15:56:22 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | private List<String> lineBuffer(String... inputs) throws Exception { |
| 58 | MockOutputStream mockOutputStream = new MockOutputStream(); |
| 59 | try (LineBufferedOutputStream cut = new LineBufferedOutputStream(mockOutputStream, 6)) { |
| 60 | for (String input : inputs) { |
| 61 | cut.write(input.getBytes(StandardCharsets.UTF_8)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return mockOutputStream.writes; |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testLineBuffering() throws Exception { |
| 70 | String large = Strings.repeat("a", 100); |
| 71 | |
| 72 | assertThat(lineBuffer("foo\nbar")).containsExactly("foo\n", "bar"); |
| 73 | assertThat(lineBuffer("foobarfoobar")).containsExactly("foobar", "foobar"); |
| 74 | assertThat(lineBuffer("fivey\none\n")).containsExactly("fivey\n", "one\n"); |
| 75 | assertThat(lineBuffer("sixish\none\n")).containsExactly("sixish", "\n", "one\n"); |
| 76 | assertThat(lineBuffer("s")).containsExactly("s"); |
| 77 | assertThat(lineBuffer("\n\n\n\n")).containsExactly("\n", "\n", "\n", "\n"); |
| 78 | assertThat(lineBuffer("foo\n\nbar\n")).containsExactly("foo\n", "\n", "bar\n"); |
| 79 | |
| 80 | assertThat(lineBuffer("a", "a", large, large, "a")).containsExactly( |
| 81 | "aa", large, large, "a"); |
Lukacs Berki | 480a2cf | 2016-11-21 12:37:46 +0000 | [diff] [blame] | 82 | } |
Lukacs Berki | 83e7c71 | 2016-10-05 15:56:22 +0000 | [diff] [blame] | 83 | |
Lukacs Berki | 480a2cf | 2016-11-21 12:37:46 +0000 | [diff] [blame] | 84 | @Test |
| 85 | public void testIOErrorOnWrappedStream() throws Exception { |
| 86 | MockOutputStream mos = new MockOutputStream(); |
| 87 | LineBufferedOutputStream cut = new LineBufferedOutputStream(mos, 4); |
| 88 | mos.throwException = true; |
jcater | fb5ed30 | 2019-04-29 13:30:34 -0700 | [diff] [blame] | 89 | assertThrows(IOException.class, () -> cut.write("aaaa".getBytes(StandardCharsets.UTF_8))); |
Lukacs Berki | 480a2cf | 2016-11-21 12:37:46 +0000 | [diff] [blame] | 90 | cut.write("a".getBytes(StandardCharsets.UTF_8)); |
| 91 | cut.close(); |
| 92 | assertThat(mos.writes).containsExactly("aaaa", "a"); |
Lukacs Berki | 83e7c71 | 2016-10-05 15:56:22 +0000 | [diff] [blame] | 93 | } |
| 94 | } |