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