提交 adb98e98 编写于 作者: R rriggs

8029629: java/lang/ProcessBuilder/Basic.java fails intermittently

Summary: Improved test for Thread.interrupt
Reviewed-by: martin, rriggs
Contributed-by: martinrb@google.com
上级 f86e9db0
...@@ -61,6 +61,15 @@ public class Basic { ...@@ -61,6 +61,15 @@ public class Basic {
/* used for AIX only */ /* used for AIX only */
static final String libpath = System.getenv("LIBPATH"); static final String libpath = System.getenv("LIBPATH");
/**
* Returns the number of milliseconds since time given by
* startNanoTime, which must have been previously returned from a
* call to {@link System.nanoTime()}.
*/
private static long millisElapsedSince(long startNanoTime) {
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
}
private static String commandOutput(Reader r) throws Throwable { private static String commandOutput(Reader r) throws Throwable {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
int c; int c;
...@@ -2294,40 +2303,66 @@ public class Basic { ...@@ -2294,40 +2303,66 @@ public class Basic {
//---------------------------------------------------------------- //----------------------------------------------------------------
// Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS) // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
// interrupt works as expected. // interrupt works as expected, if interrupted while waiting.
//---------------------------------------------------------------- //----------------------------------------------------------------
try { try {
List<String> childArgs = new ArrayList<String>(javaChildArgs); List<String> childArgs = new ArrayList<String>(javaChildArgs);
childArgs.add("sleep"); childArgs.add("sleep");
final Process p = new ProcessBuilder(childArgs).start(); final Process p = new ProcessBuilder(childArgs).start();
final long start = System.nanoTime(); final long start = System.nanoTime();
final CountDownLatch ready = new CountDownLatch(1); final CountDownLatch aboutToWaitFor = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
final Thread thread = new Thread() { final Thread thread = new Thread() {
public void run() { public void run() {
try { try {
final boolean result; aboutToWaitFor.countDown();
try { boolean result = p.waitFor(30L * 1000L, TimeUnit.MILLISECONDS);
ready.countDown();
result = p.waitFor(30000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
return;
}
fail("waitFor() wasn't interrupted, its return value was: " + result); fail("waitFor() wasn't interrupted, its return value was: " + result);
} catch (Throwable t) { } catch (InterruptedException success) {
unexpected(t); } catch (Throwable t) { unexpected(t); }
} finally {
done.countDown();
}
} }
}; };
thread.start(); thread.start();
ready.await(); aboutToWaitFor.await();
Thread.sleep(1000); Thread.sleep(1000);
thread.interrupt(); thread.interrupt();
done.await(); thread.join(10L * 1000L);
check(millisElapsedSince(start) < 10L * 1000L);
check(!thread.isAlive());
p.destroy();
} catch (Throwable t) { unexpected(t); }
//----------------------------------------------------------------
// Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
// interrupt works as expected, if interrupted before waiting.
//----------------------------------------------------------------
try {
List<String> childArgs = new ArrayList<String>(javaChildArgs);
childArgs.add("sleep");
final Process p = new ProcessBuilder(childArgs).start();
final long start = System.nanoTime();
final CountDownLatch threadStarted = new CountDownLatch(1);
final Thread thread = new Thread() {
public void run() {
try {
threadStarted.countDown();
do { Thread.yield(); }
while (!Thread.currentThread().isInterrupted());
boolean result = p.waitFor(30L * 1000L, TimeUnit.MILLISECONDS);
fail("waitFor() wasn't interrupted, its return value was: " + result);
} catch (InterruptedException success) {
} catch (Throwable t) { unexpected(t); }
}
};
thread.start();
threadStarted.await();
thread.interrupt();
thread.join(10L * 1000L);
check(millisElapsedSince(start) < 10L * 1000L);
check(!thread.isAlive());
p.destroy(); p.destroy();
} catch (Throwable t) { unexpected(t); } } catch (Throwable t) { unexpected(t); }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册