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