blob: e7fd638d6af2a32987aea58e165246feed75914d [file] [log] [blame]
Lukacs Berki83e7c712016-10-05 15:56:22 +00001// 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.
14package com.google.devtools.build.lib.runtime;
15
16import static com.google.common.truth.Truth.assertThat;
michajlo660d17f2020-03-27 09:01:57 -070017import static org.junit.Assert.assertThrows;
Lukacs Berki83e7c712016-10-05 15:56:22 +000018
19import com.google.common.base.Strings;
20import com.google.devtools.build.lib.testutil.Suite;
21import com.google.devtools.build.lib.testutil.TestSpec;
22import java.io.IOException;
23import java.io.OutputStream;
24import java.nio.charset.StandardCharsets;
25import java.util.ArrayList;
26import java.util.List;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.JUnit4;
30
31/**
32 * Unit tests for {@link LineBufferedOutputStream} .
33 */
34@TestSpec(size = Suite.SMALL_TESTS)
35@RunWith(JUnit4.class)
36public class LineBufferedOutputStreamTest {
37 private static class MockOutputStream extends OutputStream {
38 private final List<String> writes = new ArrayList<>();
Lukacs Berki480a2cf2016-11-21 12:37:46 +000039 private boolean throwException = false;
Lukacs Berki83e7c712016-10-05 15:56:22 +000040
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 Berki480a2cf2016-11-21 12:37:46 +000050 if (throwException) {
51 throwException = false;
52 throw new IOException("thrown");
53 }
Lukacs Berki83e7c712016-10-05 15:56:22 +000054 }
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 Berki480a2cf2016-11-21 12:37:46 +000082 }
Lukacs Berki83e7c712016-10-05 15:56:22 +000083
Lukacs Berki480a2cf2016-11-21 12:37:46 +000084 @Test
85 public void testIOErrorOnWrappedStream() throws Exception {
86 MockOutputStream mos = new MockOutputStream();
87 LineBufferedOutputStream cut = new LineBufferedOutputStream(mos, 4);
88 mos.throwException = true;
jcaterfb5ed302019-04-29 13:30:34 -070089 assertThrows(IOException.class, () -> cut.write("aaaa".getBytes(StandardCharsets.UTF_8)));
Lukacs Berki480a2cf2016-11-21 12:37:46 +000090 cut.write("a".getBytes(StandardCharsets.UTF_8));
91 cut.close();
92 assertThat(mos.writes).containsExactly("aaaa", "a");
Lukacs Berki83e7c712016-10-05 15:56:22 +000093 }
94}