Skip to content

Commit ae8730f

Browse files
committed
8303486: [REDO] Update ProcessTools.startProcess(...) to exit early if process exit before linePredicate is printed.
Reviewed-by: dholmes
1 parent 5977f26 commit ae8730f

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

test/lib/jdk/test/lib/process/ProcessTools.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,25 @@ protected void processLine(String line) {
217217

218218
try {
219219
if (timeout > -1) {
220-
if (timeout == 0) {
221-
latch.await();
222-
} else {
223-
if (!latch.await(Utils.adjustTimeout(timeout), unit)) {
220+
221+
long timeoutMs = timeout == 0 ? -1: unit.toMillis(Utils.adjustTimeout(timeout));
222+
// Every second check if line is printed and if process is still alive
223+
Utils.waitForCondition(() -> latch.getCount() == 0 || !p.isAlive(),
224+
timeoutMs , 1000);
225+
226+
if (latch.getCount() > 0) {
227+
if (!p.isAlive()) {
228+
// Give some extra time for the StreamPumper to run after the process completed
229+
Thread.sleep(1000);
230+
if (latch.getCount() > 0) {
231+
throw new RuntimeException("Started process " + name + " terminated before producing the expected output.");
232+
}
233+
} else {
224234
throw new TimeoutException();
225235
}
226236
}
227237
}
228-
} catch (TimeoutException | InterruptedException e) {
238+
} catch (TimeoutException | RuntimeException | InterruptedException e) {
229239
System.err.println("Failed to start a process (thread dump follows)");
230240
for (Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) {
231241
printStack(s.getKey(), s.getValue());

test/lib/jdk/test/lib/thread/ProcessThread.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -150,16 +150,21 @@ public ProcessRunnable(ProcessBuilder pb, String name, Predicate<String> waitfor
150150
*/
151151
@Override
152152
public void xrun() throws Throwable {
153-
this.process = ProcessTools.startProcess(name, processBuilder, waitfor);
154-
// Release when process is started
155-
latch.countDown();
153+
try {
154+
this.process = ProcessTools.startProcess(name, processBuilder, waitfor);
155+
} catch (Throwable t) {
156+
System.out.println(String.format("ProcessThread[%s] failed: %s", name, t.toString()));
157+
throw t;
158+
} finally {
159+
// Release when process is started or failed
160+
latch.countDown();
161+
}
156162

157-
// Will block...
158163
try {
159-
this.process.waitFor();
160164
output = new OutputAnalyzer(this.process);
165+
// Will block...
166+
this.process.waitFor();
161167
} catch (Throwable t) {
162-
String name = Thread.currentThread().getName();
163168
System.out.println(String.format("ProcessThread[%s] failed: %s", name, t.toString()));
164169
throw t;
165170
} finally {

0 commit comments

Comments
 (0)