8303133: Update ProcessTools.startProcess(...) to exit early if process exit before linePredicate is printed.

Reviewed-by: dholmes, rriggs
This commit is contained in:
Leonid Mesnik 2023-03-01 00:50:35 +00:00
parent 65da2c5d2d
commit 1fdaf252b6
2 changed files with 28 additions and 15 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -216,15 +216,23 @@ public final class ProcessTools {
try {
if (timeout > -1) {
if (timeout == 0) {
latch.await();
} else {
if (!latch.await(Utils.adjustTimeout(timeout), unit)) {
// Every second check if line is printed and if process is still alive
Utils.waitForCondition(() -> latch.getCount() == 0 || !p.isAlive(),
unit.toMillis(Utils.adjustTimeout(timeout)), 1000);
if (latch.getCount() > 0) {
if (!p.isAlive()) {
// Give some extra time for the StreamPumper to run after the process completed
Thread.sleep(1000);
if (latch.getCount() > 0) {
throw new RuntimeException("Started process " + name + " terminated before producing the expected output.");
}
} else {
throw new TimeoutException();
}
}
}
} catch (TimeoutException | InterruptedException e) {
} catch (TimeoutException | RuntimeException | InterruptedException e) {
System.err.println("Failed to start a process (thread dump follows)");
for (Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) {
printStack(s.getKey(), s.getValue());

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -150,16 +150,21 @@ public class ProcessThread extends TestThread {
*/
@Override
public void xrun() throws Throwable {
this.process = ProcessTools.startProcess(name, processBuilder, waitfor);
// Release when process is started
latch.countDown();
// Will block...
try {
this.process.waitFor();
output = new OutputAnalyzer(this.process);
this.process = ProcessTools.startProcess(name, processBuilder, waitfor);
} catch (Throwable t) {
System.out.println(String.format("ProcessThread[%s] failed: %s", name, t.toString()));
throw t;
} finally {
// Release when process is started or failed
latch.countDown();
}
try {
output = new OutputAnalyzer(this.process);
// Will block...
this.process.waitFor();
} catch (Throwable t) {
String name = Thread.currentThread().getName();
System.out.println(String.format("ProcessThread[%s] failed: %s", name, t.toString()));
throw t;
} finally {