Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
test(media): let callers await the registry's background path refresh
Etienne Lescotaf13f2ca3d7e
diff --git a/electron/media/cursorSidecar.test.ts b/electron/media/cursorSidecar.test.tsindex 400553819..88ad3f60e 100644--- a/electron/media/cursorSidecar.test.ts+++ b/electron/media/cursorSidecar.test.ts@@ -17,6 +17,7 @@ import {readCursorSidecar,readCursorTelemetryFile,} from "./cursorSidecar";+import { whenRegistryIdle } from "./mediaLinksRegistry";let dir: string;@@ -25,6 +26,12 @@ beforeEach(async () => {});afterEach(async () => {+ // The registry fallback below starts a path-refresh write that the lookup+ // deliberately does not await, so it can still be queued when the test ends.+ // Removing the tree underneath it made `fs.rm` fail with ENOTEMPTY — the write+ // recreating an entry between rm's recursive walk and its final rmdir — which+ // failed this hook, intermittently, only in the full parallel suite.+ await whenRegistryIdle();await fs.rm(dir, { recursive: true, force: true });vi.restoreAllMocks();});diff --git a/electron/media/mediaLinksRegistry.test.ts b/electron/media/mediaLinksRegistry.test.tsindex 0e50905db..7c76a7580 100644--- a/electron/media/mediaLinksRegistry.test.ts+++ b/electron/media/mediaLinksRegistry.test.ts@@ -7,6 +7,7 @@ import {findMediaLinksByFingerprint,findRelocatedMediaByStoredPath,registerMediaLinks,+ whenRegistryIdle,} from "./mediaLinksRegistry";async function makeTempDir(): Promise<string> {@@ -262,6 +263,12 @@ describe("mediaLinksRegistry", () => {process.on("unhandledRejection", onRejection);try {await fn();+ // The refresh these cases are about is deliberately not awaited by the+ // lookup, so `fn` returns while it is still queued. Waiting for the+ // queue to drain is what makes "did the refresh warn / write?"+ // answerable at all — the 50 ms below used to be doing that job by+ // accident, and lost the race whenever the suite ran under load.+ await whenRegistryIdle();// Node decides a rejection is unhandled a tick after the microtask// queue drains, so the assertion needs a real timer, not a flush.await new Promise((resolve) => setTimeout(resolve, 50));@@ -304,6 +311,26 @@ describe("mediaLinksRegistry", () => {}});+ // The drain the two cases above rely on. Without it there is no way to know+ // the refresh has landed: the lookup returns while the write is still+ // queued, so a caller that removes the directory races it and a test that+ // asserts on its outcome is asserting on a coin flip. Both were real+ // intermittent failures in the full suite (this file, and cursorSidecar's+ // `afterEach` failing with ENOTEMPTY), green in isolation every time.+ it("whenRegistryIdle waits for a refresh the lookup did not await", async () => {+ const { original, moved } = await registerThenMove();+ const recorded = async () =>+ JSON.parse(await fs.readFile(path.join(tempDir, "media-links.registry.json"), "utf-8"))+ .entries[0].lastKnownPath;++ expect(await recorded()).toBe(original);+ await findMediaLinksByFingerprint(tempDir, moved);+ await whenRegistryIdle(tempDir);++ // Durably on disk, not "probably by now".+ expect(await recorded()).toBe(moved);+ });+it("survives the directory disappearing while the refresh is queued", async () => {// The CI shape: a suite's `afterEach` removes its temp dir while a write// is still in the queue. Whoever wins the race is fine — what must notdiff --git a/electron/media/mediaLinksRegistry.ts b/electron/media/mediaLinksRegistry.tsindex c2507d138..ec0ad76de 100644--- a/electron/media/mediaLinksRegistry.ts+++ b/electron/media/mediaLinksRegistry.ts@@ -180,6 +180,33 @@ function withWriteLock<T>(baseDir: string, fn: () => Promise<T>): Promise<T> {return result;}+/**+ * Resolves once every write queued for `baseDir` — or for every directory, with+ * no argument — has drained.+ *+ * `findMediaLinksByFingerprint` refreshes a drifted path WITHOUT awaiting it, on+ * purpose (a lookup must not pay for a write it does not need). That leaves work+ * running after the call that started it returned, and nothing could wait for it:+ * a caller that then removed the directory raced the write, and a test that+ * asserted on the write's outcome was asserting on a coin flip. Both showed up as+ * intermittent failures in the full suite and passed in isolation, which is the+ * signature of exactly this.+ *+ * The queue tails never reject (see `withWriteLock`), so this never throws — it is+ * "the writes are done", not "the writes succeeded".+ */+export async function whenRegistryIdle(baseDir?: string): Promise<void> {+ for (;;) {+ const tails = baseDir ? [writeQueues.get(baseDir)] : [...writeQueues.values()];+ const pending = tails.filter((t): t is Promise<unknown> => t !== undefined);+ if (pending.length === 0) return;+ // A drained write can have queued another behind it, so loop rather than+ // await once. `withWriteLock` drops its own key when the chain goes idle,+ // which is what eventually empties the map and ends this.+ await Promise.all(pending);+ }+}+async function updateRegistry(baseDir: string,mutator: (file: MediaLinksRegistryFile) => MediaLinksRegistryFile,