| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { receiveWithRetry, resetByDeploy } from "../../src/http/git.js"; |
| 3 | import type { Env } from "../../src/env.js"; |
| 4 | import type { Caller } from "../../src/repo/types.js"; |
| 5 | |
| 6 | const RESET = "Durable Object reset because its code was updated."; |
| 7 | |
| 8 | /** A repository namespace whose objects fail the first `failures` pushes with `message`. */ |
| 9 | function fakes(failures: number, message = RESET) { |
| 10 | let calls = 0; |
| 11 | const stubs: number[] = []; |
| 12 | const retries: boolean[] = []; |
| 13 | const deleted: string[] = []; |
| 14 | const stub = (n: number) => ({ |
| 15 | gitReceivePack: async (_body: unknown, _caller: unknown, o: { retry?: boolean }) => { |
| 16 | stubs.push(n); |
| 17 | retries.push(!!o.retry); |
| 18 | if (calls++ < failures) throw new Error(message); |
| 19 | return new Uint8Array([1]); |
| 20 | }, |
| 21 | }); |
| 22 | let made = 0; |
| 23 | const env = { |
| 24 | REPO: { idFromName: (name: string) => name, get: () => stub(++made) }, |
| 25 | BUCKET: { delete: async (key: string) => void deleted.push(key) }, |
| 26 | } as unknown as Env; |
| 27 | return { env, first: stub(0) as never, stubs, retries, deleted }; |
| 28 | } |
| 29 | |
| 30 | const caller = { role: "write" } as unknown as Caller; |
| 31 | const spilled = { spilled: "push/abc", size: 70 << 20 }; |
| 32 | |
| 33 | describe("a push cut off by a deploy", () => { |
| 34 | it("is handed to the new code once more, through a new stub, marked as a retry", async () => { |
| 35 | const f = fakes(1); |
| 36 | expect(await receiveWithRetry(f.env, "r1", f.first, spilled, caller, false)).toEqual(new Uint8Array([1])); |
| 37 | expect(f.stubs).toEqual([0, 1]); |
| 38 | expect(f.retries).toEqual([false, true]); |
| 39 | // The staged body stays for the retry and is deleted once the push is done. |
| 40 | expect(f.deleted).toEqual(["push/abc"]); |
| 41 | }); |
| 42 | |
| 43 | it("deletes the staged body after a push that went through the first time", async () => { |
| 44 | const f = fakes(0); |
| 45 | await receiveWithRetry(f.env, "r1", f.first, spilled, caller, false); |
| 46 | expect(f.retries).toEqual([false]); |
| 47 | expect(f.deleted).toEqual(["push/abc"]); |
| 48 | }); |
| 49 | |
| 50 | it("fails after a second reset, and the staged body is deleted", async () => { |
| 51 | const f = fakes(2); |
| 52 | await expect(receiveWithRetry(f.env, "r1", f.first, spilled, caller, false)).rejects.toThrow(RESET); |
| 53 | expect(f.stubs).toEqual([0, 1]); |
| 54 | expect(f.deleted).toEqual(["push/abc"]); |
| 55 | }); |
| 56 | |
| 57 | it("is handed over once more after a memory reset too, marked as a retry", async () => { |
| 58 | const f = fakes(1, "Durable Object's isolate exceeded its memory limit and was reset."); |
| 59 | expect(await receiveWithRetry(f.env, "r1", f.first, new Uint8Array(4), caller, false)).toEqual(new Uint8Array([1])); |
| 60 | expect(f.stubs).toEqual([0, 1]); |
| 61 | expect(f.retries).toEqual([false, true]); |
| 62 | // Not a third time. |
| 63 | const g = fakes(2, "Durable Object's isolate exceeded its memory limit and was reset."); |
| 64 | await expect(receiveWithRetry(g.env, "r1", g.first, new Uint8Array(4), caller, false)).rejects.toThrow(/memory limit/); |
| 65 | expect(g.stubs).toEqual([0, 1]); |
| 66 | }); |
| 67 | |
| 68 | it("is not retried for any other error", async () => { |
| 69 | const f = fakes(1, "Internal error in Durable Object storage write."); |
| 70 | await expect(receiveWithRetry(f.env, "r1", f.first, new Uint8Array(4), caller, false)).rejects.toThrow(/storage write/); |
| 71 | expect(f.stubs).toEqual([0]); |
| 72 | expect(f.deleted).toEqual([]); |
| 73 | expect(resetByDeploy(new Error(RESET))).toBe(true); |
| 74 | expect(resetByDeploy("Durable Object reset because its code was updated.")).toBe(false); |
| 75 | }); |
| 76 | }); |