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