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 java.io.IOException;
21  import java.util.concurrent.TimeUnit;
22  
23  import org.apache.commons.exec.AbstractExecTest;
24  import org.apache.commons.exec.CommandLine;
25  import org.apache.commons.exec.DefaultExecutor;
26  import org.apache.commons.exec.OS;
27  import org.apache.commons.exec.PumpStreamHandler;
28  import org.junit.jupiter.api.Disabled;
29  import org.junit.jupiter.api.Test;
30  import org.junit.jupiter.api.Timeout;
31  
32  /**
33   * Test EXEC-57 (https://issues.apache.org/jira/browse/EXEC-57).
34   */
35  public class Exec57Test extends AbstractExecTest {
36  
37      /**
38       * DefaultExecutor.execute() does not return even if child process terminated - in this case the child process hangs because the grand children is connected
39       * to stdout & stderr and is still running. As work-around a stop timeout is used for the PumpStreamHandler to ensure that the caller does not block forever
40       * but if the stop timeout is exceeded an ExecuteException is thrown to notify the caller. But this case the threads are still around causing a resource
41       * leak.
42       *
43       * @TODO [EXEC-57] Broken for macOS X & Linux
44       */
45      @Disabled("Broken for Unix-based systems")
46      @Test
47      @Timeout(value = TEST_TIMEOUT, unit = TimeUnit.MILLISECONDS)
48      public void testExecutionOfBackgroundProcess() throws IOException {
49  
50          final CommandLine cmdLine = new CommandLine("sh").addArgument("-c").addArgument("./src/test/scripts/issues/exec-57-nohup.sh", false);
51          final DefaultExecutor executor = DefaultExecutor.builder().get();
52          final PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(System.out, System.err);
53  
54          executor.setStreamHandler(pumpStreamHandler);
55  
56          executor.execute(cmdLine);
57      }
58  
59      /**
60       * The same approach using a completely detached process works nicely on macOS X.
61       *
62       * @throws IOException
63       */
64      @Test
65      @Timeout(value = TEST_TIMEOUT, unit = TimeUnit.MILLISECONDS)
66      public void testExecutionOfDetachedProcess() throws IOException {
67  
68          if (!OS.isFamilyUnix()) {
69              testNotSupportedForCurrentOperatingSystem();
70              return;
71          }
72  
73          final CommandLine cmdLine = new CommandLine("sh").addArgument("-c").addArgument("./src/test/scripts/issues/exec-57-detached.sh", false);
74          final DefaultExecutor executor = DefaultExecutor.builder().get();
75          final PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(System.out, System.err);
76  
77          executor.setStreamHandler(pumpStreamHandler);
78  
79          executor.execute(cmdLine);
80      }
81  }