|
1 | 1 | package io.github.easy4j.opencli.contract; |
2 | 2 |
|
3 | 3 | import io.github.easy4j.opencli.OpenCliProperties; |
| 4 | +import io.github.easy4j.opencli.browser.OpenCliBrowserClient; |
| 5 | +import io.github.easy4j.opencli.browser.OpenCliBrowserSession; |
| 6 | +import io.github.easy4j.opencli.browser.OpenCliBrowserSession.OpenCliBrowserNetworkOptions; |
| 7 | +import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason; |
4 | 8 | import io.github.easy4j.opencli.core.OpenCliExecutor; |
5 | 9 | import io.github.easy4j.opencli.core.OpenCliResult; |
6 | 10 | import io.github.easy4j.opencli.exception.OpenCliException; |
| 11 | +import io.github.easy4j.opencli.exception.OpenCliNonZeroExitException; |
7 | 12 | import io.github.easy4j.opencli.exception.OpenCliTimeoutException; |
8 | 13 | import java.io.File; |
9 | 14 | import java.nio.charset.StandardCharsets; |
@@ -64,6 +69,109 @@ private static Object getter(Object object, String name) { |
64 | 69 |
|
65 | 70 | private static Object details(OpenCliResult result) { return getter(result, "getExecutionDetails"); } |
66 | 71 |
|
| 72 | + private OpenCliBrowserSession followSession(OpenCliProperties properties, String mode) { |
| 73 | + properties.getLeadingArguments().addAll(Arrays.asList("network-follow", mode, dir.resolve("follow-ready").toString())); |
| 74 | + return new OpenCliBrowserClient(new OpenCliExecutor(properties)).session("fixture"); |
| 75 | + } |
| 76 | + |
| 77 | + private OpenCliResult follow(OpenCliBrowserSession session) { |
| 78 | + return session.network(OpenCliBrowserNetworkOptions.builder().follow(true).build(), null); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void networkFollowReturnsCapturedEventsAfterNormalExit() { |
| 83 | + OpenCliResult result = follow(followSession(properties(1), "complete")); |
| 84 | + assertTrue(result.isSuccess()); |
| 85 | + assertEquals("event-one\nevent-two\n", result.getStdout()); |
| 86 | + assertEquals(TerminationReason.PROCESS_EXIT, result.getExecutionDetails().getTerminationReason()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void networkFollowStopsAtCaptureLimitWithPartialOutput() { |
| 91 | + OpenCliProperties properties = properties(1); |
| 92 | + properties.setMaxStdoutBytes(32); |
| 93 | + OpenCliBrowserSession session = followSession(properties, "overflow"); |
| 94 | + OpenCliException failure = assertThrows(OpenCliException.class, () -> follow(session)); |
| 95 | + OpenCliResult partial = failure.getPartialResult(); |
| 96 | + assertEquals(TerminationReason.OUTPUT_LIMIT, partial.getExecutionDetails().getTerminationReason()); |
| 97 | + assertEquals(32L, partial.getExecutionDetails().getStdoutCapturedBytes()); |
| 98 | + assertTrue(partial.getStdout().startsWith("event-one\n")); |
| 99 | + assertFalse(partial.isSuccess()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void networkFollowReportsTheCliExitCode() { |
| 104 | + OpenCliBrowserSession session = followSession(properties(1), "failed"); |
| 105 | + OpenCliNonZeroExitException failure = assertThrows(OpenCliNonZeroExitException.class, () -> follow(session)); |
| 106 | + OpenCliResult partial = failure.getPartialResult(); |
| 107 | + assertEquals(Integer.valueOf(23), partial.getExitCode()); |
| 108 | + assertEquals("fixture failed", partial.getStderr()); |
| 109 | + assertEquals(TerminationReason.PROCESS_EXIT, partial.getExecutionDetails().getTerminationReason()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void networkFollowRejectsDisabledDeadlineBeforeSpawning() { |
| 114 | + OpenCliProperties properties = properties(1); |
| 115 | + properties.setCommandTimeoutMillis(0L); |
| 116 | + OpenCliBrowserSession session = followSession(properties, "wait"); |
| 117 | + IllegalStateException failure = assertThrows(IllegalStateException.class, () -> follow(session)); |
| 118 | + assertEquals("opencli.command-timeout-millis must be positive", failure.getMessage()); |
| 119 | + assertFalse(Files.exists(dir.resolve("follow-ready"))); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void networkFollowTimeoutLeavesAnIndependentProcessRunning() throws Exception { |
| 124 | + OpenCliExecutor independent = new OpenCliExecutor(properties(1)); |
| 125 | + Path started = dir.resolve("independent-started"); |
| 126 | + Path gate = dir.resolve("independent-release"); |
| 127 | + ExecutorService worker = Executors.newSingleThreadExecutor(); |
| 128 | + try { |
| 129 | + Future<OpenCliResult> other = worker.submit(() -> independent.invoke("hold", started.toString(), gate.toString())); |
| 130 | + assertTrue(awaitFile(started, 5000L)); |
| 131 | + |
| 132 | + OpenCliProperties properties = properties(1); |
| 133 | + properties.setCommandTimeoutMillis(2500L); |
| 134 | + OpenCliBrowserSession session = followSession(properties, "wait"); |
| 135 | + OpenCliTimeoutException timeout = assertThrows(OpenCliTimeoutException.class, () -> follow(session)); |
| 136 | + OpenCliResult partial = timeout.getPartialResult(); |
| 137 | + assertEquals(TerminationReason.EXECUTION_TIMEOUT, partial.getExecutionDetails().getTerminationReason()); |
| 138 | + assertEquals("event-one\n", partial.getStdout()); |
| 139 | + |
| 140 | + assertFalse(other.isDone(), "timeout must leave independent invocation running"); |
| 141 | + release(gate); |
| 142 | + assertTrue(other.get(5, TimeUnit.SECONDS).isSuccess()); |
| 143 | + } finally { |
| 144 | + release(gate); |
| 145 | + worker.shutdownNow(); |
| 146 | + assertTrue(worker.awaitTermination(5, TimeUnit.SECONDS)); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void networkFollowPreservesCancellationAfterAnEvent() throws Exception { |
| 152 | + OpenCliBrowserSession session = followSession(properties(1), "wait"); |
| 153 | + AtomicReference<OpenCliResult> partial = new AtomicReference<>(); |
| 154 | + Thread caller = new Thread(() -> { |
| 155 | + try { |
| 156 | + follow(session); |
| 157 | + } catch (OpenCliException failure) { |
| 158 | + partial.set(failure.getPartialResult()); |
| 159 | + } |
| 160 | + }, "network-follow-caller"); |
| 161 | + try { |
| 162 | + caller.start(); |
| 163 | + assertTrue(awaitFile(dir.resolve("follow-ready"), 5000L)); |
| 164 | + caller.interrupt(); |
| 165 | + caller.join(5000L); |
| 166 | + assertFalse(caller.isAlive()); |
| 167 | + assertNotNull(partial.get()); |
| 168 | + assertEquals(TerminationReason.CANCELLED, partial.get().getExecutionDetails().getTerminationReason()); |
| 169 | + } finally { |
| 170 | + caller.interrupt(); |
| 171 | + caller.join(5000L); |
| 172 | + } |
| 173 | + } |
| 174 | + |
67 | 175 | @Test |
68 | 176 | void anotherClientCannotReplaceAnActiveClientsLimiter() throws Exception { |
69 | 177 | OpenCliExecutor a = new OpenCliExecutor(properties(1)); |
|
0 commit comments