aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/util/io/StreamMultiplexerTest.java
diff options
context:
space:
mode:
authorGravatar Lukacs Berki <lberki@google.com>2016-03-22 08:02:18 +0000
committerGravatar Lukacs Berki <lberki@google.com>2016-03-22 08:09:59 +0000
commit4dd60f9cef8a206bac8f83af8153bb8e335479bc (patch)
tree4d737bef42bfb20f707bb5362b6bbdb41d30f2f4 /src/test/java/com/google/devtools/build/lib/util/io/StreamMultiplexerTest.java
parentb4c00b6eead53ba9381bab12765b2c4ed98a61d1 (diff)
Revamp the client/server communication protocol so that it is portable to Windows.
Progress towards #930. -- MOS_MIGRATED_REVID=117799006
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/util/io/StreamMultiplexerTest.java')
-rw-r--r--src/test/java/com/google/devtools/build/lib/util/io/StreamMultiplexerTest.java61
1 files changed, 33 insertions, 28 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/util/io/StreamMultiplexerTest.java b/src/test/java/com/google/devtools/build/lib/util/io/StreamMultiplexerTest.java
index 26664a155d..6d3f2c3292 100644
--- a/src/test/java/com/google/devtools/build/lib/util/io/StreamMultiplexerTest.java
+++ b/src/test/java/com/google/devtools/build/lib/util/io/StreamMultiplexerTest.java
@@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.util.io;
-import static com.google.devtools.build.lib.util.StringUtilities.joinLines;
+
+import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
@@ -27,6 +28,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
/**
* Test for {@link StreamMultiplexer}.
@@ -61,42 +63,41 @@ public class StreamMultiplexerTest {
return string.getBytes("ISO-8859-1");
}
- private static String getLatin(byte[] bytes)
- throws UnsupportedEncodingException {
- return new String(bytes, "ISO-8859-1");
- }
-
@Test
- public void testHelloWorldOnStdOut() throws IOException {
+ public void testHelloWorldOnStdOut() throws Exception {
out.write(getLatin("Hello, world."));
out.flush();
- assertEquals(joinLines("@1@", "Hello, world.", ""),
- getLatin(multiplexed.toByteArray()));
+ assertMessage(multiplexed.toByteArray(), 0, "Hello, world.");
}
@Test
public void testInterleavedStdoutStderrControl() throws Exception {
+ int start = 0;
out.write(getLatin("Hello, stdout."));
out.flush();
+ assertMessage(multiplexed.toByteArray(), start, "Hello, stdout.");
+ start = multiplexed.toByteArray().length;
+
err.write(getLatin("Hello, stderr."));
err.flush();
+ assertMessage(multiplexed.toByteArray(), start, "Hello, stderr.");
+ start = multiplexed.toByteArray().length;
+
ctl.write(getLatin("Hello, control."));
ctl.flush();
+ assertMessage(multiplexed.toByteArray(), start, "Hello, control.");
+ start = multiplexed.toByteArray().length;
+
out.write(getLatin("... and back!"));
out.flush();
- assertEquals(joinLines("@1@", "Hello, stdout.",
- "@2@", "Hello, stderr.",
- "@3@", "Hello, control.",
- "@1@", "... and back!",
- ""),
- getLatin(multiplexed.toByteArray()));
+ assertMessage(multiplexed.toByteArray(), start, "... and back!");
}
@Test
public void testWillNotCommitToUnderlyingStreamUnlessFlushOrNewline()
throws Exception {
out.write(getLatin("There are no newline characters in here, so it won't" +
- " get written just yet."));
+ " get written just yet."));
assertArrayEquals(multiplexed.toByteArray(), new byte[0]);
}
@@ -105,16 +106,11 @@ public class StreamMultiplexerTest {
out.write(getLatin("No newline just yet, so no flushing. "));
assertArrayEquals(multiplexed.toByteArray(), new byte[0]);
out.write(getLatin("OK, here we go:\nAnd more to come."));
-
- String expected = joinLines("@1",
- "No newline just yet, so no flushing. OK, here we go:", "");
-
- assertEquals(expected, getLatin(multiplexed.toByteArray()));
-
+ assertMessage(
+ multiplexed.toByteArray(), 0, "No newline just yet, so no flushing. OK, here we go:\n");
+ int firstMessageLength = multiplexed.toByteArray().length;
out.write((byte) '\n');
- expected += joinLines("@1", "And more to come.", "");
-
- assertEquals(expected, getLatin(multiplexed.toByteArray()));
+ assertMessage(multiplexed.toByteArray(), firstMessageLength, "And more to come.\n");
}
@Test
@@ -122,14 +118,14 @@ public class StreamMultiplexerTest {
out.write(getLatin("Don't forget to flush!"));
assertArrayEquals(new byte[0], multiplexed.toByteArray());
out.flush(); // now the output will appear in multiplexed.
- assertEquals(joinLines("@1@", "Don't forget to flush!", ""),
- getLatin(multiplexed.toByteArray()));
+ assertStartsWith(multiplexed.toByteArray(), 1, 0, 0, 0);
+ assertMessage(multiplexed.toByteArray(), 0, "Don't forget to flush!");
}
@Test
public void testByteEncoding() throws IOException {
OutputStream devNull = ByteStreams.nullOutputStream();
- StreamDemultiplexer demux = new StreamDemultiplexer((byte) '1', devNull);
+ StreamDemultiplexer demux = new StreamDemultiplexer((byte) 1, devNull);
StreamMultiplexer mux = new StreamMultiplexer(demux);
OutputStream out = mux.createStdout();
@@ -145,4 +141,13 @@ public class StreamMultiplexerTest {
out.write(10);
}
+ private static void assertStartsWith(byte[] actual, int... expectedPrefix){
+ for (int i = 0; i < expectedPrefix.length; i++) {
+ assertThat(actual[i]).isEqualTo(expectedPrefix[i]);
+ }
+ }
+
+ private static void assertMessage(byte[] actual, int start, String expected) throws Exception {
+ assertThat(Arrays.copyOfRange(actual, start + 5, actual.length)).isEqualTo(getLatin(expected));
+ }
}