official-plugin-seed-lock.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { existsSync } from "node:fs";
  2. import { mkdir, mkdtemp, rm, utimes, writeFile } from "node:fs/promises";
  3. import { spawn } from "node:child_process";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. import { describe, expect, it } from "vitest";
  7. import { withOfficialPluginSeedLock } from "../src/app/official-plugin-seed-lock.js";
  8. describe("official plugin seed lock", () => {
  9. it("keeps the lock until asynchronous work settles without blocking the event loop", async () => {
  10. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-seed-async-"));
  11. const target = join(root, "plugin");
  12. const entered = Promise.withResolvers<void>();
  13. const release = Promise.withResolvers<void>();
  14. const work = withOfficialPluginSeedLock(target, async () => {
  15. entered.resolve();
  16. await release.promise;
  17. return "complete";
  18. });
  19. try {
  20. await entered.promise;
  21. await new Promise<void>((resolve) => setImmediate(resolve));
  22. expect(existsSync(`${target}.seed-lock`)).toBe(true);
  23. } finally {
  24. release.resolve();
  25. expect(await work).toBe("complete");
  26. await rm(root, { recursive: true, force: true });
  27. }
  28. });
  29. it("waits for another process to release the same version lock", async () => {
  30. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-seed-lock-wait-"));
  31. const targetRoot = join(root, "zcode-cua", "0.5.2");
  32. const lockRoot = `${targetRoot}.seed-lock`;
  33. try {
  34. await mkdir(lockRoot, { recursive: true });
  35. const releaser = spawn(
  36. process.execPath,
  37. [
  38. "-e",
  39. 'setTimeout(()=>require("node:fs").rmSync(process.argv[1],{recursive:true,force:true}),200)',
  40. lockRoot,
  41. ],
  42. { stdio: "pipe" },
  43. );
  44. const released = new Promise<void>((resolve, reject) => {
  45. releaser.once("error", reject);
  46. releaser.once("exit", (code) =>
  47. code === 0 ? resolve() : reject(new Error(`lock releaser exited with ${code}`)),
  48. );
  49. });
  50. const startedAt = Date.now();
  51. const result = await withOfficialPluginSeedLock(
  52. targetRoot,
  53. () => {
  54. expect(existsSync(lockRoot)).toBe(true);
  55. return "seeded";
  56. },
  57. { retryDelayMs: 25, staleLockAgeMs: 60_000, timeoutMs: 5_000 },
  58. );
  59. await released;
  60. expect(result).toBe("seeded");
  61. expect(Date.now() - startedAt).toBeGreaterThanOrEqual(150);
  62. expect(existsSync(lockRoot)).toBe(false);
  63. } finally {
  64. await rm(root, { recursive: true, force: true });
  65. }
  66. });
  67. it("takes over a stale lock left by a crashed process", async () => {
  68. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-seed-lock-stale-"));
  69. const targetRoot = join(root, "zcode-cua", "0.5.2");
  70. const lockRoot = `${targetRoot}.seed-lock`;
  71. const staleTime = new Date(Date.now() - 60_000);
  72. try {
  73. await mkdir(lockRoot, { recursive: true });
  74. await utimes(lockRoot, staleTime, staleTime);
  75. expect(
  76. await withOfficialPluginSeedLock(targetRoot, () => "recovered", {
  77. retryDelayMs: 0,
  78. staleLockAgeMs: 1_000,
  79. timeoutMs: 1_000,
  80. }),
  81. ).toBe("recovered");
  82. expect(existsSync(lockRoot)).toBe(false);
  83. } finally {
  84. await rm(root, { recursive: true, force: true });
  85. }
  86. });
  87. it("does not take over an old lock while its owner process is alive", async () => {
  88. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-seed-lock-live-owner-"));
  89. const targetRoot = join(root, "zcode-cua", "0.5.2");
  90. const lockRoot = `${targetRoot}.seed-lock`;
  91. const staleTime = new Date(Date.now() - 60_000);
  92. let actionCalled = false;
  93. try {
  94. await mkdir(lockRoot, { recursive: true });
  95. await writeFile(join(lockRoot, "owner.json"), JSON.stringify({ pid: process.pid }));
  96. await utimes(lockRoot, staleTime, staleTime);
  97. await expect(
  98. (async () =>
  99. await withOfficialPluginSeedLock(
  100. targetRoot,
  101. () => {
  102. actionCalled = true;
  103. },
  104. { retryDelayMs: 1, staleLockAgeMs: 1, timeoutMs: 20 },
  105. ))(),
  106. ).rejects.toThrow(/timed out waiting/u);
  107. expect(actionCalled).toBe(false);
  108. expect(existsSync(lockRoot)).toBe(true);
  109. } finally {
  110. await rm(root, { recursive: true, force: true });
  111. }
  112. });
  113. });