Skip to content

Commit 5590a10

Browse files
committed
Trying to cleanup the messy test
1 parent 5fc47c2 commit 5590a10

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

vm/tests/src/test/java/com/codename1/tools/translator/CompilerHelper.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.codename1.tools.translator;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.File;
45
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.nio.charset.StandardCharsets;
58
import java.nio.file.Files;
69
import java.nio.file.Path;
710
import java.nio.file.Paths;
@@ -16,6 +19,7 @@
1619
* Helper class to manage external JDK compilers.
1720
*/
1821
public class CompilerHelper {
22+
private static String lastErrorLog;
1923

2024
private static final Map<String, Path> availableJdks = new TreeMap<>();
2125

@@ -157,11 +161,27 @@ public static int compile(Path jdkHome, List<String> args) throws IOException, I
157161

158162
ProcessBuilder pb = new ProcessBuilder(command);
159163
// Inherit IO so we see errors in the log
160-
pb.inheritIO();
164+
pb.redirectErrorStream(true);
161165
Process p = pb.start();
166+
lastErrorLog = "";
167+
try (InputStream is = p.getInputStream();
168+
ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
169+
170+
byte[] data = new byte[8192]; // 8 KB buffer
171+
int n;
172+
while ((n = is.read(data)) != -1) {
173+
buffer.write(data, 0, n);
174+
}
175+
176+
lastErrorLog = buffer.toString("UTF-8");
177+
}
162178
return p.waitFor();
163179
}
164180

181+
public static String getLastErrorLog() {
182+
return lastErrorLog;
183+
}
184+
165185
public static class CompilerConfig {
166186
public final String jdkVersion;
167187
public final Path jdkHome;

vm/tests/src/test/java/com/codename1/tools/translator/StackOverflowIntegrationTest.java

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ void throwsAndRecoversFromStackOverflow(CompilerHelper.CompilerConfig config) th
5555
compileArgs.add(sourceDir.resolve("StackOverflowApp.java").toString());
5656

5757
int compileResult = CompilerHelper.compile(config.jdkHome, compileArgs);
58-
assertEquals(0, compileResult, "StackOverflowApp should compile with " + config);
58+
assertEquals(0, compileResult, "StackOverflowApp should compile with " + config +
59+
" error: " + CompilerHelper.getLastErrorLog());
5960

6061
CompilerHelper.copyDirectory(javaApiDir, classesDir);
6162
Files.copy(sourceDir.resolve("native_report.c"), classesDir.resolve("native_report.c"));
@@ -92,22 +93,12 @@ void throwsAndRecoversFromStackOverflow(CompilerHelper.CompilerConfig config) th
9293
"StackOverflowApp probe run exited with code " + probeResult.exitCode
9394
+ ". Output:\n" + probeResult.output
9495
+ probeDiagnostics);
95-
assertTrue(probeResult.output.contains("PROBE_CONSTANT"),
96-
"StackOverflowApp probe run should succeed. Output was:\n" + probeResult.output + probeDiagnostics);
9796
ProcessResult smokeResult = runProcess(Arrays.asList(executable.toString(), "smoke"), buildDir);
9897
String smokeDiagnostics = buildDiagnostics(srcRoot, executable, smokeResult);
9998
assertEquals(0, smokeResult.exitCode,
10099
"StackOverflowApp smoke run exited with code " + smokeResult.exitCode
101100
+ ". Output:\n" + smokeResult.output
102101
+ smokeDiagnostics);
103-
assertTrue(smokeResult.output.contains("PROBE_CONSTANT_ONE"),
104-
"StackOverflowApp smoke run should emit PROBE_CONSTANT_ONE. Output was:\n"
105-
+ smokeResult.output + smokeDiagnostics);
106-
assertTrue(smokeResult.output.contains("PROBE_CONSTANT_TWO"),
107-
"StackOverflowApp smoke run should emit PROBE_CONSTANT_TWO. Output was:\n"
108-
+ smokeResult.output
109-
+ "\nMissing PROBE_CONSTANT_TWO suggests a crash between the first and second print."
110-
+ smokeDiagnostics);
111102

112103
ProcessResult result = runProcess(Arrays.asList(executable.toString(), "overflow", "run"), buildDir);
113104
String diagnostics = buildDiagnostics(srcRoot, executable, result);
@@ -121,10 +112,11 @@ void throwsAndRecoversFromStackOverflow(CompilerHelper.CompilerConfig config) th
121112

122113
private String appSource() {
123114
return "public class StackOverflowApp {\n" +
115+
" private int counter;\n" +
124116
" private static native void report(String msg);\n" +
125-
" private static native void reportConstant();\n" +
126-
" private static native void reportConstantTwice();\n" +
127117
" private static void triggerOverflow() {\n" +
118+
" report(\"Calling ...\" + counter);\n" +
119+
" counter++;" +
128120
" triggerOverflow();\n" +
129121
" }\n" +
130122
" private static int boundedRecursion(int depth) {\n" +
@@ -134,14 +126,7 @@ private String appSource() {
134126
" return depth + boundedRecursion(depth - 1);\n" +
135127
" }\n" +
136128
" public static void main(String[] args) {\n" +
137-
" if (args == null || args.length == 0) {\n" +
138-
" reportConstant();\n" +
139-
" return;\n" +
140-
" }\n" +
141-
" if (args.length == 1) {\n" +
142-
" reportConstantTwice();\n" +
143-
" return;\n" +
144-
" }\n" +
129+
" report(\"Starting test...\");\n" +
145130
" try {\n" +
146131
" triggerOverflow();\n" +
147132
" } catch (StackOverflowError err) {\n" +
@@ -154,16 +139,6 @@ private String appSource() {
154139
private String nativeReportSource() {
155140
return "#include \"cn1_globals.h\"\n" +
156141
"#include <stdio.h>\n" +
157-
"void StackOverflowApp_reportConstant__(CODENAME_ONE_THREAD_STATE) {\n" +
158-
" printf(\"PROBE_CONSTANT\\n\");\n" +
159-
" fflush(stdout);\n" +
160-
"}\n" +
161-
"void StackOverflowApp_reportConstantTwice__(CODENAME_ONE_THREAD_STATE) {\n" +
162-
" printf(\"PROBE_CONSTANT_ONE\\n\");\n" +
163-
" fflush(stdout);\n" +
164-
" printf(\"PROBE_CONSTANT_TWO\\n\");\n" +
165-
" fflush(stdout);\n" +
166-
"}\n" +
167142
"void StackOverflowApp_report___java_lang_String(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT msg) {\n" +
168143
" struct String_Struct {\n" +
169144
" JAVA_OBJECT header;\n" +
@@ -247,7 +222,9 @@ private String buildDiagnostics(Path srcRoot, Path executable, ProcessResult res
247222
diagnostics.append("\nreport snippet: ")
248223
.append(extractSnippet(appSourceText, "StackOverflowApp_report___java_lang_String", 120));
249224
}
250-
if (!result.output.isEmpty()) {
225+
if (result.output.isEmpty()) {
226+
diagnostics.append("\n***No output printed");
227+
} else {
251228
diagnostics.append("\nOutput length: ").append(result.output.length());
252229
}
253230
return diagnostics.toString();

0 commit comments

Comments
 (0)