1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.exec.issues;
19
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21
22 import java.io.File;
23
24 import org.apache.commons.exec.CommandLine;
25 import org.apache.commons.exec.DefaultExecutor;
26 import org.apache.commons.exec.ExecuteException;
27 import org.apache.commons.exec.ExecuteWatchdog;
28 import org.apache.commons.exec.OS;
29 import org.apache.commons.exec.PumpStreamHandler;
30 import org.apache.commons.exec.TestUtil;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36 public class Exec41Test {
37
38 private final File testDir = new File("src/test/scripts");
39 private final File pingScript = TestUtil.resolveScriptForOS(testDir + "/ping");
40
41
42
43
44
45
46
47
48
49
50 @Test
51 public void testExec41WithoutStreams() throws Exception {
52
53 final CommandLine cmdLine = new CommandLine(pingScript);
54 cmdLine.addArgument("10");
55 final DefaultExecutor executor = DefaultExecutor.builder().get();
56 final ExecuteWatchdog watchdog = new ExecuteWatchdog(2 * 1000);
57
58
59 final PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(null, null, null);
60
61 executor.setWatchdog(watchdog);
62 executor.setStreamHandler(pumpStreamHandler);
63
64 final long startTime = System.currentTimeMillis();
65
66 try {
67 executor.execute(cmdLine);
68 } catch (final ExecuteException e) {
69 System.out.println(e);
70 }
71
72 final long duration = System.currentTimeMillis() - startTime;
73
74 System.out.println("Process completed in " + duration + " millis; below is its output");
75
76 if (watchdog.killedProcess()) {
77 System.out.println("Process timed out and was killed.");
78 }
79
80 assertTrue(watchdog.killedProcess(), "The process was killed by the watchdog");
81 assertTrue(duration < 9000, () -> "Skipping the Thread.join() did not work, duration=" + duration);
82 }
83
84
85
86
87
88
89
90
91
92 @Test
93 public void testExec41WithStreams() throws Exception {
94
95 CommandLine cmdLine;
96
97 if (OS.isFamilyWindows()) {
98 cmdLine = CommandLine.parse("ping.exe -n 10 -w 1000 127.0.0.1");
99 } else if ("HP-UX".equals(System.getProperty("os.name"))) {
100
101 cmdLine = CommandLine.parse("ping 127.0.0.1 -n 10");
102 } else if (OS.isFamilyUnix()) {
103 cmdLine = CommandLine.parse("ping -c 10 127.0.0.1");
104 } else {
105 System.err.println("The test 'testExec41WithStreams' does not support the following OS : " + System.getProperty("os.name"));
106 return;
107 }
108
109 final DefaultExecutor executor = DefaultExecutor.builder().get();
110 final ExecuteWatchdog watchdog = new ExecuteWatchdog(2 * 1000);
111 final PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(System.out, System.err);
112
113
114
115 executor.setWatchdog(watchdog);
116 executor.setStreamHandler(pumpStreamHandler);
117
118 final long startTime = System.currentTimeMillis();
119
120 try {
121 executor.execute(cmdLine);
122 } catch (final ExecuteException e) {
123
124 }
125
126 final long duration = System.currentTimeMillis() - startTime;
127
128 System.out.println("Process completed in " + duration + " millis; below is its output");
129
130 if (watchdog.killedProcess()) {
131 System.out.println("Process timed out and was killed by watchdog.");
132 }
133
134 assertTrue(watchdog.killedProcess(), "The process was killed by the watchdog");
135 assertTrue(duration < 9000, "Skipping the Thread.join() did not work");
136 }
137 }