From f2d459502f5fb422d6000db782795cffc6efa3e4 Mon Sep 17 00:00:00 2001 From: ulfjack Date: Wed, 9 Aug 2017 15:27:49 +0200 Subject: Rewrite the Command API Important: the simplified API now defaults to forwarding interrupts to subprocesses. I did audit all the call sites, and I think this is a safe change to make. - Properly support timeouts with all implementations - Simplify the API - only provide two flavours of blocking calls, which require no input and forward interrupts; this is the most common usage - provide a number of async calls, which optionally takes input, and a flag whether to forward interrupts - only support input streams, no byte arrays or other 'convenience features' that are rarely needed and unnecessarily increase the surface area - use java.time.Duration to specify timeout; for consistency, interpret a timeout of <= 0 as no timeout (i.e., including rather than excluding 0) - KillableObserver and subclasses are no longer part of the public API, but still used to implement timeouts if the Subprocess.Factory does not support them - Update the documentation for Command - Update all callers; most callers now use the simplified API PiperOrigin-RevId: 164716782 --- .../build/lib/shell/CommandLargeInputsTest.java | 55 ++++++++++++---------- 1 file changed, 30 insertions(+), 25 deletions(-) (limited to 'src/test/java/com/google/devtools/build/lib/shell/CommandLargeInputsTest.java') diff --git a/src/test/java/com/google/devtools/build/lib/shell/CommandLargeInputsTest.java b/src/test/java/com/google/devtools/build/lib/shell/CommandLargeInputsTest.java index f62e01b71d..9884be2adb 100644 --- a/src/test/java/com/google/devtools/build/lib/shell/CommandLargeInputsTest.java +++ b/src/test/java/com/google/devtools/build/lib/shell/CommandLargeInputsTest.java @@ -57,54 +57,57 @@ public class CommandLargeInputsTest { } @Test - public void testCatRandomBinary() throws Exception { + public void testCatRandomBinaryToOutputStream() throws Exception { final Command command = new Command(new String[] {"cat"}); byte[] randomBytes = getRandomBytes(); - final CommandResult result = command.execute(randomBytes); + ByteArrayInputStream in = new ByteArrayInputStream(randomBytes); + + CommandResult result = + command.executeAsync(in, Command.KILL_SUBPROCESS_ON_INTERRUPT).get(); assertThat(result.getTerminationStatus().getRawExitCode()).isEqualTo(0); TestUtil.assertArrayEquals(randomBytes, result.getStdout()); assertThat(result.getStderr()).isEmpty(); } @Test - public void testCatRandomBinaryToOutputStream() throws Exception { - final Command command = new Command(new String[] {"cat"}); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayOutputStream err = new ByteArrayOutputStream(); + public void testCatRandomBinaryToErrorStream() throws Exception { + final Command command = new Command(new String[] {"/bin/sh", "-c", "cat >&2"}); byte[] randomBytes = getRandomBytes(); - final CommandResult result = command.execute(randomBytes, - Command.NO_OBSERVER, out, err); + ByteArrayInputStream in = new ByteArrayInputStream(randomBytes); + + CommandResult result = + command.executeAsync(in, Command.KILL_SUBPROCESS_ON_INTERRUPT).get(); assertThat(result.getTerminationStatus().getRawExitCode()).isEqualTo(0); - TestUtil.assertArrayEquals(randomBytes, out.toByteArray()); - assertThat(err.toByteArray()).isEmpty(); - assertOutAndErrNotAvailable(result); - } + TestUtil.assertArrayEquals(randomBytes, result.getStderr()); + assertThat(result.getStdout()).isEmpty(); + } @Test - public void testCatRandomBinaryToErrorStream() throws Exception { - final Command command = new Command(new String[] {"/bin/sh", "-c", "cat >&2"}); + public void testCatRandomBinaryFromInputStreamToOutputStream() throws Exception { + final Command command = new Command(new String[] {"cat"}); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); byte[] randomBytes = getRandomBytes(); - final CommandResult result = command.execute(randomBytes, - Command.NO_OBSERVER, out, err); + ByteArrayInputStream in = new ByteArrayInputStream(randomBytes); + + CommandResult result = + command.executeAsync(in, out, err, Command.KILL_SUBPROCESS_ON_INTERRUPT).get(); assertThat(result.getTerminationStatus().getRawExitCode()).isEqualTo(0); - assertThat(out.toByteArray()).isEmpty(); - TestUtil.assertArrayEquals(randomBytes, err.toByteArray()); + assertThat(err.toByteArray()).isEmpty(); + TestUtil.assertArrayEquals(randomBytes, out.toByteArray()); assertOutAndErrNotAvailable(result); } @Test - public void testCatRandomBinaryFromInputStreamToErrorStream() - throws Exception { + public void testCatRandomBinaryFromInputStreamToErrorStream() throws Exception { final Command command = new Command(new String[] {"/bin/sh", "-c", "cat >&2"}); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); byte[] randomBytes = getRandomBytes(); ByteArrayInputStream in = new ByteArrayInputStream(randomBytes); - final CommandResult result = command.execute(in, - Command.NO_OBSERVER, out, err); + CommandResult result = + command.executeAsync(in, out, err, Command.KILL_SUBPROCESS_ON_INTERRUPT).get(); assertThat(result.getTerminationStatus().getRawExitCode()).isEqualTo(0); assertThat(out.toByteArray()).isEmpty(); TestUtil.assertArrayEquals(randomBytes, err.toByteArray()); @@ -118,7 +121,7 @@ public class CommandLargeInputsTest { }); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); - command.execute(Command.NO_INPUT, Command.NO_OBSERVER, out, err); + command.execute(out, err); StringBuilder expectedOut = new StringBuilder(); StringBuilder expectedErr = new StringBuilder(); for (int i = 0; i < 1000; i++) { @@ -144,10 +147,12 @@ public class CommandLargeInputsTest { public void testCatAllByteValues() throws Exception { final Command command = new Command(new String[] {"cat"}); byte[] allByteValues = getAllByteValues(); - final CommandResult result = command.execute(allByteValues); + ByteArrayInputStream in = new ByteArrayInputStream(allByteValues); + + CommandResult result = + command.executeAsync(in, Command.KILL_SUBPROCESS_ON_INTERRUPT).get(); assertThat(result.getTerminationStatus().getRawExitCode()).isEqualTo(0); assertThat(result.getStderr()).isEmpty(); TestUtil.assertArrayEquals(allByteValues, result.getStdout()); } - } -- cgit v1.2.3