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