Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

launcherWatch.test.ts

48 lines1.8 KB

1import { assert, it } from "@effect/vitest";
2import * as Deferred from "effect/Deferred";
3import * as Effect from "effect/Effect";
4import * as Fiber from "effect/Fiber";
5import * as TestClock from "effect/testing/TestClock";
6import * as HostProcess from "@t3tools/shared/HostProcess";
7​
8import { awaitLauncherLoss, stopOnLauncherLoss } from "./launcherWatch.ts";
9​
10const launcherPid = 4100;
11// Orphans are adopted by init or a subreaper, so the new parent is not always 1.
12const subreaperPid = 1800;
13​
14const watch = (getParentPid: () => number, platform: NodeJS.Platform, startPid = launcherPid) =>
15 awaitLauncherLoss({ launcherPid: startPid, getParentPid }).pipe(
16 Effect.provideService(HostProcess.Platform, platform),
17 );
18​
19it.effect("notices a launcher that was already gone when the watch began", () =>
20 Effect.gen(function* () {
21 const fiber = yield* Effect.forkChild(watch(() => subreaperPid, "linux"));
22 yield* Effect.yieldNow;
23 yield* Fiber.join(fiber);
24 }),
25);
26​
27it.effect("exits when shutdown after launcher loss outlasts the grace period", () =>
28 Effect.gen(function* () {
29 let exits = 0;
30 const cleanupDone = yield* Deferred.make<void>();
31 const stuckCleanup = Effect.never.pipe(Effect.onInterrupt(() => Deferred.await(cleanupDone)));
32 const fiber = yield* Effect.forkChild(
33 stopOnLauncherLoss(stuckCleanup, {
34 launcherPid,
35 getParentPid: () => subreaperPid,
36 shutdownGrace: "5 seconds",
37 exit: () => void exits++,
38 }).pipe(Effect.provideService(HostProcess.Platform, "linux")),
39 );
40 yield* Effect.yieldNow;
41 yield* TestClock.adjust("4 seconds");
42 assert.strictEqual(exits, 0);
43 yield* TestClock.adjust("1 second");
44 assert.strictEqual(exits, 1);
45 yield* Deferred.succeed(cleanupDone, undefined);
46 yield* Fiber.await(fiber);
47 }),
48);