|
| 1 | +// @effect-diagnostics-next-line nodeBuiltinImport:off - A real Node process verifies OS signal ownership outside Effect's process runtime. |
| 2 | +import * as NodeChildProcess from "node:child_process"; |
| 3 | +import * as NodeReadline from "node:readline"; |
| 4 | +import * as NodeURL from "node:url"; |
| 5 | +import { expect, it } from "@effect/vitest"; |
| 6 | + |
| 7 | +it("leaves SIGTERM to the lifecycle until its graceful shutdown completes", async ({ |
| 8 | + onTestFinished, |
| 9 | +}) => { |
| 10 | + const runner = new URL("./DesktopRuntime.ts", import.meta.url).href; |
| 11 | + const child = NodeChildProcess.spawn( |
| 12 | + process.execPath, |
| 13 | + [ |
| 14 | + "--experimental-strip-types", |
| 15 | + "--input-type=module", |
| 16 | + "-e", |
| 17 | + ` |
| 18 | + import * as Effect from 'effect/Effect'; |
| 19 | + import * as Deferred from 'effect/Deferred'; |
| 20 | + import { runMain } from ${JSON.stringify(runner)}; |
| 21 | + let released = false; |
| 22 | + runMain(Effect.scoped(Effect.gen(function* () { |
| 23 | + const done = yield* Deferred.make(); |
| 24 | + yield* Effect.addFinalizer(() => Effect.sync(() => console.log(released ? 'CLEANED' : 'PREMATURE'))); |
| 25 | + process.once('SIGTERM', () => { |
| 26 | + console.log('STOPPING'); |
| 27 | + process.stdin.once('data', () => { |
| 28 | + released = true; |
| 29 | + void Effect.runPromise(Deferred.succeed(done, undefined)).then(() => process.stdin.destroy()); |
| 30 | + }); |
| 31 | + }); |
| 32 | + console.log('READY'); |
| 33 | + yield* Deferred.await(done); |
| 34 | + }))); |
| 35 | + `, |
| 36 | + ], |
| 37 | + { |
| 38 | + cwd: NodeURL.fileURLToPath(new URL("../../", import.meta.url)), |
| 39 | + stdio: ["pipe", "pipe", "pipe"], |
| 40 | + }, |
| 41 | + ); |
| 42 | + const exited = new Promise<readonly [number | null, string | null]>((resolve, reject) => { |
| 43 | + child.once("exit", (code, signal) => resolve([code, signal])); |
| 44 | + child.once("error", reject); |
| 45 | + }); |
| 46 | + const lines: string[] = []; |
| 47 | + let stderr = ""; |
| 48 | + child.stderr.on("data", (chunk) => { |
| 49 | + stderr += chunk; |
| 50 | + }); |
| 51 | + onTestFinished(async () => { |
| 52 | + if (child.exitCode === null && child.signalCode === null) { |
| 53 | + child.kill("SIGKILL"); |
| 54 | + await exited; |
| 55 | + } |
| 56 | + }); |
| 57 | + for await (const line of NodeReadline.createInterface({ input: child.stdout })) { |
| 58 | + lines.push(line); |
| 59 | + if (line === "READY") child.kill("SIGTERM"); |
| 60 | + if (line === "STOPPING") child.stdin.write("finish"); |
| 61 | + } |
| 62 | + expect(await exited, stderr).toEqual([0, null]); |
| 63 | + expect(lines).toEqual(["READY", "STOPPING", "CLEANED"]); |
| 64 | +}); |
0 commit comments