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.assertTrue;
22  
23  import java.io.File;
24  
25  import org.apache.commons.exec.CommandLine;
26  import org.apache.commons.exec.DefaultExecuteResultHandler;
27  import org.apache.commons.exec.DefaultExecutor;
28  import org.apache.commons.exec.ExecuteWatchdog;
29  import org.apache.commons.exec.Executor;
30  import org.apache.commons.exec.TestUtil;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Test EXEC-44 (https://issues.apache.org/jira/browse/EXEC-44).
35   */
36  public class Exec44Test {
37  
38      private final Executor exec = DefaultExecutor.builder().get();
39      private final File testDir = new File("src/test/scripts");
40      private final File foreverTestScript = TestUtil.resolveScriptForOS(testDir + "/forever");
41  
42      /**
43       *
44       * Because the ExecuteWatchdog is the only way to destroy asynchronous processes, it should be possible to set it to an infinite timeout, for processes
45       * which should not timeout, but manually destroyed under some circumstances.
46       *
47       * @throws Exception the test failed
48       */
49      @Test
50      public void testExec44() throws Exception {
51  
52          final CommandLine cl = new CommandLine(foreverTestScript);
53          final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
54          final ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
55  
56          exec.setWatchdog(watchdog);
57          exec.execute(cl, resultHandler);
58  
59          // wait for script to run
60          Thread.sleep(5000);
61          assertTrue(watchdog.isWatching(), "The watchdog is watching the process");
62  
63          // terminate it
64          watchdog.destroyProcess();
65          assertTrue(watchdog.killedProcess(), "The watchdog has killed the process");
66          assertFalse(watchdog.isWatching(), "The watchdog is no longer watching any process");
67      }
68  }