Skip to content

Commit 9289e50

Browse files
authored
Feature/process utils jdk 8 (#344)
* test: Add tests around ProcessUtils.waitForWithTimeout * feat: Upgrade ProcessUtils to use jdk 8 use waitFor(long timeout, TimeUnit unit)
1 parent 813640f commit 9289e50

File tree

4 files changed

+73
-39
lines changed

4 files changed

+73
-39
lines changed

src/main/java/net/bramp/ffmpeg/FFcommon.java

-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ protected BufferedReader wrapErrorInReader(Process p) {
7272

7373
protected void throwOnError(Process p) throws IOException {
7474
try {
75-
// TODO In java 8 use waitFor(long timeout, TimeUnit unit)
7675
if (ProcessUtils.waitForWithTimeout(p, 1, TimeUnit.SECONDS) != 0) {
7776
// TODO Parse the error
7877
throw new IOException(path + " returned non-zero exit status. Check stdout.");
@@ -84,7 +83,6 @@ protected void throwOnError(Process p) throws IOException {
8483

8584
protected void throwOnError(Process p, FFmpegProbeResult result) throws IOException {
8685
try {
87-
// TODO In java 8 use waitFor(long timeout, TimeUnit unit)
8886
if (ProcessUtils.waitForWithTimeout(p, 1, TimeUnit.SECONDS) != 0) {
8987
// TODO Parse the error
9088
final FFmpegError ffmpegError = null == result ? null : result.getError();

src/main/java/net/bramp/ffmpeg/io/ProcessUtils.java

+3-35
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,10 @@
99
* @author bramp
1010
*/
1111
public final class ProcessUtils {
12-
1312
private ProcessUtils() {
1413
throw new AssertionError("No instances for you!");
1514
}
1615

17-
private static class ProcessThread extends Thread {
18-
final Process p;
19-
boolean finished = false;
20-
int exitValue = -1;
21-
22-
private ProcessThread(Process p) {
23-
this.p = p;
24-
}
25-
26-
@Override
27-
public void run() {
28-
try {
29-
exitValue = p.waitFor();
30-
finished = true;
31-
} catch (InterruptedException e) {
32-
Thread.currentThread().interrupt();
33-
}
34-
}
35-
36-
public boolean hasFinished() {
37-
return finished;
38-
}
39-
40-
public int exitValue() {
41-
return exitValue;
42-
}
43-
}
44-
4516
/**
4617
* Waits until a process finishes or a timeout occurs
4718
*
@@ -54,20 +25,17 @@ public int exitValue() {
5425
public static int waitForWithTimeout(final Process p, long timeout, TimeUnit unit)
5526
throws TimeoutException {
5627

57-
ProcessThread t = new ProcessThread(p);
58-
t.start();
5928
try {
60-
unit.timedJoin(t, timeout);
29+
p.waitFor(timeout, unit);
6130

6231
} catch (InterruptedException e) {
63-
t.interrupt();
6432
Thread.currentThread().interrupt();
6533
}
6634

67-
if (!t.hasFinished()) {
35+
if (p.isAlive()) {
6836
throw new TimeoutException("Process did not finish within timeout");
6937
}
7038

71-
return t.exitValue();
39+
return p.exitValue();
7240
}
7341
}

src/test/java/net/bramp/ffmpeg/FFprobeTest.java

+33-2
Original file line numberDiff line numberDiff line change
@@ -389,15 +389,46 @@ public void testProbeDivideByZero() throws IOException {
389389
}
390390

391391
@Test
392-
public void shouldThrowOnErrorWithFFmpegProbeResult() throws InterruptedException {
393-
Mockito.when(mockProcess.waitFor()).thenReturn(-1);
392+
public void shouldThrowOnErrorWithFFmpegProbeResult() {
393+
Mockito.doReturn(1).when(mockProcess).exitValue();
394+
394395
final FFmpegError error = new FFmpegError();
395396
final FFmpegProbeResult result = new FFmpegProbeResult();
396397
result.error = error;
397398
FFmpegException e = assertThrows(FFmpegException.class, () -> ffprobe.throwOnError(mockProcess, result));
398399
assertEquals(error, e.getError());
399400
}
400401

402+
@Test
403+
public void shouldThrowOnErrorEvenIfProbeResultHasNoError() {
404+
Mockito.doReturn(1).when(mockProcess).exitValue();
405+
406+
final FFmpegProbeResult result = new FFmpegProbeResult();
407+
FFmpegException e = assertThrows(FFmpegException.class, () -> ffprobe.throwOnError(mockProcess, result));
408+
assertNull(e.getError());
409+
}
410+
411+
@Test
412+
public void shouldThrowOnErrorEvenIfProbeResultIsNull() {
413+
Mockito.doReturn(1).when(mockProcess).exitValue();
414+
415+
FFmpegException e = assertThrows(FFmpegException.class, () -> ffprobe.throwOnError(mockProcess, null));
416+
assertNull(e.getError());
417+
}
418+
419+
@Test
420+
public void testShouldThrowErrorWithoutMock() throws IOException {
421+
FFprobe probe = new FFprobe();
422+
FFmpegException e = assertThrows(FFmpegException.class, () -> probe.probe("doesnotexist.mp4"));
423+
424+
assertNotNull(e);
425+
assertNotNull(e.getError());
426+
427+
// Intentionally not comparing the values, as those might change for different ffmpeg versions
428+
assertNotNull(e.getError().getString());
429+
assertNotEquals(0, e.getError().getCode());
430+
}
431+
401432
@Test
402433
public void testProbeSideDataList() throws IOException {
403434
FFmpegProbeResult info = ffprobe.probe(Samples.side_data_list);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.bramp.ffmpeg.io;
2+
3+
import net.bramp.ffmpeg.FFmpeg;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.concurrent.TimeoutException;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertThrows;
12+
13+
public class ProcessUtilsTest {
14+
@Test
15+
public void testProcessFinishesBeforeTimeout() throws Exception {
16+
String ffmpeg = new FFmpeg().getPath();
17+
ProcessBuilder processBuilder = new ProcessBuilder(ffmpeg, "-y", "-v", "quiet", "-f", "lavfi", "-i", "testsrc=duration=1:size=1280x720:rate=10", "-c:v", "libx264", "-t", "1", "output.mp4");
18+
Process process = processBuilder.start();
19+
20+
int exitValue = ProcessUtils.waitForWithTimeout(process, 5, TimeUnit.SECONDS);
21+
22+
assertEquals(0, exitValue);
23+
}
24+
25+
@Test
26+
public void testProcessDoesNotFinishBeforeTimeout() throws IOException {
27+
String ffmpeg = new FFmpeg().getPath();
28+
ProcessBuilder processBuilder = new ProcessBuilder(ffmpeg, "-y", "-v", "quiet", "-f", "lavfi", "-i", "testsrc=duration=10:size=1280x720:rate=30", "-c:v", "libx264", "-t", "10", "output.mp4");
29+
Process process = processBuilder.start();
30+
31+
assertThrows(TimeoutException.class, () -> {
32+
ProcessUtils.waitForWithTimeout(process, 1, TimeUnit.MILLISECONDS);
33+
});
34+
35+
process.destroy();
36+
}
37+
}

0 commit comments

Comments
 (0)