1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.exec.issues;
19
20 import java.io.File;
21 import java.io.OutputStream;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26
27 import org.apache.commons.exec.CommandLine;
28 import org.apache.commons.exec.DefaultExecutor;
29 import org.apache.commons.exec.ExecuteWatchdog;
30 import org.apache.commons.exec.OS;
31 import org.apache.commons.exec.PumpStreamHandler;
32 import org.apache.commons.exec.TestUtil;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Disabled;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.Timeout;
38
39
40
41
42 public class Exec62Test {
43 private Path outputFile;
44
45 private void execute(final String scriptName) throws Exception {
46 final ExecuteWatchdog watchdog = new ExecuteWatchdog(4000);
47 final CommandLine commandLine = new CommandLine("/bin/sh");
48 final File testScript = TestUtil.resolveScriptForOS("./src/test/scripts/issues/" + scriptName);
49
50 commandLine.addArgument(testScript.getAbsolutePath());
51
52 final DefaultExecutor executor = DefaultExecutor.builder().get();
53 executor.setExitValues(null);
54 executor.setWatchdog(watchdog);
55
56 try (OutputStream fos = Files.newOutputStream(outputFile)) {
57 final PumpStreamHandler streamHandler = new PumpStreamHandler(fos);
58 executor.setStreamHandler(streamHandler);
59 executor.execute(commandLine);
60
61 if (watchdog.killedProcess()) {
62 throw new TimeoutException(String.format("Transcode process was killed on timeout %1$s ms, command line %2$s", 4000, commandLine.toString()));
63 }
64 }
65 }
66
67 @BeforeEach
68 public void setUp() throws Exception {
69 outputFile = Files.createTempFile("foo", ".log");
70 }
71
72 @AfterEach
73 public void tearDown() throws Exception {
74 Files.delete(outputFile);
75 }
76
77 @Disabled("Test behaves differently between macOS X and Linux - don't know why")
78 @Test
79 @Timeout(value = 10, unit = TimeUnit.SECONDS)
80 public void testMe() throws Exception {
81 if (OS.isFamilyUnix()) {
82 execute("exec-62");
83 }
84 }
85 }