Skip to content

Commit

Permalink
8303133: Update ProcessTools.startProcess(...) to exit early if proce…
Browse files Browse the repository at this point in the history
…ss exit before linePredicate is printed.

Reviewed-by: dholmes, rriggs
  • Loading branch information
lmesnik committed Mar 1, 2023
1 parent 65da2c5 commit 1fdaf25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
20 changes: 14 additions & 6 deletions test/lib/jdk/test/lib/process/ProcessTools.java
@@ -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
Expand Down Expand Up @@ -216,15 +216,23 @@ protected void processLine(String line) {

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());
Expand Down
19 changes: 12 additions & 7 deletions test/lib/jdk/test/lib/thread/ProcessThread.java
@@ -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
Expand Down Expand Up @@ -150,16 +150,21 @@ public ProcessRunnable(ProcessBuilder pb, String name, Predicate<String> waitfor
*/
@Override
public void xrun() throws Throwable {
this.process = ProcessTools.startProcess(name, processBuilder, waitfor);
// Release when process is started
latch.countDown();
try {
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();
}

// Will block...
try {
this.process.waitFor();
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 {
Expand Down

1 comment on commit 1fdaf25

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.