Skip to content

Commit

Permalink
test: gather all output before going on with tests
Browse files Browse the repository at this point in the history
Wait for consumer thread to collect all output before returning from
command line run. This patch avoid a race condition (the test output
is checked before all the output is actually collected).
  • Loading branch information
cmaglie committed Jun 19, 2020
1 parent ea25c35 commit 1621776
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions app/test/processing/app/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,16 @@ public static void findBuildPaths() throws Exception {
System.out.println("found arduino: " + arduinoPath);
}

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

public Process runArduino(OutputStream output, boolean success, File wd, String[] extraArgs) throws IOException, InterruptedException {
Expand All @@ -107,9 +109,11 @@ public Process runArduino(OutputStream output, boolean success, File wd, String[
System.out.println("Running: " + String.join(" ", args));

Process pr = rt.exec(args.toArray(new String[0]), null, wd);
consume(pr.getInputStream(), output);
consume(pr.getErrorStream(), System.err);
Thread outThread = consume(pr.getInputStream(), output);
Thread errThread = consume(pr.getErrorStream(), System.err);
pr.waitFor();
outThread.join(5000);
errThread.join(5000);
if (success)
assertEquals(0, pr.exitValue());
return pr;
Expand Down

0 comments on commit 1621776

Please sign in to comment.