|
4 | 4 | import io.github.easy4j.opencli.browser.OpenCliBrowserClient; |
5 | 5 | import io.github.easy4j.opencli.browser.OpenCliBrowserSession; |
6 | 6 | import io.github.easy4j.opencli.browser.OpenCliBrowserSession.OpenCliBrowserNetworkOptions; |
| 7 | +import io.github.easy4j.opencli.core.OpenCliExecutionDetails; |
7 | 8 | import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason; |
8 | 9 | import io.github.easy4j.opencli.core.OpenCliExecutor; |
| 10 | +import io.github.easy4j.opencli.core.OpenCliProcessRuntime; |
9 | 11 | import io.github.easy4j.opencli.core.OpenCliResult; |
10 | 12 | import io.github.easy4j.opencli.exception.OpenCliException; |
11 | 13 | import io.github.easy4j.opencli.exception.OpenCliNonZeroExitException; |
@@ -321,6 +323,71 @@ void explicitSharedRuntimeLimitsBothClients() throws Exception { |
321 | 323 | } |
322 | 324 | } |
323 | 325 |
|
| 326 | + @Test |
| 327 | + void sharedRuntimeBoundsConcurrentChildrenAndCombinedCapture() throws Exception { |
| 328 | + int stdoutLimit = 131_073; |
| 329 | + int stderrLimit = 65_539; |
| 330 | + OpenCliProcessRuntime runtime = new OpenCliProcessRuntime(2); |
| 331 | + OpenCliProperties capture = properties(6); |
| 332 | + capture.setMaxStdoutBytes(stdoutLimit); |
| 333 | + capture.setMaxStderrBytes(stderrLimit); |
| 334 | + OpenCliProperties queued = properties(6); |
| 335 | + queued.setCommandTimeoutMillis(750L); |
| 336 | + OpenCliExecutor first = new OpenCliExecutor(capture, runtime); |
| 337 | + OpenCliExecutor second = new OpenCliExecutor(capture, runtime); |
| 338 | + OpenCliExecutor waiting = new OpenCliExecutor(queued, runtime); |
| 339 | + Path gate = dir.resolve("capture-release"); |
| 340 | + List<Path> markers = Arrays.asList(dir.resolve("capture-first"), dir.resolve("capture-second")); |
| 341 | + ExecutorService workers = Executors.newFixedThreadPool(6); |
| 342 | + try { |
| 343 | + Future<OpenCliResult> one = workers.submit(() -> first.invoke("capture-hold", |
| 344 | + markers.get(0).toString(), gate.toString(), Integer.toString(stdoutLimit), |
| 345 | + Integer.toString(stderrLimit))); |
| 346 | + Future<OpenCliResult> two = workers.submit(() -> second.invoke("capture-hold", |
| 347 | + markers.get(1).toString(), gate.toString(), Integer.toString(stdoutLimit), |
| 348 | + Integer.toString(stderrLimit))); |
| 349 | + assertTrue(awaitFile(markers.get(0), 5_000L)); |
| 350 | + assertTrue(awaitFile(markers.get(1), 5_000L)); |
| 351 | + |
| 352 | + List<Future<?>> contenders = new ArrayList<>(); |
| 353 | + for (int index = 0; index < 4; index++) { |
| 354 | + Path marker = dir.resolve("queued-" + index); |
| 355 | + contenders.add(workers.submit(() -> { |
| 356 | + OpenCliTimeoutException timeout = assertThrows(OpenCliTimeoutException.class, |
| 357 | + () -> waiting.invoke("write", marker.toString())); |
| 358 | + OpenCliExecutionDetails evidence = timeout.getPartialResult().getExecutionDetails(); |
| 359 | + assertEquals(TerminationReason.QUEUE_TIMEOUT, evidence.getTerminationReason()); |
| 360 | + assertFalse(evidence.isProcessStarted()); |
| 361 | + assertFalse(Files.exists(marker), "a queued invocation spawned a child"); |
| 362 | + })); |
| 363 | + } |
| 364 | + for (Future<?> contender : contenders) { |
| 365 | + contender.get(4, TimeUnit.SECONDS); |
| 366 | + } |
| 367 | + assertFalse(one.isDone()); |
| 368 | + assertFalse(two.isDone()); |
| 369 | + |
| 370 | + release(gate); |
| 371 | + long aggregateBytes = 0; |
| 372 | + for (Future<OpenCliResult> active : Arrays.asList(one, two)) { |
| 373 | + OpenCliResult result = active.get(5, TimeUnit.SECONDS); |
| 374 | + assertTrue(result.isSuccess()); |
| 375 | + OpenCliExecutionDetails evidence = result.getExecutionDetails(); |
| 376 | + assertEquals(stdoutLimit, evidence.getStdoutCapturedBytes()); |
| 377 | + assertEquals(stderrLimit, evidence.getStderrCapturedBytes()); |
| 378 | + assertFalse(evidence.isStdoutTruncated()); |
| 379 | + assertFalse(evidence.isStderrTruncated()); |
| 380 | + assertEquals(OpenCliExecutionDetails.CleanupState.ROOT_EXIT_CONFIRMED, evidence.getCleanupState()); |
| 381 | + aggregateBytes += evidence.getStdoutCapturedBytes() + evidence.getStderrCapturedBytes(); |
| 382 | + } |
| 383 | + assertEquals(393_224L, aggregateBytes); |
| 384 | + } finally { |
| 385 | + release(gate); |
| 386 | + workers.shutdownNow(); |
| 387 | + assertTrue(workers.awaitTermination(5, TimeUnit.SECONDS)); |
| 388 | + } |
| 389 | + } |
| 390 | + |
324 | 391 | @Test |
325 | 392 | void preCancelledRequestNeverStartsAProcess() throws Exception { |
326 | 393 | Class<?> tokenType = assertDoesNotThrow(() -> Class.forName("io.github.easy4j.opencli.core.OpenCliCancellationToken")); |
|
0 commit comments