Skip to content

Commit 195ab27

Browse files
committed
[BAEL-44334] Enhance the process test to shutdown ExecutorService
1 parent 79fa194 commit 195ab27

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

core-java-modules/core-java-os/src/test/java/com/baeldung/shell/JavaProcessUnitIntegrationTest.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
package com.baeldung.java.shell;
1+
package com.baeldung.shell;
22

3+
import org.junit.After;
34
import org.junit.Assert;
5+
import org.junit.Before;
46
import org.junit.Test;
57

6-
import java.io.*;
8+
import java.io.BufferedReader;
9+
import java.io.File;
10+
import java.io.InputStream;
11+
import java.io.InputStreamReader;
12+
import java.util.concurrent.ExecutorService;
713
import java.util.concurrent.Executors;
14+
import java.util.concurrent.Future;
15+
import java.util.concurrent.TimeUnit;
816
import java.util.function.Consumer;
917

18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
20+
1021
public class JavaProcessUnitIntegrationTest {
1122
private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows");
1223

@@ -29,6 +40,18 @@ public void run() {
2940

3041
private String homeDirectory = System.getProperty("user.home");
3142

43+
private ExecutorService executorService;
44+
45+
@Before
46+
public void setUp() {
47+
executorService = Executors.newSingleThreadExecutor();
48+
}
49+
50+
@After
51+
public void tearDown() {
52+
executorService.shutdown();
53+
}
54+
3255
@Test
3356
public void givenProcess_whenCreatingViaRuntime_shouldSucceed() throws Exception {
3457
Process process;
@@ -38,9 +61,13 @@ public void givenProcess_whenCreatingViaRuntime_shouldSucceed() throws Exception
3861
process = Runtime.getRuntime().exec(String.format("sh -c ls %s", homeDirectory));
3962
}
4063
StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), consumer);
41-
Executors.newSingleThreadExecutor().submit(streamGobbler);
64+
65+
Future<?> future = executorService.submit(streamGobbler);
4266
int exitCode = process.waitFor();
43-
Assert.assertEquals(0, exitCode);
67+
68+
// verify the stream output from the process
69+
assertDoesNotThrow(() -> future.get(10, TimeUnit.SECONDS));
70+
assertEquals(0, exitCode);
4471
}
4572

4673
@Test
@@ -54,8 +81,12 @@ public void givenProcess_whenCreatingViaProcessBuilder_shouldSucceed() throws Ex
5481
builder.directory(new File(homeDirectory));
5582
Process process = builder.start();
5683
StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), consumer);
57-
Executors.newSingleThreadExecutor().submit(streamGobbler);
84+
85+
Future<?> future = executorService.submit(streamGobbler);
5886
int exitCode = process.waitFor();
59-
Assert.assertEquals(0, exitCode);
87+
88+
// verify the stream output from the process
89+
assertDoesNotThrow(() -> future.get(10, TimeUnit.SECONDS));
90+
assertEquals(0, exitCode);
6091
}
6192
}

0 commit comments

Comments
 (0)