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.AbstractExecTest;
25 import org.apache.commons.exec.CommandLine;
26 import org.apache.commons.exec.DefaultExecutor;
27 import org.apache.commons.exec.ExecuteException;
28 import org.apache.commons.exec.ExecuteWatchdog;
29 import org.apache.commons.exec.Executor;
30 import org.junit.jupiter.api.Disabled;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36 public class Exec60Test extends AbstractExecTest {
37
38 private final Executor exec = DefaultExecutor.builder().get();
39 private final File pingScript = resolveTestScript("ping");
40
41
42
43
44
45 @Disabled("The test is fragile and might fail out of the blue")
46 @Test
47 public void testExec_60() throws Exception {
48
49 final int start = 0;
50 final int seconds = 1;
51 final int offsetMultiplier = 1;
52 final int maxRetries = 180;
53 int processTerminatedCounter = 0;
54 int watchdogKilledProcessCounter = 0;
55 final CommandLine cmdLine = new CommandLine(pingScript);
56 cmdLine.addArgument(Integer.toString(seconds + 1));
57
58 final long startTime = System.currentTimeMillis();
59 for (int offset = start; offset <= maxRetries; offset++) {
60
61
62
63
64 final ExecuteWatchdog watchdog = new ExecuteWatchdog(seconds * 1000 + offset * offsetMultiplier);
65 exec.setWatchdog(watchdog);
66 try {
67 exec.execute(cmdLine);
68 processTerminatedCounter++;
69
70 if (processTerminatedCounter > 5) {
71 break;
72 }
73 } catch (final ExecuteException ex) {
74
75 assertTrue(watchdog.killedProcess(), "Watchdog killed the process");
76 watchdogKilledProcessCounter++;
77 }
78 }
79
80 final long avg = (System.currentTimeMillis() - startTime) / (watchdogKilledProcessCounter + processTerminatedCounter);
81 System.out.println("Processes terminated: " + processTerminatedCounter + " killed: " + watchdogKilledProcessCounter + " Multiplier: " + offsetMultiplier
82 + " MaxRetries: " + maxRetries + " Elapsed (avg ms): " + avg);
83 assertTrue(processTerminatedCounter > 0, "Not a single process terminated on its own");
84 assertTrue(watchdogKilledProcessCounter > 0, "Not a single process was killed by the watch dog");
85 }
86 }