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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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("retries an asynchronous rejection and yields during retry waits", async () => {
  10. const waiting = Promise.withResolvers<void>();
  11. const release = Promise.withResolvers<void>();
  12. const operation = vi.fn().mockRejectedValueOnce(errno("EPERM")).mockResolvedValue("ok");
  13. let sleepEntered = false;
  14. const work = retryOfficialPluginCacheFs(operation, {
  15. budget: createOfficialPluginCacheRetryBudget({
  16. sleep: () => {
  17. sleepEntered = true;
  18. waiting.resolve();
  19. return release.promise;
  20. },
  21. }),
  22. });
  23. // 立即观察拒绝;旧同步重试不能处理异步操作失败。
  24. const observed = Promise.resolve(work).then(
  25. (value) => ({ value }),
  26. (error) => ({ error }),
  27. );
  28. await Promise.race([waiting.promise, observed]);
  29. try {
  30. expect(sleepEntered).toBe(true);
  31. expect(operation).toHaveBeenCalledTimes(1);
  32. await new Promise<void>((resolve) => setImmediate(resolve));
  33. expect(operation).toHaveBeenCalledTimes(1);
  34. } finally {
  35. release.resolve();
  36. }
  37. expect(await observed).toEqual({ value: "ok" });
  38. expect(operation).toHaveBeenCalledTimes(2);
  39. });
  40. it("recognizes only transient cache contention errors", () => {
  41. for (const code of ["EACCES", "EBUSY", "EEXIST", "ENOTEMPTY", "EPERM"]) {
  42. expect(isTransientOfficialPluginCacheFsError(errno(code))).toBe(true);
  43. }
  44. expect(isTransientOfficialPluginCacheFsError(errno("ENOENT"))).toBe(false);
  45. // overlay 下层目录的 EXDEV 不会因重试消失;它由 seed 的删除后替换路径和降级协议处理。
  46. expect(isTransientOfficialPluginCacheFsError(errno("EXDEV"))).toBe(false);
  47. expect(isTransientOfficialPluginCacheFsError(new Error("invalid manifest"))).toBe(false);
  48. });
  49. it("does not start another filesystem attempt after cancellation", async () => {
  50. const controller = new AbortController();
  51. const entered = Promise.withResolvers<void>();
  52. const release = Promise.withResolvers<void>();
  53. const operation = vi.fn(async () => {
  54. throw errno("EPERM");
  55. });
  56. const result = retryOfficialPluginCacheFs(operation, {
  57. budget: createOfficialPluginCacheRetryBudget({
  58. signal: controller.signal,
  59. sleep: async () => {
  60. entered.resolve();
  61. await release.promise;
  62. },
  63. }),
  64. }).catch((error: unknown) => error);
  65. await entered.promise;
  66. controller.abort();
  67. release.resolve();
  68. expect(await result).toMatchObject({ name: "AbortError" });
  69. expect(operation).toHaveBeenCalledOnce();
  70. });
  71. it("keeps the event loop responsive through four failed EPERM attempts", async () => {
  72. const entered = Array.from({ length: 3 }, () => Promise.withResolvers<void>());
  73. const releases = Array.from({ length: 3 }, () => Promise.withResolvers<void>());
  74. const operation = vi.fn(async () => {
  75. throw errno("EPERM");
  76. });
  77. let waits = 0;
  78. let settled = false;
  79. const work = retryOfficialPluginCacheFs(operation, {
  80. budget: createOfficialPluginCacheRetryBudget({
  81. now: () => 0,
  82. sleep: () => {
  83. const index = waits++;
  84. entered[index]!.resolve();
  85. return releases[index]!.promise;
  86. },
  87. }),
  88. }).then(
  89. () => {
  90. settled = true;
  91. return undefined;
  92. },
  93. (error: unknown) => {
  94. settled = true;
  95. return error;
  96. },
  97. );
  98. try {
  99. for (let index = 0; index < entered.length; index += 1) {
  100. await Promise.race([entered[index]!.promise, work]);
  101. await new Promise<void>((resolve) => setImmediate(resolve));
  102. expect(settled).toBe(false);
  103. expect(operation).toHaveBeenCalledTimes(index + 1);
  104. releases[index]!.resolve();
  105. }
  106. const error = await work;
  107. expect(error).toMatchObject({ code: "EPERM" });
  108. expect(getOfficialPluginCacheRetryAttempts(error)).toBe(4);
  109. expect(operation).toHaveBeenCalledTimes(4);
  110. } finally {
  111. for (const release of releases) release.resolve();
  112. await work;
  113. }
  114. });
  115. it("retries transient failures within the shared deadline", async () => {
  116. let nowMs = 0;
  117. const sleep = vi.fn((delayMs: number) => {
  118. nowMs += delayMs;
  119. });
  120. const budget = createOfficialPluginCacheRetryBudget({
  121. durationMs: 1_500,
  122. now: () => nowMs,
  123. sleep,
  124. });
  125. let attempts = 0;
  126. const result = await retryOfficialPluginCacheFs(
  127. () => {
  128. attempts += 1;
  129. if (attempts < 3) throw errno("EPERM");
  130. return "ok";
  131. },
  132. { budget },
  133. );
  134. expect(result).toBe("ok");
  135. expect(attempts).toBe(3);
  136. expect(sleep.mock.calls).toEqual([[25], [50]]);
  137. });
  138. it("stops retrying when the shared deadline cannot fit the next delay", async () => {
  139. let nowMs = 0;
  140. const budget = createOfficialPluginCacheRetryBudget({
  141. durationMs: 60,
  142. now: () => nowMs,
  143. sleep: (delayMs) => {
  144. nowMs += delayMs;
  145. },
  146. });
  147. const error = errno("EPERM");
  148. let attempts = 0;
  149. await expect(
  150. (async () =>
  151. await retryOfficialPluginCacheFs(
  152. () => {
  153. attempts += 1;
  154. throw error;
  155. },
  156. { budget },
  157. ))(),
  158. ).rejects.toThrow(error);
  159. expect(attempts).toBe(2);
  160. expect(getOfficialPluginCacheRetryAttempts(error)).toBe(2);
  161. });
  162. it("does not retry non-transient failures", async () => {
  163. const operation = vi.fn(() => {
  164. throw new Error("invalid manifest");
  165. });
  166. await expect((async () => await retryOfficialPluginCacheFs(operation))()).rejects.toThrow(
  167. "invalid manifest",
  168. );
  169. expect(operation).toHaveBeenCalledTimes(1);
  170. });
  171. });
  172. function errno(code: string): NodeJS.ErrnoException {
  173. return Object.assign(new Error(code), { code });
  174. }