Skip to content

Commit 0340522

Browse files
committed
test: cover cancellation with inherited OpenCLI pipes
1 parent dd4f395 commit 0340522

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎openspec/changes/harden-opencli-process-lifecycle/tasks.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
## 1. 资源回归
88

9-
- [ ] 1.1 用可控子进程分别编写排队超时、并发 Client、取消、超限与 cleanup 竞态失败测试。
9+
- [x] 1.1 用可控子进程分别编写排队超时、并发 Client、取消、超限与 cleanup 竞态失败测试。见本文 OC-PROC-004/005 回归矩阵。
1010
- [x] 1.2 测量在途进程与 capture 峰值;保留失败证据而不运行真实用户网站。见本文 OC-PROC-001/002/003 饱和验证。
1111

1212
## 2. 执行核修复
@@ -73,3 +73,11 @@ JDK 17 使用系统 Maven 执行 `-B -ntp -o clean verify`,2,115 项通过,0
7373
释放后,两次调用均确认根进程退出,四条输出流保留 393,224 字节,恰好达到两个执行槽的合计捕获预算,零截断。这个数值由执行详情测得,对应离线 fixture 的捕获量。JVM 整体堆内存还包含其他分配,本场景不对其计量。
7474

7575
JDK 17 使用系统 Maven 执行 `-B -ntp -o clean verify`,2,116 项通过,0 失败、0 错误、0 跳过。
76+
77+
## 2026-11-18 OC-PROC-004/005 回归矩阵
78+
79+
`OpenCliProcessContractTest` 使用真实子进程覆盖排队超时、独立和共享 Client 限流、取消及独立流超限。`SubprocessCompletionRaceTest` 用同步屏障覆盖输出超限、取消、watchdog 与进程结束竞争,确认唯一结果和许可复用。`SubprocessCleanupFaultTest` 注入 watchdog 初始化、流初始化、终止和清理异常,覆盖最初失败保留及无法确认退出的运行时隔离。
80+
81+
新增真实继承管道回归。根进程启动继承 stdout、stderr 的独立 JVM,后者通过文件汇报心跳。观察到后代进程启动后取消调用。清理宽限配置为 250 ms,测试允许 1,500 ms 等待窗口,包含线程调度。SDK 仍返回 `CANCELLED`,流未排空时必须报告 `UNCONFIRMED`,并且不声称后代已退出。断言取消后后代心跳仍更新,最后由测试自己的释放文件终止它。
82+
83+
JDK 17 使用系统 Maven 执行 `-B -ntp -o clean verify`,2,117 项通过,0 失败、0 错误、0 跳过。该场景验证未追踪后代的安全边界,后代所有权和回收能力仍由 2.3、3.2 跟踪。

‎src/test/java/io/github/easy4j/opencli/contract/LifecycleProbe.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public static void main(String[] args) throws Exception {
3131
writeStream(stream, Integer.parseInt(args[1]));
3232
return;
3333
}
34+
if ("inherited-pipes".equals(mode)) {
35+
String executable = System.getProperty("os.name").startsWith("Windows") ? "java.exe" : "java";
36+
Path java = Paths.get(System.getProperty("java.home"), "bin", executable);
37+
new ProcessBuilder(java.toString(), "-cp", System.getProperty("java.class.path"),
38+
LifecycleProbe.class.getName(), "heartbeat", args[3], args[2]).inheritIO().start();
39+
}
3440
if ("capture-hold".equals(mode)) {
3541
writeStream(System.out, Integer.parseInt(args[3]));
3642
writeStream(System.err, Integer.parseInt(args[4]));

‎src/test/java/io/github/easy4j/opencli/contract/OpenCliProcessContractTest.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.github.easy4j.opencli.browser.OpenCliBrowserClient;
55
import io.github.easy4j.opencli.browser.OpenCliBrowserSession;
66
import io.github.easy4j.opencli.browser.OpenCliBrowserSession.OpenCliBrowserNetworkOptions;
7+
import io.github.easy4j.opencli.core.OpenCliCancellationToken;
78
import io.github.easy4j.opencli.core.OpenCliExecutionDetails;
89
import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason;
910
import io.github.easy4j.opencli.core.OpenCliExecutor;
@@ -388,6 +389,44 @@ void sharedRuntimeBoundsConcurrentChildrenAndCombinedCapture() throws Exception
388389
}
389390
}
390391

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+
391430
@Test
392431
void preCancelledRequestNeverStartsAProcess() throws Exception {
393432
Class<?> tokenType = assertDoesNotThrow(() -> Class.forName("io.github.easy4j.opencli.core.OpenCliCancellationToken"));

0 commit comments

Comments
 (0)