|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off |
| 2 | +import * as NodeChildProcess from "node:child_process"; |
| 3 | +import * as NodeFS from "node:fs"; |
| 4 | +import * as NodePath from "node:path"; |
| 5 | +import * as NodeReadline from "node:readline"; |
| 6 | +import * as NodeURL from "node:url"; |
| 7 | + |
| 8 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 9 | +import { it } from "@effect/vitest"; |
| 10 | +import * as Deferred from "effect/Deferred"; |
| 11 | +import * as Effect from "effect/Effect"; |
| 12 | +import * as Fiber from "effect/Fiber"; |
| 13 | +import * as FileSystem from "effect/FileSystem"; |
| 14 | +import * as Queue from "effect/Queue"; |
| 15 | +import { describe, expect } from "vite-plus/test"; |
| 16 | + |
| 17 | +import * as DesktopLifetime from "./desktopLifetime.ts"; |
| 18 | + |
| 19 | +// Stands in for the desktop: spawns a backend with the lifetime pipe on fd 6, |
| 20 | +// the way DesktopBackendManager does, then waits to be killed. |
| 21 | +const DESKTOP_SCRIPT = ` |
| 22 | +const backend = require("node:child_process").spawn( |
| 23 | + process.execPath, |
| 24 | + ["--input-type=module", "-e", process.env.T3_TEST_BACKEND_SCRIPT], |
| 25 | + { stdio: ["ignore", "inherit", "inherit", "ignore", "ignore", "ignore", "pipe"] }, |
| 26 | +); |
| 27 | +console.log("backend " + backend.pid); |
| 28 | +setInterval(() => {}, 1 << 30); |
| 29 | +`; |
| 30 | + |
| 31 | +// Runs the real watcher on fd 6 and reports through the stdout it inherits |
| 32 | +// from the desktop, which outlives the desktop. |
| 33 | +const BACKEND_SCRIPT = ` |
| 34 | +import * as Effect from "effect/Effect"; |
| 35 | +import * as Fiber from "effect/Fiber"; |
| 36 | +const { watch } = await import(process.env.T3_TEST_DESKTOP_LIFETIME_MODULE); |
| 37 | +const onDesktopExit = Effect.sync(() => console.log("desktop exited")); |
| 38 | +await Effect.runPromise(Effect.scoped(Effect.flatMap(watch(6, onDesktopExit), Fiber.join))); |
| 39 | +`; |
| 40 | + |
| 41 | +const takeLineMatching = (lines: Queue.Dequeue<string>, matches: (line: string) => boolean) => |
| 42 | + Queue.take(lines).pipe(Effect.repeat({ until: matches })); |
| 43 | + |
| 44 | +describe("DesktopLifetime", () => { |
| 45 | + it.effect("reports the desktop exit when the desktop dies without cleanup", () => |
| 46 | + Effect.gen(function* () { |
| 47 | + const lines = yield* Queue.unbounded<string>(); |
| 48 | + const desktop = yield* Effect.acquireRelease( |
| 49 | + Effect.sync(() => { |
| 50 | + const child = NodeChildProcess.spawn(process.execPath, ["-e", DESKTOP_SCRIPT], { |
| 51 | + cwd: NodePath.join(import.meta.dirname, ".."), |
| 52 | + env: { |
| 53 | + ...process.env, |
| 54 | + T3_TEST_BACKEND_SCRIPT: BACKEND_SCRIPT, |
| 55 | + T3_TEST_DESKTOP_LIFETIME_MODULE: NodeURL.pathToFileURL( |
| 56 | + NodePath.join(import.meta.dirname, "desktopLifetime.ts"), |
| 57 | + ).href, |
| 58 | + }, |
| 59 | + stdio: ["ignore", "pipe", "inherit"], |
| 60 | + }); |
| 61 | + NodeReadline.createInterface({ input: child.stdout }).on("line", (line) => { |
| 62 | + Queue.offerUnsafe(lines, line); |
| 63 | + }); |
| 64 | + return child; |
| 65 | + }), |
| 66 | + (child) => |
| 67 | + Effect.sync(() => { |
| 68 | + child.kill("SIGKILL"); |
| 69 | + }), |
| 70 | + ); |
| 71 | + const backendPid = Number( |
| 72 | + (yield* takeLineMatching(lines, (line) => line.startsWith("backend "))).slice(8), |
| 73 | + ); |
| 74 | + yield* Effect.addFinalizer(() => |
| 75 | + Effect.sync(() => { |
| 76 | + try { |
| 77 | + process.kill(backendPid, "SIGKILL"); |
| 78 | + } catch { |
| 79 | + // Already exited. |
| 80 | + } |
| 81 | + }), |
| 82 | + ); |
| 83 | + |
| 84 | + desktop.kill("SIGKILL"); |
| 85 | + expect(yield* takeLineMatching(lines, (line) => line === "desktop exited")).toBe( |
| 86 | + "desktop exited", |
| 87 | + ); |
| 88 | + }), |
| 89 | + ); |
| 90 | + |
| 91 | + it.effect("keeps running when the descriptor cannot be watched", () => |
| 92 | + Effect.gen(function* () { |
| 93 | + const fs = yield* FileSystem.FileSystem; |
| 94 | + const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-desktop-lifetime-" }); |
| 95 | + const filePath = NodePath.join(tempDir, "not-a-pipe"); |
| 96 | + yield* fs.writeFileString(filePath, ""); |
| 97 | + const fd = yield* Effect.acquireRelease( |
| 98 | + Effect.sync(() => NodeFS.openSync(filePath, "r")), |
| 99 | + (fd) => Effect.sync(() => NodeFS.closeSync(fd)), |
| 100 | + ); |
| 101 | + |
| 102 | + const desktopExited = yield* Deferred.make<void>(); |
| 103 | + const watcher = yield* DesktopLifetime.watch(fd, Deferred.succeed(desktopExited, undefined)); |
| 104 | + yield* Fiber.join(watcher); |
| 105 | + |
| 106 | + expect(yield* Deferred.isDone(desktopExited)).toBe(false); |
| 107 | + }).pipe(Effect.provide(NodeServices.layer)), |
| 108 | + ); |
| 109 | +}); |
0 commit comments