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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -216,15 +216,23 @@ public final class ProcessTools {
try { try {
if (timeout > -1) { if (timeout > -1) {
if (timeout == 0) { // Every second check if line is printed and if process is still alive
latch.await(); 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 { } else {
if (!latch.await(Utils.adjustTimeout(timeout), unit)) {
throw new TimeoutException(); throw new TimeoutException();
} }
} }
} }
} catch (TimeoutException | InterruptedException e) { } catch (TimeoutException | RuntimeException | InterruptedException e) {
System.err.println("Failed to start a process (thread dump follows)"); System.err.println("Failed to start a process (thread dump follows)");
for (Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) { for (Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) {
printStack(s.getKey(), s.getValue()); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -150,16 +150,21 @@ public class ProcessThread extends TestThread {
*/ */
@Override @Override
public void xrun() throws Throwable { public void xrun() throws Throwable {
this.process = ProcessTools.startProcess(name, processBuilder, waitfor);
// Release when process is started
latch.countDown();
// Will block...
try { try {
this.process.waitFor(); this.process = ProcessTools.startProcess(name, processBuilder, waitfor);
output = new OutputAnalyzer(this.process); } 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) { } catch (Throwable t) {
String name = Thread.currentThread().getName();
System.out.println(String.format("ProcessThread[%s] failed: %s", name, t.toString())); System.out.println(String.format("ProcessThread[%s] failed: %s", name, t.toString()));
throw t; throw t;
} finally { } finally {