|
14 | 14 | // issued — method, path, query — and the binding cache is a real file in a real |
15 | 15 | // sandbox directory. |
16 | 16 | import { afterAll, afterEach, beforeEach, describe, expect, test } from "bun:test" |
17 | | -import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs" |
| 17 | +import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs" |
18 | 18 | import { execFileSync } from "node:child_process" |
19 | 19 | import path from "node:path" |
20 | 20 | import os from "node:os" |
@@ -1035,6 +1035,63 @@ describe("the poller does not drip", () => { |
1035 | 1035 | }) |
1036 | 1036 | }) |
1037 | 1037 |
|
| 1038 | +describe("a state directory that cannot be written", () => { |
| 1039 | + test("does not turn the notify → resolve chain into a hot loop", async () => { |
| 1040 | + // Ralph's reproduction on #1279: a row on disk, the server answering |
| 1041 | + // unbound, the state directory read-only, and a subscriber that resolves |
| 1042 | + // the way the sidebar's queued refresh does. Notifying on a failed write |
| 1043 | + // made every resolve re-enter `forgetBinding`, which notified, which the |
| 1044 | + // subscriber answered with another resolve — forty times in 24ms. |
| 1045 | + await bind(projectDir) |
| 1046 | + expireValidationForTests(projectDir) |
| 1047 | + const originalFetch3 = globalThis.fetch |
| 1048 | + globalThis.fetch = (async (input: any, init?: any) => { |
| 1049 | + const url = typeof input === "string" ? input : input.url |
| 1050 | + const method = (init?.method ?? "GET").toUpperCase() |
| 1051 | + requests.push({ method, url }) |
| 1052 | + if (method === "GET" && url.includes("/datamate-project-bindings/by-")) |
| 1053 | + return new Response(JSON.stringify({ detail: "gone" }), { |
| 1054 | + status: 404, |
| 1055 | + headers: { "content-type": "application/json" }, |
| 1056 | + }) |
| 1057 | + return originalFetch3(input, init) |
| 1058 | + }) as typeof fetch |
| 1059 | + let notifications = 0 |
| 1060 | + let resolves = 0 |
| 1061 | + const unsubscribe = onBindingChanged(() => { |
| 1062 | + notifications++ |
| 1063 | + if (notifications < 50) void resolveBindingOutcome(projectDir).then(() => resolves++) |
| 1064 | + }) |
| 1065 | + const stateDir = path.dirname(cachePath()) |
| 1066 | + chmodSync(stateDir, 0o555) |
| 1067 | + try { |
| 1068 | + const outcome = await resolveBindingOutcome(projectDir) |
| 1069 | + expect(outcome.status).toBe("unbound") |
| 1070 | + await new Promise((r) => setTimeout(r, 100)) |
| 1071 | + } finally { |
| 1072 | + chmodSync(stateDir, 0o755) |
| 1073 | + unsubscribe() |
| 1074 | + globalThis.fetch = originalFetch3 |
| 1075 | + } |
| 1076 | + // Nothing on disk changed, so nobody was told; the row stays until the |
| 1077 | + // next poll, which is one interval of staleness rather than a loop. |
| 1078 | + expect(notifications).toBe(0) |
| 1079 | + expect(resolves).toBe(0) |
| 1080 | + }) |
| 1081 | +}) |
| 1082 | + |
| 1083 | +describe("status reuses a binding the caller resolved", () => { |
| 1084 | + test("makes no binding request of its own when handed one", async () => { |
| 1085 | + await bind(projectDir) |
| 1086 | + expireValidationForTests(projectDir) |
| 1087 | + const b = (await readLocalBinding(projectDir))! |
| 1088 | + requests = [] |
| 1089 | + const report = await status(projectDir, { poll: true, binding: b }) |
| 1090 | + expect(report.binding?.datamateId).toBe(42) |
| 1091 | + expect(requests.filter((r) => r.url.includes("/datamate-project-bindings/"))).toHaveLength(0) |
| 1092 | + }) |
| 1093 | +}) |
| 1094 | + |
1038 | 1095 | describe("the poller coalesces overlapping misses", () => { |
1039 | 1096 | test("two refreshes on a cold memo put one request on the wire", async () => { |
1040 | 1097 | // A remount while a slow refresh is still out started a second one; both |
|
0 commit comments