Skip to content

Commit

Permalink
Explicitly pass output stream in runArduino(..) test function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cmaglie committed May 12, 2020
1 parent a8384b5 commit 44afcbf
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions app/test/processing/app/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -84,17 +86,17 @@ public static void findBuildPaths() throws Exception {
System.out.println("found arduino: " + arduinoPath);
}

private void consume(InputStream s) {
private void consume(InputStream s, OutputStream out) {
new Thread(() -> {
try {
IOUtils.copy(s, System.out);
IOUtils.copy(s, out);
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}

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

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

Process pr = rt.exec(args.toArray(new String[0]), null, wd);
if (output) {
consume(pr.getInputStream());
consume(pr.getErrorStream());
}
consume(pr.getInputStream(), output);
consume(pr.getErrorStream(), System.err);
pr.waitFor();
if (success)
assertEquals(0, pr.exitValue());
Expand All @@ -118,7 +118,7 @@ public Process runArduino(boolean output, boolean success, File wd, String[] ext
@Test
public void testCommandLineBuildWithRelativePath() throws Exception {
File wd = new File(buildPath, "build/shared/examples/01.Basics/Blink/");
runArduino(true, true, wd, new String[] {
runArduino(System.out, true, wd, new String[] {
"--board", "arduino:avr:uno",
"--verify", "Blink.ino",
});
Expand All @@ -129,21 +129,21 @@ public void testCommandLinePreferencesSave() throws Exception {
File prefFile = File.createTempFile("test_pref", ".txt");
prefFile.deleteOnExit();

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

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

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

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

@Test
public void testCommandLineVersion() throws Exception {
Process pr = runArduino(false, true, null, new String[] {
ByteArrayOutputStream out = new ByteArrayOutputStream();
runArduino(out, true, null, new String[] {
"--version",
});

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

@Test
public void testCommandLineMultipleAction() throws Exception {
Process pr = runArduino(true, false, null, new String[] {
Process pr = runArduino(System.out, false, null, new String[] {
"--version",
"--verify",
});
Expand Down

0 comments on commit 44afcbf

Please sign in to comment.