This should fix the shell tests. - expectation in testSimpleKillableObserver is wrong - work around race condition in testSimpleKillableObserver by sleeping - make FutureConsumptionTest thread-safe Thanks to Adrian Colley for the report. -- MOS_MIGRATED_REVID=91678160
diff --git a/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java b/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java index 136ffbb..11f88ee 100644 --- a/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java +++ b/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java
@@ -284,11 +284,12 @@ fail(); } catch (CommandException e) { // Good. - checkCommandElements(e, "/bin/sh", "-c", "sleep 5"); + checkCommandElements(e, "sleep", "5"); } } }.start(); - Thread.yield(); + // We're racing against the actual startup of the other command. Wait for 10ms so it can start. + Thread.sleep(10); observer.kill(); } @@ -600,9 +601,6 @@ public synchronized boolean getIsKilled() { return isKilled; } - public synchronized boolean getTimedOut() { - return timedOut; - } /** * Wait for a specified time or until the {@link #kill()} is called. */
diff --git a/src/test/java/com/google/devtools/build/lib/shell/FutureConsumptionTest.java b/src/test/java/com/google/devtools/build/lib/shell/FutureConsumptionTest.java index 578e046..fcd672f 100644 --- a/src/test/java/com/google/devtools/build/lib/shell/FutureConsumptionTest.java +++ b/src/test/java/com/google/devtools/build/lib/shell/FutureConsumptionTest.java
@@ -25,6 +25,7 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.OutputStream; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; @@ -49,8 +50,6 @@ public void write(int b) {} }; - private boolean inputFinished; - @Test public void testFutureConsumptionIgnoresInterruptedExceptions() throws Exception { @@ -59,20 +58,18 @@ OutErrConsumers outErr = Consumers.createStreamingConsumers(DEV_NULL, DEV_NULL); - inputFinished = false; + final AtomicBoolean inputFinished = new AtomicBoolean(false); // We keep producing input until the other thread (the main test thread) // tells us to shut up ... InputStream outInput = new InputStream() { - @Override public int read() { - if(inputFinished){ + if (inputFinished.get()){ return -1; } return 0; } - }; ByteArrayInputStream errInput = new ByteArrayInputStream(new byte[0]); outErr.registerInputs(outInput, errInput, false); @@ -83,7 +80,7 @@ // go into a different thread, wait a bit, interrupt the test thread, // wait a bit, and tell the input stream to finish. new Thread() { - + @Override public void run() { try { Thread.sleep(1000); @@ -92,9 +89,8 @@ try { Thread.sleep(1000); } catch (InterruptedException e) {} - inputFinished = true; + inputFinished.set(true); } - }.start(); outErr.waitForCompletion();
diff --git a/src/test/java/com/google/devtools/build/lib/shell/ShellUtilsTest.java b/src/test/java/com/google/devtools/build/lib/shell/ShellUtilsTest.java index cfda807..804eda4 100644 --- a/src/test/java/com/google/devtools/build/lib/shell/ShellUtilsTest.java +++ b/src/test/java/com/google/devtools/build/lib/shell/ShellUtilsTest.java
@@ -32,7 +32,6 @@ /** * Tests for ShellUtils. - * */ @RunWith(JUnit4.class) public class ShellUtilsTest {