run-e2e-session-lifecycle-stress.mjs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env node
  2. import { randomBytes } from "node:crypto";
  3. import { mkdir, rm, writeFile } from "node:fs/promises";
  4. import { join, resolve } from "node:path";
  5. import process from "node:process";
  6. import { fileURLToPath } from "node:url";
  7. import { spawn } from "node:child_process";
  8. const DEFAULT_ITERATIONS = 25;
  9. const desktopDir = resolve(fileURLToPath(new URL("..", import.meta.url)));
  10. function readIterations(args, env) {
  11. const arg = args.find((item) => item.startsWith("--iterations="));
  12. const raw = arg?.slice("--iterations=".length) ?? env.ZCODE_E2E_SESSION_STRESS_ITERATIONS;
  13. if (raw === undefined || raw.trim() === "") {
  14. return DEFAULT_ITERATIONS;
  15. }
  16. if (!/^\d+$/u.test(raw) || Number(raw) <= 0) {
  17. throw new Error(`iterations must be a positive integer, received: ${raw}`);
  18. }
  19. return Number(raw);
  20. }
  21. function run(command, args, options) {
  22. return new Promise((resolvePromise, reject) => {
  23. const child = spawn(command, args, options);
  24. child.once("error", reject);
  25. child.once("exit", (code, signal) => {
  26. if (signal) {
  27. reject(new Error(`session lifecycle stress terminated by ${signal}`));
  28. return;
  29. }
  30. resolvePromise(code ?? 1);
  31. });
  32. });
  33. }
  34. const iterations = readIterations(process.argv.slice(2), process.env);
  35. const entropy = randomBytes(6).toString("hex");
  36. const runId = `desktop-e2e-session-lifecycle-p${process.pid}-${entropy}`;
  37. const cacheRoot = join(desktopDir, ".e2e-cache", runId);
  38. const specsDir = join(cacheRoot, "specs");
  39. const homeDir = join(cacheRoot, "home");
  40. const artifactDir = join(desktopDir, ".e2e-artifacts", runId);
  41. const pnpmCommand = process.platform === "win32" ? "pnpm.cmd" : "pnpm";
  42. await mkdir(specsDir, { recursive: true });
  43. for (let index = 1; index <= iterations; index += 1) {
  44. const marker = String(index).padStart(3, "0");
  45. await writeFile(
  46. join(specsDir, `session-lifecycle-${marker}.test.ts`),
  47. // 每个 alias 都由同一个 WDIO launcher 派发为独立 worker/session,复现全量
  48. // 运行的重复启动/销毁模式,而不是在一个 Electron session 内循环断言。
  49. 'import "../../../test/e2e/infrastructure/session-lifecycle.test.js";\n',
  50. "utf8",
  51. );
  52. }
  53. let exitCode = 1;
  54. try {
  55. exitCode = await run(
  56. pnpmCommand,
  57. ["exec", "wdio", "run", "wdio.conf.ts", "--spec", join(specsDir, "*.test.ts")],
  58. {
  59. cwd: desktopDir,
  60. env: {
  61. ...process.env,
  62. PNPM_CONFIG_VERIFY_DEPS_BEFORE_RUN: "false",
  63. ZCODE_E2E_ARTIFACT_DIR: artifactDir,
  64. ZCODE_E2E_HOME_DIR: homeDir,
  65. ZCODE_E2E_RUN_ID: runId,
  66. },
  67. stdio: "inherit",
  68. },
  69. );
  70. } finally {
  71. await rm(cacheRoot, { force: true, recursive: true });
  72. }
  73. console.log(`[e2e-session-lifecycle] iterations=${iterations}; artifact=${artifactDir}`);
  74. process.exitCode = exitCode;