Article code and JavaProcessUnitIntegrationTest.java does not close the async thread, loses partial output
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
Hi @verhas , thanks for the heads up! We'll take a look into this.
Internal note: this is a core-java-os module
Hi @verhas The code and article is updated