plugin-storage-lock.test.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { spawn } from "node:child_process";
  2. import { once } from "node:events";
  3. import { mkdtemp, rm } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  7. import { withPluginStorageLock } from "../src/lib/plugin-storage-lock.js";
  8. describe("withPluginStorageLock", () => {
  9. let root: string;
  10. beforeEach(async () => {
  11. root = await mkdtemp(join(tmpdir(), "plugin-storage-lock-"));
  12. });
  13. afterEach(async () => {
  14. await rm(root, { recursive: true, force: true });
  15. });
  16. it("serializes same-root writes and checks cancellation after admission", async () => {
  17. const release = Promise.withResolvers<void>();
  18. const controller = new AbortController();
  19. const cancelledWrite = vi.fn(async () => {});
  20. const order: string[] = [];
  21. const first = withPluginStorageLock(root, async () => {
  22. order.push("first");
  23. await release.promise;
  24. order.push("commit");
  25. });
  26. const cancelled = withPluginStorageLock(root, cancelledWrite, undefined, controller.signal);
  27. const second = withPluginStorageLock(root, async () => {
  28. order.push("second");
  29. });
  30. controller.abort(new Error("cancelled"));
  31. release.resolve();
  32. await expect(cancelled).rejects.toThrow("cancelled");
  33. await Promise.all([first, second]);
  34. expect(cancelledWrite).not.toHaveBeenCalled();
  35. expect(order).toEqual(["first", "commit", "second"]);
  36. });
  37. it("runs different roots independently, releases failed writes and rejects expired owners", async () => {
  38. await withPluginStorageLock(root, async (owner) => {
  39. await expect(withPluginStorageLock(root, async () => "joined", owner)).resolves.toBe(
  40. "joined",
  41. );
  42. await expect(
  43. withPluginStorageLock(join(root, "other"), async () => true, owner),
  44. ).rejects.toThrow();
  45. await expect(
  46. withPluginStorageLock(join(root, "other"), async () => {
  47. throw new Error("boom");
  48. }),
  49. ).rejects.toThrow("boom");
  50. return owner;
  51. }).then(async (expired) => {
  52. await expect(withPluginStorageLock(root, async () => true, expired)).rejects.toThrow();
  53. });
  54. await expect(withPluginStorageLock(root, async () => "ok")).resolves.toBe("ok");
  55. });
  56. it("reclaims the lock when a child process dies in a transaction", async () => {
  57. const worker = spawn(
  58. process.execPath,
  59. [
  60. "--import",
  61. "tsx",
  62. "--input-type=module",
  63. "-e",
  64. `const { withPluginStorageLock } = await import(process.argv[1]);
  65. await withPluginStorageLock(process.argv[2], async () => {
  66. process.stdout.write("locked\\n");
  67. await new Promise(() => setInterval(() => {}, 1000));
  68. });`,
  69. new URL("../src/lib/plugin-storage-lock.ts", import.meta.url).href,
  70. root,
  71. ],
  72. { stdio: ["ignore", "pipe", "pipe"] },
  73. );
  74. const exit = once(worker, "exit");
  75. try {
  76. await Promise.race([
  77. once(worker.stdout, "data"),
  78. exit.then(([code]) => {
  79. throw new Error(`lock child exited ${code}`);
  80. }),
  81. ]);
  82. } finally {
  83. worker.kill("SIGKILL");
  84. await exit;
  85. }
  86. await expect(withPluginStorageLock(root, async () => "recovered")).resolves.toBe("recovered");
  87. }, 15_000);
  88. });