Skip to content

Commit 7ddce3e

Browse files
committed
Fix hanging TimeoutConfigurationDoc test
Fixes the hanging TimeoutConfigurationDoc on Java 25. Where jstack blocks, when the other process tries to read the stdout/err of jstack.
1 parent c30d2ab commit 7ddce3e

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

spock-core/src/main/java/org/spockframework/util/JavaProcessThreadDumpCollector.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616

1717
package org.spockframework.util;
1818

19+
import org.codehaus.groovy.runtime.ResourceGroovyMethods;
1920
import org.spockframework.runtime.SpockException;
2021
import org.spockframework.runtime.extension.builtin.ThreadDumpUtility;
2122
import org.spockframework.runtime.extension.builtin.ThreadDumpUtilityType;
2223

2324
import java.io.*;
2425
import java.lang.management.ManagementFactory;
26+
import java.nio.charset.StandardCharsets;
27+
import java.nio.file.Files;
2528
import java.nio.file.Path;
2629
import java.nio.file.Paths;
2730
import java.util.List;
@@ -66,21 +69,23 @@ public void appendThreadDumpOfCurrentJvm(StringBuilder builder) throws IOExcepti
6669
.append(TextUtil.repeatChar('-', utilityName.length()))
6770
.append("\n");
6871

72+
73+
File threadDumpFile = Files.createTempFile("Spock-threaddump", ".txt").toFile();
74+
6975
Process process = new ProcessBuilder(command)
7076
.redirectErrorStream(true)
77+
.redirectOutput(ProcessBuilder.Redirect.to(threadDumpFile))
7178
.start();
7279

73-
captureProcessOutput(process, builder);
7480
process.waitFor();
81+
82+
readAndDeleteFile(builder, threadDumpFile);
7583
}
7684

77-
private void captureProcessOutput(Process process, StringBuilder builder) throws IOException {
78-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
79-
String line;
80-
while ((line = reader.readLine()) != null) {
81-
builder.append(line).append("\n");
82-
}
83-
}
85+
private void readAndDeleteFile(StringBuilder builder, File threadDumpFile) throws IOException {
86+
builder.append(ResourceGroovyMethods.getText(threadDumpFile, StandardCharsets.UTF_8.name()));
87+
//noinspection ResultOfMethodCallIgnored
88+
threadDumpFile.delete(); // We do not care if the temp file could not be deleted.
8489
}
8590

8691
private static Path getJavaHome() {

0 commit comments

Comments
 (0)