tutorials icon indicating copy to clipboard operation
tutorials copied to clipboard

Article code and JavaProcessUnitIntegrationTest.java does not close the async thread, loses partial output

Open verhas opened this issue 3 years ago • 1 comments

JavaProcessUnitIntegrationTest.java does not properly test the output reading of a process. The example in the article is also misleading and can cause a lot of headaches to readers. I recommend fixing at least the article.

The problem does not manifest when sending the output to the standard output. However, when you want to collect the output of the external process to a String, for example, using a StringWriter sw supplying sw::write as a string consumer, then the output may be truncated. The reason for that is the still running StreamGobbler on an asynchronous thread. Therefore, you MUST store the Future and do like:

final var future = Executors.newSingleThreadExecutor().submit(streamGobbler);
int exitCode = process.waitFor();
future.get();  <- wait for all the returned data written out to the consumer

verhas avatar Feb 20 '22 17:02 verhas

Hi @verhas , thanks for the heads up! We'll take a look into this.

Internal note: this is a core-java-os module

kwoyke avatar Mar 10 '22 12:03 kwoyke

Hi @verhas The code and article is updated

anastasiosioannidis avatar May 01 '23 20:05 anastasiosioannidis