plugin-atomic-state.test.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterAll, expect, it } from "vitest";
  5. import { readAtomicTarget, recoverAtomicTargetSync } from "../src/plugins/atomic-directory.js";
  6. /** 不存在的进程号:存活检查返回 ESRCH,事务视为无人持有。 */
  7. const DEAD_OWNER_PID = 2 ** 31 - 2;
  8. const TRANSACTION_ID = "transaction";
  9. const MARKER = "generation.txt";
  10. type Transaction = "none" | "v1" | "standalone" | "coordinated" | "committed";
  11. interface AtomicCase {
  12. transaction: Transaction;
  13. live: boolean;
  14. hadTarget: boolean;
  15. target: boolean;
  16. backup: boolean;
  17. }
  18. const cases: AtomicCase[] = [];
  19. for (const transaction of ["none", "v1", "standalone", "coordinated", "committed"] as const)
  20. for (const live of transaction === "none" || transaction === "v1" ? [false] : [false, true])
  21. for (const hadTarget of transaction === "none" || transaction === "v1"
  22. ? [false]
  23. : [false, true])
  24. for (const target of [false, true])
  25. for (const backup of [false, true])
  26. cases.push({ transaction, live, hadTarget, target, backup });
  27. const root = await mkdtemp(join(tmpdir(), "plugin-atomic-state-"));
  28. afterAll(() => rm(root, { recursive: true, force: true }));
  29. async function generation(path: string): Promise<string> {
  30. return readFile(join(path, MARKER), "utf8").catch(() => "none");
  31. }
  32. it("enumerates every transaction state", () => {
  33. expect(cases).toHaveLength(56);
  34. });
  35. // 修复前同步恢复入口与异步只读入口各写一套判定,同一磁盘状态下读到的一代或稳定性不同。
  36. it.each(cases)(
  37. "both entries agree on the committed generation: %o",
  38. async ({ transaction, live, hadTarget, target, backup }) => {
  39. const parent = await mkdtemp(join(root, "case-"));
  40. const targetPath = join(parent, "plugin");
  41. const backupPath = join(parent, ".plugin.backup");
  42. const authorityPath = join(parent, "installed.json");
  43. for (const [exists, path, name] of [
  44. [target, targetPath, "target"],
  45. [backup, backupPath, "backup"],
  46. ] as const) {
  47. if (!exists) continue;
  48. await mkdir(path);
  49. await writeFile(join(path, MARKER), name);
  50. }
  51. const coordinated = transaction === "coordinated" || transaction === "committed";
  52. await writeFile(
  53. authorityPath,
  54. JSON.stringify({
  55. plugins: transaction === "committed" ? [{ cacheTransactionId: TRANSACTION_ID }] : [],
  56. }),
  57. );
  58. if (transaction !== "none")
  59. await writeFile(
  60. join(parent, ".plugin.transaction.json"),
  61. JSON.stringify(
  62. transaction === "v1"
  63. ? { version: 1, stageName: ".plugin.stage-old" }
  64. : {
  65. version: 2,
  66. stageName: ".plugin.stage-old",
  67. transactionId: TRANSACTION_ID,
  68. ownerId: "other-process",
  69. // 父进程一定存活,用它模拟仍在写入的另一个进程。
  70. ownerPid: live ? process.ppid : DEAD_OWNER_PID,
  71. hadTarget,
  72. mode: coordinated ? "coordinated" : "standalone",
  73. ...(coordinated ? { authorityPath } : {}),
  74. },
  75. ),
  76. );
  77. const view = await readAtomicTarget(targetPath);
  78. const read = await generation(view.path);
  79. const before = await generation(targetPath);
  80. const recovered = await generation(recoverAtomicTargetSync(targetPath));
  81. expect(read).toBe(recovered);
  82. if (live) {
  83. // 仍有 writer:只有已提交且读 target 时才可作为执行根。
  84. expect(view.stable).toBe(transaction === "committed" && view.path === targetPath);
  85. } else {
  86. // 无人持有:恢复不需要搬动 target 时,读取方也应把 target 视为稳定。
  87. expect(view.stable).toBe(recovered === before);
  88. }
  89. },
  90. );