host-acceptance.cjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Windows 原生运行时验收。传入包含 helper、fixture、adapter.cjs 的构建目录。
  2. // Wine 可用 Node 20 执行;仅补 Promise.withResolvers,不把它记为 Node 24 / Windows 实机证据。
  3. Promise.withResolvers ??= function () {
  4. let resolve, reject;
  5. const promise = new Promise((a, b) => {
  6. resolve = a;
  7. reject = b;
  8. });
  9. return { promise, resolve, reject };
  10. };
  11. const assert = require("node:assert/strict");
  12. const { setTimeout: delay } = require("node:timers/promises");
  13. const { join, resolve } = require("node:path");
  14. const { spawn } = require("node:child_process");
  15. const { once } = require("node:events");
  16. const buildDir = resolve(process.argv[2]);
  17. const { spawnWindowsRuntimeHost } = require(join(buildDir, "adapter.cjs"));
  18. process.env.ZCODE_RUNTIME_HOST_PATH = join(buildDir, "zcode-runtime-host.exe");
  19. const fixture = join(buildDir, "zcode-runtime-host-acceptance.exe");
  20. async function create(mode) {
  21. const abort = new AbortController();
  22. const runtime = await spawnWindowsRuntimeHost({
  23. command: fixture,
  24. args: ["fixture", mode],
  25. cwd: buildDir,
  26. env: process.env,
  27. signal: abort.signal,
  28. });
  29. await runtime.ready();
  30. let output = "";
  31. runtime.child.stdout.on("data", (s) => (output += s));
  32. runtime.child.stderr.on("data", (s) => process.stderr.write(s));
  33. let root;
  34. runtime.onRootExit((code) => (root = code));
  35. return { runtime, abort, output: () => output, root: () => root };
  36. }
  37. (async () => {
  38. if (process.argv[3] === "crash-host") {
  39. const owned = await create("tree");
  40. await owned.runtime.start();
  41. while (!/descendant ([0-9]+)/.test(owned.output())) await delay(10);
  42. console.log(
  43. JSON.stringify([
  44. owned.runtime.runtimePid,
  45. owned.runtime.child.pid,
  46. Number(/descendant ([0-9]+)/.exec(owned.output())[1]),
  47. ]),
  48. );
  49. return;
  50. }
  51. for (const mode of ["graceful", "tree", "root-exit"]) {
  52. const x = await create(mode);
  53. assert.equal(x.output(), "");
  54. assert.notEqual(x.runtime.runtimePid, x.runtime.child.pid);
  55. await x.runtime.start();
  56. await delay(100);
  57. const begin = Date.now();
  58. await Promise.all([x.runtime.close(Date.now() + 3250), x.runtime.close(Date.now() + 3250)]);
  59. if (mode === "graceful") {
  60. assert.equal(x.root(), 0);
  61. assert.ok(Date.now() - begin >= 1400);
  62. }
  63. console.log("PASS adapter", mode, Date.now() - begin);
  64. }
  65. const late = await create("tree");
  66. await late.runtime.start();
  67. await assert.rejects(late.runtime.close(Date.now() + 50), /timed out/);
  68. await delay(2100);
  69. await late.runtime.close(Date.now() + 1000);
  70. console.log("PASS deadline retains Job; later close succeeds");
  71. const crashed = await create("tree");
  72. await crashed.runtime.start();
  73. await delay(150);
  74. crashed.runtime.child.kill();
  75. await delay(150);
  76. await crashed.runtime.close(Date.now() + 1500);
  77. console.log("PASS helper crash kernel Job probe recovery");
  78. const cancelled = await create("tree");
  79. cancelled.abort.abort();
  80. await assert.rejects(cancelled.runtime.start());
  81. await cancelled.runtime.close(Date.now() + 1500);
  82. assert.equal(cancelled.output(), "");
  83. console.log("PASS cancelled startup never executes CLI");
  84. const early = await spawnWindowsRuntimeHost({
  85. command: fixture,
  86. args: ["fixture", "tree"],
  87. cwd: buildDir,
  88. env: process.env,
  89. signal: new AbortController().signal,
  90. });
  91. await early.close(Date.now() + 2000);
  92. await assert.rejects(early.start());
  93. console.log("PASS close during startup retains and cleans owner");
  94. const invalid = await spawnWindowsRuntimeHost({
  95. command: join(buildDir, "missing-cli.exe"),
  96. args: [],
  97. cwd: buildDir,
  98. env: process.env,
  99. signal: new AbortController().signal,
  100. });
  101. invalid.child.stderr.resume();
  102. await assert.rejects(invalid.ready());
  103. await invalid.close(Date.now() + 2000);
  104. console.log("PASS failed CLI creation releases verified empty Job");
  105. const host = spawn(process.execPath, [__filename, buildDir, "crash-host"], {
  106. stdio: ["ignore", "pipe", "inherit"],
  107. env: process.env,
  108. });
  109. let hostOutput = "";
  110. host.stdout.on("data", (s) => (hostOutput += s));
  111. while (!hostOutput.includes("\n")) await delay(10);
  112. const pids = JSON.parse(hostOutput.trim());
  113. const observer = spawn(fixture, ["watch", ...pids.map(String)], {
  114. stdio: ["ignore", "pipe", "inherit"],
  115. });
  116. const observerExit = once(observer, "exit");
  117. let observerOutput = "";
  118. observer.stdout.on("data", (s) => (observerOutput += s));
  119. while (!observerOutput.includes("watching")) await delay(10);
  120. host.kill();
  121. assert.equal((await observerExit)[0], 0);
  122. console.log("PASS Host death terminates helper, CLI and descendant handles");
  123. })().catch((e) => {
  124. console.error(e);
  125. process.exitCode = 1;
  126. });