From 1621776cc725f8d33e58053c7dea5574e3f6a7b5 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 19 Jun 2020 11:49:52 +0100 Subject: [PATCH] test: gather all output before going on with tests 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). --- app/test/processing/app/CommandLineTest.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/test/processing/app/CommandLineTest.java b/app/test/processing/app/CommandLineTest.java index fd9e35e4ff9..5df7307ae47 100644 --- a/app/test/processing/app/CommandLineTest.java +++ b/app/test/processing/app/CommandLineTest.java @@ -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 { @@ -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;