Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 44afcbf

Browse files
committedMay 12, 2020
Explicitly pass output stream in runArduino(..) test function
Previously while testing on Windows, if the process produces a lot of output that is not consumed, the pr.Wait() call will never end because the OS doesn't have enough buffering.
1 parent a8384b5 commit 44afcbf

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed
 

‎app/test/processing/app/CommandLineTest.java

+16-15
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
import static org.junit.Assert.assertEquals;
3333
import static org.junit.Assert.assertNull;
3434

35+
import java.io.ByteArrayOutputStream;
3536
import java.io.File;
3637
import java.io.IOException;
3738
import java.io.InputStream;
39+
import java.io.OutputStream;
3840
import java.util.List;
3941
import java.util.ArrayList;
4042
import java.util.Arrays;
@@ -84,17 +86,17 @@ public static void findBuildPaths() throws Exception {
8486
System.out.println("found arduino: " + arduinoPath);
8587
}
8688

87-
private void consume(InputStream s) {
89+
private void consume(InputStream s, OutputStream out) {
8890
new Thread(() -> {
8991
try {
90-
IOUtils.copy(s, System.out);
92+
IOUtils.copy(s, out);
9193
} catch (IOException e) {
9294
e.printStackTrace();
9395
}
9496
}).start();
9597
}
9698

97-
public Process runArduino(boolean output, boolean success, File wd, String[] extraArgs) throws IOException, InterruptedException {
99+
public Process runArduino(OutputStream output, boolean success, File wd, String[] extraArgs) throws IOException, InterruptedException {
98100
Runtime rt = Runtime.getRuntime();
99101

100102
List<String> args = new ArrayList<>();
@@ -105,10 +107,8 @@ public Process runArduino(boolean output, boolean success, File wd, String[] ext
105107
System.out.println("Running: " + String.join(" ", args));
106108

107109
Process pr = rt.exec(args.toArray(new String[0]), null, wd);
108-
if (output) {
109-
consume(pr.getInputStream());
110-
consume(pr.getErrorStream());
111-
}
110+
consume(pr.getInputStream(), output);
111+
consume(pr.getErrorStream(), System.err);
112112
pr.waitFor();
113113
if (success)
114114
assertEquals(0, pr.exitValue());
@@ -118,7 +118,7 @@ public Process runArduino(boolean output, boolean success, File wd, String[] ext
118118
@Test
119119
public void testCommandLineBuildWithRelativePath() throws Exception {
120120
File wd = new File(buildPath, "build/shared/examples/01.Basics/Blink/");
121-
runArduino(true, true, wd, new String[] {
121+
runArduino(System.out, true, wd, new String[] {
122122
"--board", "arduino:avr:uno",
123123
"--verify", "Blink.ino",
124124
});
@@ -129,21 +129,21 @@ public void testCommandLinePreferencesSave() throws Exception {
129129
File prefFile = File.createTempFile("test_pref", ".txt");
130130
prefFile.deleteOnExit();
131131

132-
runArduino(true, true, null, new String[] {
132+
runArduino(System.out, true, null, new String[] {
133133
"--save-prefs",
134134
"--preferences-file", prefFile.getAbsolutePath(),
135135
"--version", // avoids starting the GUI
136136
});
137137

138-
runArduino(true, true, null, new String[] {
138+
runArduino(System.out, true, null, new String[] {
139139
"--pref", "test_pref=xxx",
140140
"--preferences-file", prefFile.getAbsolutePath(),
141141
});
142142

143143
PreferencesMap prefs = new PreferencesMap(prefFile);
144144
assertNull("preference should not be saved", prefs.get("test_pref"));
145145

146-
runArduino(true, true, null, new String[] {
146+
runArduino(System.out, true, null, new String[] {
147147
"--pref", "test_pref=xxx",
148148
"--preferences-file", prefFile.getAbsolutePath(),
149149
"--save-prefs",
@@ -155,17 +155,18 @@ public void testCommandLinePreferencesSave() throws Exception {
155155

156156
@Test
157157
public void testCommandLineVersion() throws Exception {
158-
Process pr = runArduino(false, true, null, new String[] {
158+
ByteArrayOutputStream out = new ByteArrayOutputStream();
159+
runArduino(out, true, null, new String[] {
159160
"--version",
160161
});
161162

162-
Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream())))
163-
.matches("Arduino: \\d+\\.\\d+\\.\\d+.*\r?\n");
163+
Assertions.assertThat(out.toString())
164+
.matches("(?m)Arduino: \\d+\\.\\d+\\.\\d+.*\r?\n");
164165
}
165166

166167
@Test
167168
public void testCommandLineMultipleAction() throws Exception {
168-
Process pr = runArduino(true, false, null, new String[] {
169+
Process pr = runArduino(System.out, false, null, new String[] {
169170
"--version",
170171
"--verify",
171172
});

0 commit comments

Comments
 (0)
Please sign in to comment.