View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.exec.issues;
19  
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
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.DefaultExecuteResultHandler;
28  import org.apache.commons.exec.DefaultExecutor;
29  import org.apache.commons.exec.ExecuteWatchdog;
30  import org.apache.commons.exec.Executor;
31  import org.apache.commons.exec.TestUtil;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * EXEC-34 https://issues.apache.org/jira/browse/EXEC-34
36   */
37  public class Exec34Test {
38  
39      private final Executor exec = DefaultExecutor.builder().get();
40      private final File testDir = new File("src/test/scripts");
41      private final File pingScript = TestUtil.resolveScriptForOS(testDir + "/ping");
42  
43      /**
44       *
45       * Race condition prevent watchdog working using ExecuteStreamHandler. The test fails because when watchdog.destroyProcess() is invoked the external process
46       * is not bound to the watchdog yet.
47       *
48       * @throws Exception the test failed
49       */
50      @Test
51      public void testExec34_1() throws Exception {
52  
53          final CommandLine cmdLine = new CommandLine(pingScript);
54          cmdLine.addArgument("10"); // sleep 10 seconds
55  
56          final ExecuteWatchdog watchdog = new ExecuteWatchdog(Integer.MAX_VALUE);
57          final DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
58          exec.setWatchdog(watchdog);
59          exec.execute(cmdLine, handler);
60          assertTrue(watchdog.isWatching());
61          watchdog.destroyProcess();
62          assertTrue(watchdog.killedProcess(), "Watchdog should have killed the process");
63          assertFalse(watchdog.isWatching(), "Watchdog is no longer watching the process");
64      }
65  
66      /**
67       * Some user waited for an asynchronous process using watchdog.isWatching() which is now properly implemented using {@code DefaultExecuteResultHandler}.
68       *
69       * @throws Exception the test failed
70       */
71      @Test
72      public void testExec34_2() throws Exception {
73  
74          final CommandLine cmdLine = new CommandLine(pingScript);
75          cmdLine.addArgument("10"); // sleep 10 seconds
76  
77          final ExecuteWatchdog watchdog = new ExecuteWatchdog(5000);
78          final DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
79          exec.setWatchdog(watchdog);
80          exec.execute(cmdLine, handler);
81          handler.waitFor();
82          assertTrue(handler.hasResult(), "Process has exited");
83          assertNotNull(handler.getException(), "Process was aborted");
84          assertTrue(watchdog.killedProcess(), "Watchdog should have killed the process");
85          assertFalse(watchdog.isWatching(), "Watchdog is no longer watching the process");
86      }
87  }