Skip to content

Commit

Permalink
ptree
Browse files Browse the repository at this point in the history
  • Loading branch information
snazy committed Apr 13, 2023
1 parent 39cd2fa commit 986b81a
Showing 1 changed file with 48 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -41,18 +44,45 @@ public void main() throws Exception {
beforeQuarkusDev();
ExecutorService executor = null;
AtomicReference<BuildResult> buildResult = new AtomicReference<>();
List<String> processesBeforeTest = dumpProcesses();
List<String> processesAfterTest = Collections.emptyList();
try {
executor = Executors.newSingleThreadExecutor();
quarkusDev = executor.submit(() -> {
try {
buildResult.set(build());
} catch (Exception e) {
throw new IllegalStateException("Failed to build the project", e);
try {
executor = Executors.newSingleThreadExecutor();
quarkusDev = executor.submit(() -> {
try {
buildResult.set(build());
} catch (Exception e) {
throw new IllegalStateException("Failed to build the project", e);
}
});
testDevMode();
} finally {
processesAfterTest = dumpProcesses();

if (quarkusDev != null) {
quarkusDev.cancel(true);
}
if (executor != null) {
executor.shutdownNow();
}

// Kill all processes that were (indirectly) spawned by the current process.
DevModeTestUtils.killDescendingProcesses();

DevModeTestUtils.awaitUntilServerDown();

if (projectDir != null && projectDir.isDirectory()) {
FileUtils.deleteQuietly(projectDir);
}
});
testDevMode();
}
} catch (Exception | AssertionError e) {
dumpProcesses();
System.err.println("PROCESSES BEFORE TEST:");
processesBeforeTest.forEach(System.err::println);
System.err.println("PROCESSES AFTER TEST (BEFORE CLEANUP):");
processesAfterTest.forEach(System.err::println);
System.err.println("PROCESSES AFTER CLEANUP:");
dumpProcesses().forEach(System.err::println);

if (buildResult.get() != null) {
System.err.println("BELOW IS THE CAPTURED LOGGING OF THE FAILED GRADLE TEST PROJECT BUILD");
Expand All @@ -73,34 +103,18 @@ public void main() throws Exception {
}
}
throw e;
} finally {
if (quarkusDev != null) {
quarkusDev.cancel(true);
}
if (executor != null) {
executor.shutdownNow();
}

// Kill all processes that were (indirectly) spawned by the current process.
DevModeTestUtils.killDescendingProcesses();

DevModeTestUtils.awaitUntilServerDown();

if (projectDir != null && projectDir.isDirectory()) {
FileUtils.deleteQuietly(projectDir);
}
}
}

public static void dumpProcesses() {
Consumer<ProcessHandle> dumpProcess = p -> {
public static List<String> dumpProcesses() {
ProcessHandle current = ProcessHandle.current();
return Stream.concat(Stream.of(current), current.descendants()).map(p -> {
ProcessHandle.Info i = p.info();
System.err.printf("PID %8d (%8d) started:%s CPU:%s - %s%n", p.pid(), p.parent().map(ProcessHandle::pid).orElse(-1L),
i.startInstant(), i.totalCpuDuration(), i.commandLine());
};
ProcessHandle root = ProcessHandle.current().parent().orElse(ProcessHandle.current());
dumpProcess.accept(root);
root.descendants().forEach(dumpProcess);
return String.format("PID %8d (%8d) started:%s CPU:%s - %s", p.pid(),
p.parent().map(ProcessHandle::pid).orElse(-1L),
i.startInstant().orElse(null), i.totalCpuDuration().orElse(null),
i.commandLine().orElse("<command line not available>"));
}).collect(Collectors.toList());
}

protected BuildResult build() throws Exception {
Expand Down

0 comments on commit 986b81a

Please sign in to comment.