Skip to content

Commit 0e52cac

Browse files
committed
test: verify OpenCLI saturated process and capture limits
1 parent 4d42b93 commit 0e52cac

3 files changed

Lines changed: 96 additions & 11 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
## 1. 资源回归
88

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

1212
## 2. 执行核修复
1313

@@ -49,4 +49,12 @@ JDK 17 红灯测试以 8,193 字节作为捕获上限,分两次写满,观察
4949

5050
JDK 8 使用系统 Maven 执行 `-B -ntp -o clean verify`,2,114 项通过,0 失败、0 错误、0 跳过。真实 OpenCLI 1.8.6 两项复验通过。4,500 ms 时限返回 `EXECUTION_TIMEOUT` 并保留 34 条完整事件。2 KiB 上限返回 `OUTPUT_LIMIT` 并保留 2,048 字节和 11 条完整事件。两次均确认 SDK 启动的 CLI 子进程已退出,后续 daemon 查询可用。
5151

52-
本次测量单个捕获数组的容量。整体堆内存峰值还包含扩容时的数组复制、输出快照和并发调用,任务 1.2 继续跟踪。
52+
本次测量单个捕获数组的容量。整体堆内存还包含扩容时的数组复制、输出快照和并发调用,按独立指标记录。
53+
54+
## 2026-11-18 OC-PROC-001/002/003 饱和验证
55+
56+
`OpenCliProcessContractTest#sharedRuntimeBoundsConcurrentChildrenAndCombinedCapture` 使用三个客户端共享两个执行许可。两个真实 JVM 子进程分别写入 131,073 字节 stdout 和 65,539 字节 stderr,然后发出就绪标记并等待同一释放信号。两个子进程仍在途时提交四次调用,四次均返回 `QUEUE_TIMEOUT`,执行详情和子进程标记均确认没有启动进程。该场景在途峰值为 2。
57+
58+
释放后,两次调用均确认根进程退出,四条输出流保留 393,224 字节,恰好达到两个执行槽的合计捕获预算,零截断。这个数值由执行详情测得,对应离线 fixture 的捕获量。JVM 整体堆内存还包含其他分配,本场景不对其计量。
59+
60+
JDK 8 使用系统 Maven 执行 `-B -ntp -o clean verify`,2,115 项通过,0 失败、0 错误、0 跳过。

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.easy4j.opencli.contract;
22

3+
import java.io.PrintStream;
34
import java.nio.charset.StandardCharsets;
45
import java.nio.file.Files;
56
import java.nio.file.Path;
@@ -26,17 +27,14 @@ public static void main(String[] args) throws Exception {
2627
return;
2728
}
2829
if ("stdout".equals(mode) || "stderr".equals(mode)) {
29-
byte[] block = new byte[8192];
30-
Arrays.fill(block, (byte) 'x');
31-
int remaining = Integer.parseInt(args[1]);
32-
while (remaining > 0) {
33-
int size = Math.min(block.length, remaining);
34-
if ("stdout".equals(mode)) { System.out.write(block, 0, size); }
35-
else { System.err.write(block, 0, size); }
36-
remaining -= size;
37-
}
30+
PrintStream stream = "stdout".equals(mode) ? System.out : System.err;
31+
writeStream(stream, Integer.parseInt(args[1]));
3832
return;
3933
}
34+
if ("capture-hold".equals(mode)) {
35+
writeStream(System.out, Integer.parseInt(args[3]));
36+
writeStream(System.err, Integer.parseInt(args[4]));
37+
}
4038
Path marker = Paths.get(args[1]);
4139
if ("count-hold".equals(mode)) {
4240
Files.write(marker, "started\n".getBytes(StandardCharsets.UTF_8),
@@ -56,6 +54,18 @@ public static void main(String[] args) throws Exception {
5654
}
5755
}
5856

57+
private static void writeStream(PrintStream stream, int bytes) {
58+
byte[] block = new byte[8192];
59+
Arrays.fill(block, (byte) 'x');
60+
int remaining = bytes;
61+
while (remaining > 0) {
62+
int size = Math.min(block.length, remaining);
63+
stream.write(block, 0, size);
64+
remaining -= size;
65+
}
66+
stream.flush();
67+
}
68+
5969
private static void exercisePrivacy(String outcome, String canary) throws Exception {
6070
switch (outcome) {
6171
case "nonzero":

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
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.OpenCliExecutionDetails;
78
import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason;
89
import io.github.easy4j.opencli.core.OpenCliExecutor;
10+
import io.github.easy4j.opencli.core.OpenCliProcessRuntime;
911
import io.github.easy4j.opencli.core.OpenCliResult;
1012
import io.github.easy4j.opencli.exception.OpenCliException;
1113
import io.github.easy4j.opencli.exception.OpenCliNonZeroExitException;
@@ -321,6 +323,71 @@ void explicitSharedRuntimeLimitsBothClients() throws Exception {
321323
}
322324
}
323325

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

0 commit comments

Comments
 (0)