|
| 1 | +package io.github.easy4j.opencli.core.support; |
| 2 | + |
| 3 | +import io.github.easy4j.opencli.core.OpenCliCancellationToken; |
| 4 | +import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason; |
| 5 | +import java.io.ByteArrayInputStream; |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.OutputStream; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import org.apache.commons.exec.CommandLine; |
| 12 | +import org.apache.commons.exec.ExecuteWatchdog; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.Timeout; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 19 | + |
| 20 | +/** Verifies that reader-thread I/O faults survive into the terminal result. */ |
| 21 | +@Timeout(5) |
| 22 | +class SubprocessStreamFailureTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + void partialOutputAndReadFailureRemainVisibleAfterRootExit() throws Exception { |
| 26 | + IOException readFailure = new IOException("injected stdout read failure"); |
| 27 | + SubprocessExecutionSupport.Runtime runtime = new SubprocessExecutionSupport.Runtime(1, |
| 28 | + builder -> new ExitedProcess(readFailure), timeout -> new IdleWatchdog()); |
| 29 | + SubprocessExecutionSupport.ExecutionRequest request = new SubprocessExecutionSupport.ExecutionRequest( |
| 30 | + CommandLine.parse("fake"), null, null, 1_000L, 32, 32, 100L, |
| 31 | + System.nanoTime(), new OpenCliCancellationToken()); |
| 32 | + |
| 33 | + SubprocessExecutionSupport.RunSession session = runtime.execute(request); |
| 34 | + |
| 35 | + assertEquals(TerminationReason.IO_FAILURE, |
| 36 | + session.getExecutionDetails().getTerminationReason()); |
| 37 | + assertSame(readFailure, session.getIoFailure()); |
| 38 | + assertEquals("abc", session.getStdout().toString(StandardCharsets.UTF_8.name())); |
| 39 | + assertTrue(session.getExecutionDetails().isStreamsDrained()); |
| 40 | + } |
| 41 | + |
| 42 | + private static final class IdleWatchdog extends ExecuteWatchdog { |
| 43 | + IdleWatchdog() { super(1_000L); } |
| 44 | + |
| 45 | + @Override |
| 46 | + public synchronized void start(Process process) { } |
| 47 | + } |
| 48 | + |
| 49 | + private static final class ExitedProcess extends Process { |
| 50 | + private final InputStream output; |
| 51 | + |
| 52 | + ExitedProcess(IOException readFailure) { |
| 53 | + output = new InputStream() { |
| 54 | + private boolean emitted; |
| 55 | + |
| 56 | + @Override |
| 57 | + public int read() throws IOException { |
| 58 | + throw new UnsupportedOperationException("Reader uses block reads"); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public int read(byte[] bytes, int offset, int length) throws IOException { |
| 63 | + if (emitted) { throw readFailure; } |
| 64 | + byte[] partial = "abc".getBytes(StandardCharsets.UTF_8); |
| 65 | + System.arraycopy(partial, 0, bytes, offset, partial.length); |
| 66 | + emitted = true; |
| 67 | + return partial.length; |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public OutputStream getOutputStream() { return new ByteArrayOutputStream(); } |
| 74 | + |
| 75 | + @Override |
| 76 | + public InputStream getInputStream() { return output; } |
| 77 | + |
| 78 | + @Override |
| 79 | + public InputStream getErrorStream() { return new ByteArrayInputStream(new byte[0]); } |
| 80 | + |
| 81 | + @Override |
| 82 | + public int waitFor() { return 0; } |
| 83 | + |
| 84 | + @Override |
| 85 | + public int exitValue() { return 0; } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void destroy() { } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean isAlive() { return false; } |
| 92 | + } |
| 93 | +} |
0 commit comments