sailfork / t3code-github / .repos/ alchemy-effect/ packages/ alchemy/ test/ Auth/ Lock.test.ts

import { spawn } from "node:child_process";
import { resolve } from "node:path";
import { assert, describe, expect, it, layer } from "@alchemy.run/test";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Clock from "effect/Clock";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Logger from "effect/Logger";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Schedule from "effect/Schedule";
import { sanitizeLockKey, withLock } from "@/Auth/Lock.ts";
import { rootDir } from "@/Auth/Paths.ts";
import { recordOf } from "@/Util/LogRecord.ts";

describe("sanitizeLockKey", { tags: ["unit", "local"] }, () => {
  it("leaves conventional keys untouched", () => {
    expect(sanitizeLockKey("default-Cloudflare")).toBe("default-Cloudflare");
    expect(sanitizeLockKey("my_profile.v2-AWS")).toBe("my_profile.v2-AWS");
  });

  it("neutralises unexpanded shell placeholders", () => {
    // Seen verbatim in production: EINVAL mkdir
    // '...\${ALCHEMY_PROFILE:-default}-Cloudflare.lock.lock' on Windows,
    // where `:`/`

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/alchemy/test/Auth/Lock.test.ts at main · sailfork/t3code-github · Sailfork
	
		
/`{`/`}` are invalid in file names. const sanitized = sanitizeLockKey("${ALCHEMY_PROFILE:-default}-Cloudflare"); expect(sanitized).toBe("__ALCHEMY_PROFILE_-default_-Cloudflare"); expect(sanitized).not.toMatch(/[<>:"/\\|?*${}]/); }); it("replaces path separators so keys cannot escape the lock dir", () => { expect(sanitizeLockKey("../../etc/passwd")).toBe(".._.._etc_passwd"); }); }); // Scenarios adapted from proper-lockfile's suite // (https://github.com/moxystudio/node-proper-lockfile/tree/master/test) to // the Effect lock: withLock is scoped acquire+release, the lockfile is a // directory with an owner marker, staleness is 30s, the heartbeat is 10s. layer(NodeServices.layer, { excludeTestServices: true })("withLock", (it) => { const lockPathOf = Effect.fn(function* (key: string) { const path = yield* Path.Path; return path.join(rootDir(), "lock", `${sanitizeLockKey(key)}.lock`); }); /** Create a foreign lock as another process would have left it. */ const plantForeignLock = Effect.fn(function* ( lockPath: string, options?: { readonly ageMillis?: number }, ) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; yield* fs.makeDirectory(lockPath, { recursive: true }); yield* fs.writeFileString(path.join(lockPath, "owner"), "foreign-owner"); if (options?.ageMillis !== undefined) { const stamp = new Date((yield* Clock.currentTimeMillis) - options.ageMillis); yield* fs.utimes(lockPath, stamp, stamp); } }); const removeLock = Effect.fn(function* (lockPath: string) { const fs = yield* FileSystem.FileSystem; yield* fs.remove(lockPath, { recursive: true, force: true }); }); it.effect( "acquires and releases a lock whose key contains characters invalid in file names", () => Effect.gen(function* () { // Regression for the production EINVAL: this key is unusable as a // raw Windows file name without sanitisation. const result = yield* withLock( "${ALCHEMY_PROFILE:-default}-LockTest", Effect.succeed("ran"), ); expect(result).toBe("ran"); }), { tags: ["unit", "local"] }, ); it.effect( "creates the lock directory while held and removes it on release", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const key = "lock-test-lifecycle"; const lockPath = yield* lockPathOf(key); const seen = yield* withLock( key, Effect.all({ lock: fs.exists(lockPath), owner: fs .readFileString(`${lockPath}/owner`) .pipe(Effect.map((owner) => owner.length > 0)), }), ); expect(seen).toEqual({ lock: true, owner: true }); expect(yield* fs.exists(lockPath)).toBe(false); }), { tags: ["unit", "local"] }, ); it.effect( "serialises same-key critical sections in-process", () => Effect.gen(function* () { const order: number[] = []; const critical = (i: number) => withLock( "lock-test-serialise", Effect.gen(function* () { order.push(i); yield* Effect.sleep("50 millis"); order.push(i); }), ); yield* Effect.all([critical(1), critical(2)], { concurrency: "unbounded" }); // Each critical section's two entries must be adjacent — no // interleaving between holders. expect(order.slice(0, 2)).toEqual([order[0], order[0]]); expect(order.slice(2)).toEqual([order[2], order[2]]); }), { tags: ["unit", "local"] }, ); it.effect( "allows different-key critical sections to overlap", () => Effect.gen(function* () { const firstEntered = yield* Deferred.make<void>(); const releaseFirst = yield* Deferred.make<void>(); const first = yield* withLock( "lock-test-independent-a", Deferred.succeed(firstEntered, undefined).pipe( Effect.andThen(Deferred.await(releaseFirst)), ), ).pipe(Effect.forkScoped); yield* Deferred.await(firstEntered); const secondRan = yield* withLock("lock-test-independent-b", Effect.succeed(true)); expect(secondRan).toBe(true); yield* Deferred.succeed(releaseFirst, undefined); yield* Fiber.await(first); }), { tags: ["unit", "local"] }, ); it.effect( "excludes another process and recovers its lock after a crash", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const key = `lock-test-process-${crypto.randomUUID()}`; const lockPath = yield* lockPathOf(key); const child = spawn(process.execPath, ["test/Auth/fixtures/lock-holder.ts", key], { cwd: resolve(import.meta.dir, "../.."), stdio: ["ignore", "pipe", "inherit"], }); yield* Effect.addFinalizer(() => Effect.sync(() => { if (child.exitCode === null) child.kill("SIGKILL"); }), ); yield* Effect.tryPromise( () => new Promise<void>((resolve, reject) => { child.once("error", reject); child.once("exit", (code) => reject(new Error(`lock holder exited before ready: ${code}`)), ); child.stdout.once("data", (chunk) => { if (chunk.toString().includes("ready")) resolve(); }); }), ); const blocked = yield* withLock(key, Effect.void, { timeout: "250 millis" }).pipe( Effect.exit, ); assert(Exit.isFailure(blocked)); expect(String(blocked.cause)).toContain("Timed out waiting"); child.kill("SIGKILL"); yield* Effect.tryPromise( () => new Promise<void>((resolve) => { if (child.exitCode !== null) resolve(); else child.once("exit", () => resolve()); }), ); expect(yield* fs.exists(lockPath)).toBe(true); const stale = new Date((yield* Clock.currentTimeMillis) - 60_000); yield* fs.utimes(lockPath, stale, stale); expect(yield* withLock(key, Effect.succeed("recovered"), { timeout: "2 seconds" })).toBe( "recovered", ); expect(yield* fs.exists(lockPath)).toBe(false); }).pipe(Effect.scoped), { tags: ["unit", "local"], timeout: 10_000 }, ); it.effect( "releases the lock when the critical section fails", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const key = "lock-test-release-on-failure"; const lockPath = yield* lockPathOf(key); const exit = yield* withLock(key, Effect.fail("boom")).pipe(Effect.exit); assert(Exit.isFailure(exit)); expect(String(exit.cause)).toContain("boom"); expect(yield* fs.exists(lockPath)).toBe(false); }), { tags: ["unit", "local"] }, ); it.effect( "releases the lock when the holder is interrupted", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const key = "lock-test-release-on-interrupt"; const lockPath = yield* lockPathOf(key); const entered = yield* Deferred.make<void>(); const fiber = yield* withLock( key, Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)), ).pipe(Effect.forkScoped); yield* Deferred.await(entered); yield* Fiber.interrupt(fiber); expect(yield* fs.exists(lockPath)).toBe(false); }), { tags: ["unit", "local"] }, ); it.effect( "releases the lock when interrupted during acquisition", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const key = "lock-test-interrupt-acquisition"; const lockPath = yield* lockPathOf(key); const created = yield* Deferred.make<void>(); const resume = yield* Deferred.make<void>(); yield* Effect.addFinalizer(() => removeLock(lockPath).pipe(Effect.orDie)); const acquisitionFs: FileSystem.FileSystem = { ...fs, makeDirectory: (directory, options) => fs .makeDirectory(directory, options) .pipe( Effect.andThen( directory === lockPath ? Deferred.succeed(created, undefined).pipe( Effect.andThen(Deferred.await(resume)), ) : Effect.void, ), ), }; const holder = yield* withLock(key, Effect.never).pipe( Effect.provideService(FileSystem.FileSystem, acquisitionFs), Effect.forkScoped, ); yield* Deferred.await(created); const interrupted = yield* Fiber.interrupt(holder).pipe( Effect.forkScoped({ startImmediately: true }), ); yield* Deferred.succeed(resume, undefined); yield* Fiber.join(interrupted); expect(yield* fs.exists(lockPath)).toBe(false); }).pipe(Effect.scoped), { tags: ["unit", "local"] }, ); it.effect( "dies with a timeout while a live foreign lock is held", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const key = "lock-test-foreign-holder"; const lockPath = yield* lockPathOf(key); yield* plantForeignLock(lockPath); const exit = yield* withLock(key, Effect.void, { timeout: "500 millis" }).pipe(Effect.exit); assert(Exit.isFailure(exit)); expect(String(exit.cause)).toMatch(/Timed out waiting/); // A fresh (non-stale) foreign lock must survive our failed attempt. expect(yield* fs.readFileString(`${lockPath}/owner`)).toBe("foreign-owner"); yield* removeLock(lockPath); }), { tags: ["unit", "local"] }, ); it.effect( "reaps and acquires over a stale lock", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const key = "lock-test-stale"; const lockPath = yield* lockPathOf(key); // A holder that crashed 60s ago: owner file present, mtime past the // 30s stale threshold, nobody refreshing. yield* plantForeignLock(lockPath, { ageMillis: 60_000 }); const result = yield* withLock(key, Effect.succeed("recovered"), { timeout: "5 seconds" }); expect(result).toBe("recovered"); expect(yield* fs.exists(lockPath)).toBe(false); }), { tags: ["unit", "local"] }, ); it.effect( "retries until a foreign lock is released", () => Effect.gen(function* () { const key = "lock-test-wait-for-release"; const lockPath = yield* lockPathOf(key); yield* plantForeignLock(lockPath); yield* removeLock(lockPath).pipe(Effect.delay("300 millis"), Effect.forkScoped); const result = yield* withLock(key, Effect.succeed("ran"), { timeout: "5 seconds" }); expect(result).toBe("ran"); }), { tags: ["unit", "local"] }, ); it.effect( "does not remove a lock it no longer owns on release", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const key = "lock-test-compromised-release"; const lockPath = yield* lockPathOf(key); const release = yield* Deferred.make<void>(); const fiber = yield* withLock(key, Deferred.await(release)).pipe(Effect.forkScoped); yield* fs.exists(lockPath).pipe( Effect.repeat({ schedule: Schedule.spaced("25 millis"), until: (exists) => exists, times: 100, }), ); // Another process reaped our lock as stale and took it over. yield* fs.writeFileString(`${lockPath}/owner`, "foreign-owner"); yield* Deferred.succeed(release, undefined); yield* Fiber.await(fiber); // Release must leave the usurper's lock in place. expect(yield* fs.readFileString(`${lockPath}/owner`)).toBe("foreign-owner"); yield* removeLock(lockPath); }), { tags: ["unit", "local"] }, ); it.effect.skipIf(process.env.FAST)( "refreshes the lock mtime while held", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const key = "lock-test-heartbeat"; const lockPath = yield* lockPathOf(key); const release = yield* Deferred.make<void>(); const fiber = yield* withLock(key, Deferred.await(release)).pipe(Effect.forkScoped); yield* fs.exists(lockPath).pipe( Effect.repeat({ schedule: Schedule.spaced("25 millis"), until: (exists) => exists, times: 100, }), ); // Age the lock past the heartbeat interval; the next refresh must // bump the mtime back to the present so it never appears stale. const before = new Date((yield* Clock.currentTimeMillis) - 25_000); yield* fs.utimes(lockPath, before, before); const refreshed = yield* fs.stat(lockPath).pipe( Effect.map((info) => Option.exists(info.mtime, (mtime) => mtime.getTime() > before.getTime() + 20_000), ), Effect.repeat({ schedule: Schedule.spaced("500 millis"), until: (bumped) => bumped, times: 25, }), ); expect(refreshed).toBe(true); yield* Deferred.succeed(release, undefined); yield* Fiber.await(fiber); }), { tags: ["unit", "local"], timeout: 30_000 }, ); /** * A locked operation that never completes used to be entirely invisible: * the deploy sat there with no output at any log level (#1231). The stall * notice is the backstop that makes a hang self-describing, so it has to * name the operation and it must stay quiet for flows that legitimately * block on the user. */ /** Collects warnings logged while `self` runs. */ const captureConsole = () => { const messages: Array<string> = []; return { messages, provide: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.provide( self, Logger.layer([ Logger.make<unknown, void>((options) => { const record = recordOf(options); if (record.kind === "log" && record.level === "warn") { messages.push(record.data.message); } }), ]), ), }; }; it.effect( "names the stalled operation in a warning", () => Effect.gen(function* () { const console = captureConsole(); yield* console.provide( withLock("stall-notice-key", Effect.sleep("250 millis"), { label: "AWS.read (profile 'probe')", stallInterval: "50 millis", }), ); expect(console.messages.length).toBeGreaterThan(0); expect(console.messages[0]).toContain("AWS.read (profile 'probe')"); expect(console.messages[0]).toContain("stall-notice-key.lock"); yield* removeLock(yield* lockPathOf("stall-notice-key")); }), { tags: ["unit", "local"] }, ); it.effect( "stays silent when the watchdog is disabled", () => Effect.gen(function* () { const console = captureConsole(); yield* console.provide( withLock("stall-quiet-key", Effect.sleep("250 millis"), { watchdog: false, stallInterval: "50 millis", }), ); expect(console.messages).toEqual([]); yield* removeLock(yield* lockPathOf("stall-quiet-key")); }), { tags: ["unit", "local"] }, ); });
.repos/alchemy-effect/packages/alchemy/test/Auth/Lock.test.ts at main · sailfork/t3code-github · Sailfork
/`{`/`}` are invalid in file names.\n const sanitized = sanitizeLockKey(\"${ALCHEMY_PROFILE:-default}-Cloudflare\");\n expect(sanitized).toBe(\"__ALCHEMY_PROFILE_-default_-Cloudflare\");\n expect(sanitized).not.toMatch(/[\u003c>:\"/\\\\|?*${}]/);\n });\n\n it(\"replaces path separators so keys cannot escape the lock dir\", () => {\n expect(sanitizeLockKey(\"../../etc/passwd\")).toBe(\".._.._etc_passwd\");\n });\n});\n\n// Scenarios adapted from proper-lockfile's suite\n// (https://github.com/moxystudio/node-proper-lockfile/tree/master/test) to\n// the Effect lock: withLock is scoped acquire+release, the lockfile is a\n// directory with an owner marker, staleness is 30s, the heartbeat is 10s.\nlayer(NodeServices.layer, { excludeTestServices: true })(\"withLock\", (it) => {\n const lockPathOf = Effect.fn(function* (key: string) {\n const path = yield* Path.Path;\n return path.join(rootDir(), \"lock\", `${sanitizeLockKey(key)}.lock`);\n });\n\n /** Create a foreign lock as another process would have left it. */\n const plantForeignLock = Effect.fn(function* (\n lockPath: string,\n options?: { readonly ageMillis?: number },\n ) {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n yield* fs.makeDirectory(lockPath, { recursive: true });\n yield* fs.writeFileString(path.join(lockPath, \"owner\"), \"foreign-owner\");\n if (options?.ageMillis !== undefined) {\n const stamp = new Date((yield* Clock.currentTimeMillis) - options.ageMillis);\n yield* fs.utimes(lockPath, stamp, stamp);\n }\n });\n\n const removeLock = Effect.fn(function* (lockPath: string) {\n const fs = yield* FileSystem.FileSystem;\n yield* fs.remove(lockPath, { recursive: true, force: true });\n });\n\n it.effect(\n \"acquires and releases a lock whose key contains characters invalid in file names\",\n () =>\n Effect.gen(function* () {\n // Regression for the production EINVAL: this key is unusable as a\n // raw Windows file name without sanitisation.\n const result = yield* withLock(\n \"${ALCHEMY_PROFILE:-default}-LockTest\",\n Effect.succeed(\"ran\"),\n );\n expect(result).toBe(\"ran\");\n }),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect(\n \"creates the lock directory while held and removes it on release\",\n () =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const key = \"lock-test-lifecycle\";\n const lockPath = yield* lockPathOf(key);\n const seen = yield* withLock(\n key,\n Effect.all({\n lock: fs.exists(lockPath),\n owner: fs\n .readFileString(`${lockPath}/owner`)\n .pipe(Effect.map((owner) => owner.length > 0)),\n }),\n );\n expect(seen).toEqual({ lock: true, owner: true });\n expect(yield* fs.exists(lockPath)).toBe(false);\n }),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect(\n \"serialises same-key critical sections in-process\",\n () =>\n Effect.gen(function* () {\n const order: number[] = [];\n const critical = (i: number) =>\n withLock(\n \"lock-test-serialise\",\n Effect.gen(function* () {\n order.push(i);\n yield* Effect.sleep(\"50 millis\");\n order.push(i);\n }),\n );\n yield* Effect.all([critical(1), critical(2)], { concurrency: \"unbounded\" });\n // Each critical section's two entries must be adjacent — no\n // interleaving between holders.\n expect(order.slice(0, 2)).toEqual([order[0], order[0]]);\n expect(order.slice(2)).toEqual([order[2], order[2]]);\n }),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect(\n \"allows different-key critical sections to overlap\",\n () =>\n Effect.gen(function* () {\n const firstEntered = yield* Deferred.make\u003cvoid>();\n const releaseFirst = yield* Deferred.make\u003cvoid>();\n const first = yield* withLock(\n \"lock-test-independent-a\",\n Deferred.succeed(firstEntered, undefined).pipe(\n Effect.andThen(Deferred.await(releaseFirst)),\n ),\n ).pipe(Effect.forkScoped);\n\n yield* Deferred.await(firstEntered);\n const secondRan = yield* withLock(\"lock-test-independent-b\", Effect.succeed(true));\n expect(secondRan).toBe(true);\n\n yield* Deferred.succeed(releaseFirst, undefined);\n yield* Fiber.await(first);\n }),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect(\n \"excludes another process and recovers its lock after a crash\",\n () =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const key = `lock-test-process-${crypto.randomUUID()}`;\n const lockPath = yield* lockPathOf(key);\n const child = spawn(process.execPath, [\"test/Auth/fixtures/lock-holder.ts\", key], {\n cwd: resolve(import.meta.dir, \"../..\"),\n stdio: [\"ignore\", \"pipe\", \"inherit\"],\n });\n\n yield* Effect.addFinalizer(() =>\n Effect.sync(() => {\n if (child.exitCode === null) child.kill(\"SIGKILL\");\n }),\n );\n\n yield* Effect.tryPromise(\n () =>\n new Promise\u003cvoid>((resolve, reject) => {\n child.once(\"error\", reject);\n child.once(\"exit\", (code) =>\n reject(new Error(`lock holder exited before ready: ${code}`)),\n );\n child.stdout.once(\"data\", (chunk) => {\n if (chunk.toString().includes(\"ready\")) resolve();\n });\n }),\n );\n\n const blocked = yield* withLock(key, Effect.void, { timeout: \"250 millis\" }).pipe(\n Effect.exit,\n );\n assert(Exit.isFailure(blocked));\n expect(String(blocked.cause)).toContain(\"Timed out waiting\");\n\n child.kill(\"SIGKILL\");\n yield* Effect.tryPromise(\n () =>\n new Promise\u003cvoid>((resolve) => {\n if (child.exitCode !== null) resolve();\n else child.once(\"exit\", () => resolve());\n }),\n );\n expect(yield* fs.exists(lockPath)).toBe(true);\n\n const stale = new Date((yield* Clock.currentTimeMillis) - 60_000);\n yield* fs.utimes(lockPath, stale, stale);\n expect(yield* withLock(key, Effect.succeed(\"recovered\"), { timeout: \"2 seconds\" })).toBe(\n \"recovered\",\n );\n expect(yield* fs.exists(lockPath)).toBe(false);\n }).pipe(Effect.scoped),\n { tags: [\"unit\", \"local\"], timeout: 10_000 },\n );\n\n it.effect(\n \"releases the lock when the critical section fails\",\n () =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const key = \"lock-test-release-on-failure\";\n const lockPath = yield* lockPathOf(key);\n const exit = yield* withLock(key, Effect.fail(\"boom\")).pipe(Effect.exit);\n assert(Exit.isFailure(exit));\n expect(String(exit.cause)).toContain(\"boom\");\n expect(yield* fs.exists(lockPath)).toBe(false);\n }),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect(\n \"releases the lock when the holder is interrupted\",\n () =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const key = \"lock-test-release-on-interrupt\";\n const lockPath = yield* lockPathOf(key);\n const entered = yield* Deferred.make\u003cvoid>();\n const fiber = yield* withLock(\n key,\n Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)),\n ).pipe(Effect.forkScoped);\n yield* Deferred.await(entered);\n yield* Fiber.interrupt(fiber);\n expect(yield* fs.exists(lockPath)).toBe(false);\n }),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect(\n \"releases the lock when interrupted during acquisition\",\n () =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const key = \"lock-test-interrupt-acquisition\";\n const lockPath = yield* lockPathOf(key);\n const created = yield* Deferred.make\u003cvoid>();\n const resume = yield* Deferred.make\u003cvoid>();\n yield* Effect.addFinalizer(() => removeLock(lockPath).pipe(Effect.orDie));\n const acquisitionFs: FileSystem.FileSystem = {\n ...fs,\n makeDirectory: (directory, options) =>\n fs\n .makeDirectory(directory, options)\n .pipe(\n Effect.andThen(\n directory === lockPath\n ? Deferred.succeed(created, undefined).pipe(\n Effect.andThen(Deferred.await(resume)),\n )\n : Effect.void,\n ),\n ),\n };\n const holder = yield* withLock(key, Effect.never).pipe(\n Effect.provideService(FileSystem.FileSystem, acquisitionFs),\n Effect.forkScoped,\n );\n yield* Deferred.await(created);\n const interrupted = yield* Fiber.interrupt(holder).pipe(\n Effect.forkScoped({ startImmediately: true }),\n );\n yield* Deferred.succeed(resume, undefined);\n yield* Fiber.join(interrupted);\n expect(yield* fs.exists(lockPath)).toBe(false);\n }).pipe(Effect.scoped),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect(\n \"dies with a timeout while a live foreign lock is held\",\n () =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const key = \"lock-test-foreign-holder\";\n const lockPath = yield* lockPathOf(key);\n yield* plantForeignLock(lockPath);\n\n const exit = yield* withLock(key, Effect.void, { timeout: \"500 millis\" }).pipe(Effect.exit);\n assert(Exit.isFailure(exit));\n expect(String(exit.cause)).toMatch(/Timed out waiting/);\n // A fresh (non-stale) foreign lock must survive our failed attempt.\n expect(yield* fs.readFileString(`${lockPath}/owner`)).toBe(\"foreign-owner\");\n yield* removeLock(lockPath);\n }),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect(\n \"reaps and acquires over a stale lock\",\n () =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const key = \"lock-test-stale\";\n const lockPath = yield* lockPathOf(key);\n // A holder that crashed 60s ago: owner file present, mtime past the\n // 30s stale threshold, nobody refreshing.\n yield* plantForeignLock(lockPath, { ageMillis: 60_000 });\n\n const result = yield* withLock(key, Effect.succeed(\"recovered\"), { timeout: \"5 seconds\" });\n expect(result).toBe(\"recovered\");\n expect(yield* fs.exists(lockPath)).toBe(false);\n }),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect(\n \"retries until a foreign lock is released\",\n () =>\n Effect.gen(function* () {\n const key = \"lock-test-wait-for-release\";\n const lockPath = yield* lockPathOf(key);\n yield* plantForeignLock(lockPath);\n yield* removeLock(lockPath).pipe(Effect.delay(\"300 millis\"), Effect.forkScoped);\n const result = yield* withLock(key, Effect.succeed(\"ran\"), { timeout: \"5 seconds\" });\n expect(result).toBe(\"ran\");\n }),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect(\n \"does not remove a lock it no longer owns on release\",\n () =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const key = \"lock-test-compromised-release\";\n const lockPath = yield* lockPathOf(key);\n const release = yield* Deferred.make\u003cvoid>();\n const fiber = yield* withLock(key, Deferred.await(release)).pipe(Effect.forkScoped);\n yield* fs.exists(lockPath).pipe(\n Effect.repeat({\n schedule: Schedule.spaced(\"25 millis\"),\n until: (exists) => exists,\n times: 100,\n }),\n );\n // Another process reaped our lock as stale and took it over.\n yield* fs.writeFileString(`${lockPath}/owner`, \"foreign-owner\");\n yield* Deferred.succeed(release, undefined);\n yield* Fiber.await(fiber);\n // Release must leave the usurper's lock in place.\n expect(yield* fs.readFileString(`${lockPath}/owner`)).toBe(\"foreign-owner\");\n yield* removeLock(lockPath);\n }),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect.skipIf(process.env.FAST)(\n \"refreshes the lock mtime while held\",\n () =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const key = \"lock-test-heartbeat\";\n const lockPath = yield* lockPathOf(key);\n const release = yield* Deferred.make\u003cvoid>();\n const fiber = yield* withLock(key, Deferred.await(release)).pipe(Effect.forkScoped);\n yield* fs.exists(lockPath).pipe(\n Effect.repeat({\n schedule: Schedule.spaced(\"25 millis\"),\n until: (exists) => exists,\n times: 100,\n }),\n );\n // Age the lock past the heartbeat interval; the next refresh must\n // bump the mtime back to the present so it never appears stale.\n const before = new Date((yield* Clock.currentTimeMillis) - 25_000);\n yield* fs.utimes(lockPath, before, before);\n const refreshed = yield* fs.stat(lockPath).pipe(\n Effect.map((info) =>\n Option.exists(info.mtime, (mtime) => mtime.getTime() > before.getTime() + 20_000),\n ),\n Effect.repeat({\n schedule: Schedule.spaced(\"500 millis\"),\n until: (bumped) => bumped,\n times: 25,\n }),\n );\n expect(refreshed).toBe(true);\n yield* Deferred.succeed(release, undefined);\n yield* Fiber.await(fiber);\n }),\n { tags: [\"unit\", \"local\"], timeout: 30_000 },\n );\n /**\n * A locked operation that never completes used to be entirely invisible:\n * the deploy sat there with no output at any log level (#1231). The stall\n * notice is the backstop that makes a hang self-describing, so it has to\n * name the operation and it must stay quiet for flows that legitimately\n * block on the user.\n */\n /** Collects warnings logged while `self` runs. */\n const captureConsole = () => {\n const messages: Array\u003cstring> = [];\n return {\n messages,\n provide: \u003cA, E, R>(self: Effect.Effect\u003cA, E, R>) =>\n Effect.provide(\n self,\n Logger.layer([\n Logger.make\u003cunknown, void>((options) => {\n const record = recordOf(options);\n if (record.kind === \"log\" && record.level === \"warn\") {\n messages.push(record.data.message);\n }\n }),\n ]),\n ),\n };\n };\n\n it.effect(\n \"names the stalled operation in a warning\",\n () =>\n Effect.gen(function* () {\n const console = captureConsole();\n yield* console.provide(\n withLock(\"stall-notice-key\", Effect.sleep(\"250 millis\"), {\n label: \"AWS.read (profile 'probe')\",\n stallInterval: \"50 millis\",\n }),\n );\n expect(console.messages.length).toBeGreaterThan(0);\n expect(console.messages[0]).toContain(\"AWS.read (profile 'probe')\");\n expect(console.messages[0]).toContain(\"stall-notice-key.lock\");\n yield* removeLock(yield* lockPathOf(\"stall-notice-key\"));\n }),\n { tags: [\"unit\", \"local\"] },\n );\n\n it.effect(\n \"stays silent when the watchdog is disabled\",\n () =>\n Effect.gen(function* () {\n const console = captureConsole();\n yield* console.provide(\n withLock(\"stall-quiet-key\", Effect.sleep(\"250 millis\"), {\n watchdog: false,\n stallInterval: \"50 millis\",\n }),\n );\n expect(console.messages).toEqual([]);\n yield* removeLock(yield* lockPathOf(\"stall-quiet-key\"));\n }),\n { tags: [\"unit\", \"local\"] },\n );\n});\n","preview":null,"history":null,"version":"fc671f23ca9bd2c9de762f400e6756be67baa574"},"/_session?shell=1":null}