|
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.OpenCliCancellationToken; |
7 | 8 | import io.github.easy4j.opencli.core.OpenCliExecutionDetails; |
8 | 9 | import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason; |
9 | 10 | import io.github.easy4j.opencli.core.OpenCliExecutor; |
@@ -388,6 +389,44 @@ void sharedRuntimeBoundsConcurrentChildrenAndCombinedCapture() throws Exception |
388 | 389 | } |
389 | 390 | } |
390 | 391 |
|
| 392 | + @Test |
| 393 | + void cancelledParentWithInheritedPipesReturnsWithinCleanupBudget() throws Exception { |
| 394 | + Path parent = dir.resolve("parent"); |
| 395 | + Path descendant = dir.resolve("descendant"); |
| 396 | + Path release = dir.resolve("release"); |
| 397 | + OpenCliCancellationToken token = new OpenCliCancellationToken(); |
| 398 | + OpenCliProperties properties = properties(1); |
| 399 | + properties.setCleanupGraceMillis(250L); |
| 400 | + OpenCliExecutor executor = new OpenCliExecutor(properties); |
| 401 | + ExecutorService worker = Executors.newSingleThreadExecutor(); |
| 402 | + try { |
| 403 | + Future<OpenCliException> failure = worker.submit(() -> assertThrows(OpenCliException.class, |
| 404 | + () -> executor.invoke(Arrays.asList("inherited-pipes", parent.toString(), |
| 405 | + release.toString(), descendant.toString()), token))); |
| 406 | + assertTrue(awaitFile(descendant, 5_000L), "descendant never inherited the pipes"); |
| 407 | + token.cancel(); |
| 408 | + OpenCliExecutionDetails details = failure.get(1_500L, TimeUnit.MILLISECONDS).getPartialResult() |
| 409 | + .getExecutionDetails(); |
| 410 | + assertEquals(TerminationReason.CANCELLED, details.getTerminationReason()); |
| 411 | + assertFalse(details.isDescendantsExitConfirmed()); |
| 412 | + if (!details.isStreamsDrained()) { |
| 413 | + assertEquals(OpenCliExecutionDetails.CleanupState.UNCONFIRMED, details.getCleanupState()); |
| 414 | + } |
| 415 | + byte[] firstHeartbeat = Files.readAllBytes(descendant); |
| 416 | + long start = System.nanoTime(); |
| 417 | + while (Arrays.equals(firstHeartbeat, Files.readAllBytes(descendant)) |
| 418 | + && System.nanoTime() - start < TimeUnit.SECONDS.toNanos(1)) { |
| 419 | + Thread.sleep(20L); |
| 420 | + } |
| 421 | + assertFalse(Arrays.equals(firstHeartbeat, Files.readAllBytes(descendant)), |
| 422 | + "the untracked descendant was stopped by cancellation"); |
| 423 | + } finally { |
| 424 | + release(release); |
| 425 | + worker.shutdownNow(); |
| 426 | + assertTrue(worker.awaitTermination(5, TimeUnit.SECONDS)); |
| 427 | + } |
| 428 | + } |
| 429 | + |
391 | 430 | @Test |
392 | 431 | void preCancelledRequestNeverStartsAProcess() throws Exception { |
393 | 432 | Class<?> tokenType = assertDoesNotThrow(() -> Class.forName("io.github.easy4j.opencli.core.OpenCliCancellationToken")); |
|
0 commit comments