|
1 | | -// @effect-diagnostics-next-line nodeBuiltinImport:off - a skip condition must be known before the test runs. |
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - a skip condition must be known before the test runs, and the spawner's checks are node:fs/promises calls. |
2 | 2 | import * as NodeChildProcess from "node:child_process"; |
| 3 | +import * as NodeFSP from "node:fs/promises"; |
3 | 4 |
|
4 | 5 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
5 | 6 | import { describe, expect, it } from "@effect/vitest"; |
| 7 | +import { vi } from "vite-plus/test"; |
6 | 8 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 9 | +import * as Deferred from "effect/Deferred"; |
7 | 10 | import * as Effect from "effect/Effect"; |
| 11 | +import * as Fiber from "effect/Fiber"; |
8 | 12 | import * as FileSystem from "effect/FileSystem"; |
9 | 13 | import * as Layer from "effect/Layer"; |
| 14 | +import * as Path from "effect/Path"; |
10 | 15 | import * as PlatformError from "effect/PlatformError"; |
11 | 16 | import * as ChildProcess from "effect/process/ChildProcess"; |
12 | 17 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
13 | 18 |
|
14 | 19 | import * as NonBlockingSpawner from "./NonBlockingSpawner.ts"; |
15 | 20 |
|
| 21 | +vi.mock("node:fs/promises", { spy: true }); |
| 22 | + |
16 | 23 | const wrapped = NonBlockingSpawner.layer.pipe(Layer.provideMerge(NodeServices.layer)); |
17 | 24 |
|
18 | 25 | // A host without /usr/bin/env, such as a minimal container, spawns through /bin/sh. |
@@ -186,6 +193,60 @@ describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("NonBlockingSpa |
186 | 193 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
187 | 194 | ); |
188 | 195 |
|
| 196 | + // A hung check holds a libuv threadpool thread, and those also compress WebSocket frames. |
| 197 | + it.live("checks at most two paths at once, however many directories hang", () => |
| 198 | + Effect.gen(function* () { |
| 199 | + const allArrived = yield* Deferred.make<void>(); |
| 200 | + let arrivals = 0; |
| 201 | + // Each spawn resolves its cwd, then starts its check within microtasks. |
| 202 | + const arrivalPath = Layer.effect( |
| 203 | + Path.Path, |
| 204 | + Effect.map(Path.Path, (path) => ({ |
| 205 | + ...path, |
| 206 | + resolve: (...segments: ReadonlyArray<string>) => { |
| 207 | + if (++arrivals === 3) setImmediate(() => Deferred.doneUnsafe(allArrived, Effect.void)); |
| 208 | + return path.resolve(...segments); |
| 209 | + }, |
| 210 | + })), |
| 211 | + ); |
| 212 | + const hung: Array<() => void> = []; |
| 213 | + let releasing = false; |
| 214 | + let running = 0; |
| 215 | + let mostRunning = 0; |
| 216 | + using _access = vi.spyOn(NodeFSP, "access").mockImplementation( |
| 217 | + () => |
| 218 | + new Promise<void>((_resolve, reject) => { |
| 219 | + mostRunning = Math.max(mostRunning, ++running); |
| 220 | + const fail = () => { |
| 221 | + running--; |
| 222 | + reject(Object.assign(new Error("ENOENT"), { code: "ENOENT" })); |
| 223 | + }; |
| 224 | + if (releasing) fail(); |
| 225 | + else hung.push(fail); |
| 226 | + }), |
| 227 | + ); |
| 228 | + const spawns = yield* Effect.forEach( |
| 229 | + ["a", "b", "c"], |
| 230 | + (name) => spawnError(ChildProcess.make("true", [], { cwd: `/t3code-hung-30183/${name}` })), |
| 231 | + { concurrency: "unbounded" }, |
| 232 | + ).pipe( |
| 233 | + Effect.provide( |
| 234 | + NonBlockingSpawner.layer.pipe( |
| 235 | + Layer.provide(arrivalPath), |
| 236 | + Layer.provide(NodeServices.layer), |
| 237 | + ), |
| 238 | + ), |
| 239 | + Effect.forkChild, |
| 240 | + ); |
| 241 | + yield* Deferred.await(allArrived); |
| 242 | + expect(mostRunning).toBe(2); |
| 243 | + releasing = true; |
| 244 | + for (const fail of hung.splice(0)) fail(); |
| 245 | + yield* Fiber.join(spawns); |
| 246 | + expect(mostRunning).toBe(2); |
| 247 | + }), |
| 248 | + ); |
| 249 | + |
189 | 250 | it.live("only ever hands the native spawner a trampoline, without a cwd", () => |
190 | 251 | Effect.gen(function* () { |
191 | 252 | const fileSystem = yield* FileSystem.FileSystem; |
|
0 commit comments