official-plugin-cache-fs.test.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. createOfficialPluginCacheRetryBudget,
  4. getOfficialPluginCacheRetryAttempts,
  5. isTransientOfficialPluginCacheFsError,
  6. retryOfficialPluginCacheFs,
  7. } from "../src/app/official-plugin-cache-fs.js";
  8. describe("official plugin cache fs helpers", () => {
  9. it("recognizes only transient cache contention errors", () => {
  10. for (const code of ["EACCES", "EBUSY", "EEXIST", "ENOTEMPTY", "EPERM"]) {
  11. expect(isTransientOfficialPluginCacheFsError(errno(code))).toBe(true);
  12. }
  13. expect(isTransientOfficialPluginCacheFsError(errno("ENOENT"))).toBe(false);
  14. // overlay 下层目录的 EXDEV 不会因重试消失;它由 seed 的删除后替换路径和降级协议处理。
  15. expect(isTransientOfficialPluginCacheFsError(errno("EXDEV"))).toBe(false);
  16. expect(isTransientOfficialPluginCacheFsError(new Error("invalid manifest"))).toBe(false);
  17. });
  18. it("retries transient failures within the shared deadline", () => {
  19. let nowMs = 0;
  20. const sleep = vi.fn((delayMs: number) => {
  21. nowMs += delayMs;
  22. });
  23. const budget = createOfficialPluginCacheRetryBudget({
  24. durationMs: 1_500,
  25. now: () => nowMs,
  26. sleep,
  27. });
  28. let attempts = 0;
  29. const result = retryOfficialPluginCacheFs(
  30. () => {
  31. attempts += 1;
  32. if (attempts < 3) throw errno("EPERM");
  33. return "ok";
  34. },
  35. { budget },
  36. );
  37. expect(result).toBe("ok");
  38. expect(attempts).toBe(3);
  39. expect(sleep.mock.calls).toEqual([[25], [50]]);
  40. });
  41. it("stops retrying when the shared deadline cannot fit the next delay", () => {
  42. let nowMs = 0;
  43. const budget = createOfficialPluginCacheRetryBudget({
  44. durationMs: 60,
  45. now: () => nowMs,
  46. sleep: (delayMs) => {
  47. nowMs += delayMs;
  48. },
  49. });
  50. const error = errno("EPERM");
  51. let attempts = 0;
  52. expect(() =>
  53. retryOfficialPluginCacheFs(
  54. () => {
  55. attempts += 1;
  56. throw error;
  57. },
  58. { budget },
  59. ),
  60. ).toThrow(error);
  61. expect(attempts).toBe(2);
  62. expect(getOfficialPluginCacheRetryAttempts(error)).toBe(2);
  63. });
  64. it("does not retry non-transient failures", () => {
  65. const operation = vi.fn(() => {
  66. throw new Error("invalid manifest");
  67. });
  68. expect(() => retryOfficialPluginCacheFs(operation)).toThrow("invalid manifest");
  69. expect(operation).toHaveBeenCalledTimes(1);
  70. });
  71. });
  72. function errno(code: string): NodeJS.ErrnoException {
  73. return Object.assign(new Error(code), { code });
  74. }