|
18 | 18 | import io.github.easy4j.codex.CodexClientConfig; |
19 | 19 | import org.junit.jupiter.api.Test; |
20 | 20 |
|
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
21 | 27 | import static org.junit.jupiter.api.Assertions.assertEquals; |
22 | 28 | import static org.junit.jupiter.api.Assertions.assertFalse; |
23 | 29 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
@@ -158,6 +164,37 @@ void shouldReportFailureFromProbeWhenExecutableMissing() { |
158 | 164 | assertFalse(executor.probe()); |
159 | 165 | } |
160 | 166 |
|
| 167 | + @Test |
| 168 | + void shouldUseProbeTimeoutWithoutChangingNormalCommandTimeout() throws Exception { |
| 169 | + Path script = Files.createTempFile("slow-codex-", ".sh"); |
| 170 | + Files.write(script, Arrays.asList( |
| 171 | + "#!/bin/sh", |
| 172 | + "sleep 2", |
| 173 | + "echo codex-test" |
| 174 | + ), StandardCharsets.UTF_8); |
| 175 | + assertTrue(script.toFile().setExecutable(true)); |
| 176 | + |
| 177 | + CodexClientConfig config = configFor(script.toAbsolutePath().toString()); |
| 178 | + config.setLocalProbeTimeoutSeconds(1); |
| 179 | + config.setLocalTimeoutSeconds(5); |
| 180 | + CodexCliExecutor executor = new CodexCliExecutor(config); |
| 181 | + |
| 182 | + long probeStarted = System.nanoTime(); |
| 183 | + assertFalse(executor.probe()); |
| 184 | + long probeElapsedMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - probeStarted); |
| 185 | + assertTrue(probeElapsedMs < 3_500, |
| 186 | + "probe must use localProbeTimeoutSeconds instead of localTimeoutSeconds"); |
| 187 | + |
| 188 | + long executeStarted = System.nanoTime(); |
| 189 | + CodexCliResult normal = executor.execute("--version"); |
| 190 | + long executeElapsedMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - executeStarted); |
| 191 | + assertTrue(normal.isSuccess(), "normal command must still use localTimeoutSeconds"); |
| 192 | + assertTrue(executeElapsedMs >= 1_500, |
| 193 | + "normal command should be allowed to outlive the probe timeout"); |
| 194 | + |
| 195 | + Files.deleteIfExists(script); |
| 196 | + } |
| 197 | + |
161 | 198 | @Test |
162 | 199 | void shouldTimeoutOnHangingProcess() { |
163 | 200 | // Use a short timeout and a command that sleeps for a long time. |
|
0 commit comments