Skip to content

Commit c4896d2

Browse files
committed
test: synchronize overlapping OpenCLI context failure calls
1 parent 2986a86 commit c4896d2

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎openspec/changes/add-opencli-context-and-diagnostics/tasks.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
一个 Java 客户端通过独立 JVM 子进程并发执行两个不同 profile/window 的调用。故意失败的调用及其 partial result 保留自己的 argv,另一调用保留自己的 argv。随后没有覆盖值的调用继续使用客户端的原默认值。现有回归同时验证显式 `false`、scope 冲突和未经验证诊断的 UNKNOWN。
4444

45+
并发夹具使用临时目录中的两个同步信号。成功调用的子进程先发出就绪信号,失败调用才以 7 退出;父测试确认失败结果后释放成功调用。由此验证失败发生时成功调用确实尚未完成。以 `2986a86`、`a4687d1`、`72c9dd8` 为基线加入同步验证后,Java 8、17、21 的 `./mvnw -B -ntp -o clean verify` 分别通过 2235、2236、2235 项,零失败、零错误、零跳过。
46+
4547
使用发布版 OpenCLI 1.8.8 的独立配置目录验证默认诊断只取版本,不执行安装版 CLI 的 startup hook。显式发起帮助探测会触发 fixture hook,后续默认诊断不会再次执行它。sitemap 和 trace 路径保持数据,pipeline CLI 仍为 DEFERRED。
4648

4749
本分支以 `ee64145ca904f2384bed1265036cd7c2acc1f73b` 为基线,使用 JDK 8,提供安装版 CLI 对应的 `OPENCLI_INSTALLED_NODE` 与 `OPENCLI_INSTALLED_ENTRY` 后执行 `./mvnw -B -ntp -o -Dtest=OpenCliInvocationContextTest,OpenCliDiagnosticsTest,OpenCliReadOnlyDiagnosticsIT test`。8 项实际执行,零失败、零错误、零跳过,退出码为 0。报告位于 `target/surefire-reports`。

‎src/test/java/io/github/easy4j/opencli/capability/OpenCliInvocationContextTest.java‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import java.io.File;
1010
import java.io.InputStream;
1111
import java.nio.charset.StandardCharsets;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.nio.file.Paths;
1215
import java.util.Arrays;
1316
import java.util.Collections;
1417
import java.util.Scanner;
@@ -17,6 +20,7 @@
1720
import java.util.concurrent.Future;
1821
import java.util.concurrent.TimeUnit;
1922
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.io.TempDir;
2024

2125
import static org.junit.jupiter.api.Assertions.*;
2226

@@ -35,20 +39,25 @@ void concurrentInvocationsKeepTheirProfileAndWindow() throws Exception {
3539
}
3640

3741
@Test
38-
void failedInvocationKeepsItsContextWithoutChangingConcurrentOrDefaultCalls() throws Exception {
42+
void failedInvocationKeepsItsContextWithoutChangingConcurrentOrDefaultCalls(@TempDir Path coordination) throws Exception {
3943
OpenCliProperties properties = properties();
4044
properties.setInvocationDefaults(OpenCliInvocationContext.builder().profile("profile-default")
4145
.window(WindowMode.FOREGROUND).build());
46+
properties.getEnvironment().put("OPENCLI_CONTEXT_TEST_COORDINATION", coordination.toString());
4247
ExecutorService pool = Executors.newFixedThreadPool(2);
4348
try (OpenCliClient client = new OpenCliClient(properties)) {
4449
Future<OpenCliNonZeroExitException> failure = pool.submit(() -> assertThrows(
4550
OpenCliNonZeroExitException.class, () -> state(client, "profile-failure", WindowMode.BACKGROUND)));
4651
Future<String> success = pool.submit(() -> state(client, "profile-success", WindowMode.FOREGROUND));
4752

48-
assertEquals("--profile|profile-success|browser|session-a|--window|foreground|state",
49-
success.get(12, TimeUnit.SECONDS));
53+
OpenCliNonZeroExitException failed = failure.get(20, TimeUnit.SECONDS);
54+
assertEquals(Integer.valueOf(7), failed.getPartialResult().getExitCode());
5055
assertEquals("--profile|profile-failure|browser|session-a|--window|background|state",
51-
failure.get(12, TimeUnit.SECONDS).getPartialResult().getStdout());
56+
failed.getPartialResult().getStdout());
57+
assertFalse(success.isDone());
58+
Files.createFile(coordination.resolve("release-peer"));
59+
assertEquals("--profile|profile-success|browser|session-a|--window|foreground|state",
60+
success.get(20, TimeUnit.SECONDS));
5261
assertEquals("--profile|profile-default|browser|session-a|--window|foreground|state",
5362
client.invokeDiscovered(OpenCliHelpScope.BROWSER_STATE, "opencli browser <session> state",
5463
"session-a", null, null).getStdout());
@@ -124,8 +133,25 @@ public static void main(String[] args) throws Exception {
124133
}
125134
System.out.print(String.join("|", args));
126135
if (argv.startsWith("--profile profile-failure ")) {
136+
Path coordination = Paths.get(System.getenv("OPENCLI_CONTEXT_TEST_COORDINATION"));
137+
awaitFile(coordination.resolve("peer-ready"));
127138
System.exit(7);
128139
}
140+
if (argv.startsWith("--profile profile-success ")) {
141+
Path coordination = Paths.get(System.getenv("OPENCLI_CONTEXT_TEST_COORDINATION"));
142+
Files.createFile(coordination.resolve("peer-ready"));
143+
awaitFile(coordination.resolve("release-peer"));
144+
}
145+
}
146+
}
147+
148+
private static void awaitFile(Path file) throws Exception {
149+
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
150+
while (!Files.exists(file)) {
151+
if (System.nanoTime() >= deadline) {
152+
throw new IllegalStateException("Timed out waiting for peer fixture signal");
153+
}
154+
Thread.sleep(20);
129155
}
130156
}
131157

0 commit comments

Comments
 (0)