View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.exec.issues;
21  
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.File;
26  
27  import org.apache.commons.exec.CommandLine;
28  import org.apache.commons.exec.DefaultExecuteResultHandler;
29  import org.apache.commons.exec.DefaultExecutor;
30  import org.apache.commons.exec.ExecuteWatchdog;
31  import org.apache.commons.exec.Executor;
32  import org.apache.commons.exec.TestUtil;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Test EXEC-44 (https://issues.apache.org/jira/browse/EXEC-44).
37   */
38  class Exec44Test {
39  
40      private final Executor exec = DefaultExecutor.builder().get();
41      private final File testDir = new File("src/test/scripts");
42      private final File foreverTestScript = TestUtil.resolveScriptFileForOS(testDir + "/forever");
43  
44      /**
45       *
46       * Because the ExecuteWatchdog is the only way to destroy asynchronous processes, it should be possible to set it to an infinite timeout, for processes
47       * which should not time out, but manually destroyed under some circumstances.
48       *
49       * @throws Exception the test failed
50       */
51      @Test
52      void testExec44() throws Exception {
53  
54          final CommandLine cl = new CommandLine(foreverTestScript);
55          final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
56          final ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
57  
58          exec.setWatchdog(watchdog);
59          exec.execute(cl, resultHandler);
60  
61          // wait for script to run
62          Thread.sleep(5000);
63          assertTrue(watchdog.isWatching(), "The watchdog is watching the process");
64  
65          // terminate it
66          watchdog.destroyProcess();
67          assertTrue(watchdog.killedProcess(), "The watchdog has killed the process");
68          assertFalse(watchdog.isWatching(), "The watchdog is no longer watching any process");
69      }
70  }