|
| 1 | +package io.github.easy4j.opencli.contract; |
| 2 | + |
| 3 | +import io.github.easy4j.opencli.OpenCliProperties; |
| 4 | +import io.github.easy4j.opencli.core.OpenCliCancellationToken; |
| 5 | +import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason; |
| 6 | +import io.github.easy4j.opencli.core.OpenCliExecutor; |
| 7 | +import io.github.easy4j.opencli.core.OpenCliResult; |
| 8 | +import io.github.easy4j.opencli.exception.OpenCliException; |
| 9 | +import io.github.easy4j.opencli.exception.OpenCliTimeoutException; |
| 10 | +import java.io.File; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | +import java.util.concurrent.ExecutionException; |
| 18 | +import java.util.concurrent.ExecutorService; |
| 19 | +import java.util.concurrent.Executors; |
| 20 | +import java.util.concurrent.Future; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.Timeout; |
| 24 | +import org.junit.jupiter.api.io.TempDir; |
| 25 | +import static org.junit.jupiter.api.Assertions.*; |
| 26 | + |
| 27 | +/** Additional finite-budget and immutable-submission regression vectors. */ |
| 28 | +@Timeout(20) |
| 29 | +class OpenCliProcessBoundaryTest { |
| 30 | + @TempDir Path dir; |
| 31 | + |
| 32 | + private static OpenCliProperties properties() { |
| 33 | + OpenCliProperties p = new OpenCliProperties(); |
| 34 | + String exe = System.getProperty("os.name").startsWith("Windows") ? "java.exe" : "java"; |
| 35 | + p.setExecutable(new File(new File(System.getProperty("java.home"), "bin"), exe).getAbsolutePath()); |
| 36 | + p.setLeadingArguments(new ArrayList<>(Arrays.asList("-cp", |
| 37 | + System.getProperty("surefire.test.class.path", System.getProperty("java.class.path")), |
| 38 | + LifecycleProbe.class.getName()))); |
| 39 | + p.setCommandTimeoutMillis(10000L); |
| 40 | + p.setMaxConcurrentExecutions(1); |
| 41 | + return p; |
| 42 | + } |
| 43 | + |
| 44 | + private static void await(Path path) throws Exception { |
| 45 | + long started = System.nanoTime(); |
| 46 | + while (!Files.exists(path) && System.nanoTime() - started < TimeUnit.SECONDS.toNanos(3L)) { Thread.sleep(10L); } |
| 47 | + assertTrue(Files.exists(path), "fixture did not start"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void utf8TruncationReportsBytesNotReencodedCharacters() { |
| 52 | + OpenCliProperties p = properties(); |
| 53 | + p.setMaxStdoutBytes(4); |
| 54 | + OpenCliException failure = assertThrows(OpenCliException.class, () -> new OpenCliExecutor(p).invoke("utf8")); |
| 55 | + OpenCliResult partial = failure.getPartialResult(); |
| 56 | + assertNotNull(partial); |
| 57 | + assertEquals(TerminationReason.OUTPUT_LIMIT, partial.getExecutionDetails().getTerminationReason()); |
| 58 | + assertEquals(4L, partial.getExecutionDetails().getStdoutCapturedBytes()); |
| 59 | + assertEquals(6L, partial.getExecutionDetails().getStdoutObservedBytes()); |
| 60 | + assertEquals("中\uFFFD", partial.getStdout()); |
| 61 | + assertTrue(partial.getExecutionDetails().isStdoutTruncated()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void invalidBudgetsFailBeforeChildCreation() { |
| 66 | + for (int choice = 0; choice < 5; choice++) { |
| 67 | + OpenCliProperties p = properties(); |
| 68 | + if (choice == 0) { p.setMaxStdoutBytes(0); } |
| 69 | + if (choice == 1) { p.setMaxStderrBytes(-1); } |
| 70 | + if (choice == 2) { p.setCleanupGraceMillis(0); } |
| 71 | + if (choice == 3) { p.setCommandTimeoutMillis(Long.MAX_VALUE); } |
| 72 | + if (choice == 4) { p.setCleanupGraceMillis(Long.MAX_VALUE); } |
| 73 | + Path marker = dir.resolve("invalid-" + choice); |
| 74 | + assertThrows(IllegalArgumentException.class, () -> new OpenCliExecutor(p).invoke("write", marker.toString())); |
| 75 | + assertFalse(Files.exists(marker)); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void explicitCancellationOfRunningChildRetainsBoundedEvidence() throws Exception { |
| 81 | + OpenCliExecutor executor = new OpenCliExecutor(properties()); |
| 82 | + OpenCliCancellationToken token = new OpenCliCancellationToken(); |
| 83 | + Path heartbeat = dir.resolve("heartbeat"); |
| 84 | + Path release = dir.resolve("release"); |
| 85 | + ExecutorService worker = Executors.newSingleThreadExecutor(); |
| 86 | + try { |
| 87 | + Future<OpenCliResult> future = worker.submit(() -> executor.invoke( |
| 88 | + Arrays.asList("heartbeat", heartbeat.toString(), release.toString()), token)); |
| 89 | + await(heartbeat); |
| 90 | + token.cancel(); |
| 91 | + ExecutionException failed = assertThrows(ExecutionException.class, () -> future.get(3, TimeUnit.SECONDS)); |
| 92 | + assertTrue(failed.getCause() instanceof OpenCliException); |
| 93 | + OpenCliResult partial = ((OpenCliException) failed.getCause()).getPartialResult(); |
| 94 | + assertEquals(TerminationReason.CANCELLED, partial.getExecutionDetails().getTerminationReason()); |
| 95 | + assertTrue(partial.getExecutionDetails().isProcessStarted()); |
| 96 | + assertFalse(partial.getExecutionDetails().isDescendantsExitConfirmed()); |
| 97 | + String stopped = new String(Files.readAllBytes(heartbeat), StandardCharsets.UTF_8); |
| 98 | + Thread.sleep(100L); |
| 99 | + assertEquals(stopped, new String(Files.readAllBytes(heartbeat), StandardCharsets.UTF_8)); |
| 100 | + assertTrue(executor.invoke("write", dir.resolve("next").toString()).isSuccess()); |
| 101 | + } finally { |
| 102 | + Files.write(release, new byte[]{1}); |
| 103 | + worker.shutdownNow(); |
| 104 | + assertTrue(worker.awaitTermination(5, TimeUnit.SECONDS)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void queuedTokenCancellationDoesNotStartTheWaitingChild() throws Exception { |
| 110 | + OpenCliExecutor executor = new OpenCliExecutor(properties()); |
| 111 | + Path first = dir.resolve("first"); |
| 112 | + Path second = dir.resolve("second"); |
| 113 | + Path gate = dir.resolve("release"); |
| 114 | + OpenCliCancellationToken token = new OpenCliCancellationToken(); |
| 115 | + ExecutorService workers = Executors.newFixedThreadPool(2); |
| 116 | + try { |
| 117 | + Future<OpenCliResult> one = workers.submit(() -> executor.invoke("hold", first.toString(), gate.toString())); |
| 118 | + await(first); |
| 119 | + Future<OpenCliResult> two = workers.submit(() -> executor.invoke(Arrays.asList("write", second.toString()), token)); |
| 120 | + Thread.sleep(100L); |
| 121 | + token.cancel(); |
| 122 | + ExecutionException failed = assertThrows(ExecutionException.class, () -> two.get(2, TimeUnit.SECONDS)); |
| 123 | + OpenCliResult partial = ((OpenCliException) failed.getCause()).getPartialResult(); |
| 124 | + assertEquals(TerminationReason.CANCELLED, partial.getExecutionDetails().getTerminationReason()); |
| 125 | + assertFalse(partial.getExecutionDetails().isProcessStarted()); |
| 126 | + assertNull(partial.getExitCode()); |
| 127 | + assertFalse(Files.exists(second)); |
| 128 | + Files.write(gate, new byte[]{1}); |
| 129 | + assertTrue(one.get(3, TimeUnit.SECONDS).isSuccess()); |
| 130 | + } finally { |
| 131 | + Files.write(gate, new byte[]{1}); |
| 132 | + workers.shutdownNow(); |
| 133 | + assertTrue(workers.awaitTermination(5, TimeUnit.SECONDS)); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void timeoutAndCaptureConfigurationAreCopiedForReverseWorkers() { |
| 139 | + OpenCliProperties p = properties(); |
| 140 | + p.setMaxStdoutBytes(123); |
| 141 | + p.setMaxStderrBytes(456); |
| 142 | + p.setCleanupGraceMillis(789); |
| 143 | + OpenCliProperties copy = p.copyForLocalCliExecution(); |
| 144 | + assertEquals(123, copy.getMaxStdoutBytes()); |
| 145 | + assertEquals(456, copy.getMaxStderrBytes()); |
| 146 | + assertEquals(789L, copy.getCleanupGraceMillis()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void repeatedExecutionTimeoutsDoNotBecomeNonzeroOrIoFailures() { |
| 151 | + OpenCliProperties p = properties(); |
| 152 | + p.setCommandTimeoutMillis(100L); |
| 153 | + OpenCliExecutor executor = new OpenCliExecutor(p); |
| 154 | + for (int i = 0; i < 5; i++) { |
| 155 | + Path marker = dir.resolve("timeout-" + i); |
| 156 | + OpenCliTimeoutException failure = assertThrows(OpenCliTimeoutException.class, |
| 157 | + () -> executor.invoke("hold", marker.toString(), dir.resolve("never-release").toString())); |
| 158 | + assertEquals(TerminationReason.EXECUTION_TIMEOUT, failure.getPartialResult().getExecutionDetails().getTerminationReason()); |
| 159 | + assertFalse(executor.getProcessRuntime().isQuarantined()); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments