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

launcherWatch.test.ts

65 lines2.2 KB

1import { assert, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Fiber from "effect/Fiber";
4import * as TestClock from "effect/testing/TestClock";
5import * as HostProcess from "@t3tools/shared/HostProcess";
6​
7import { awaitLauncherLoss } from "./launcherWatch.ts";
8​
9const launcherPid = 4100;
10// Orphans are adopted by init or a subreaper, so the new parent is not always 1.
11const subreaperPid = 1800;
12​
13const watch = (getParentPid: () => number, platform: NodeJS.Platform, startPid = launcherPid) =>
14 awaitLauncherLoss({ launcherPid: startPid, getParentPid }).pipe(
15 Effect.provideService(HostProcess.Platform, platform),
16 );
17​
18it.effect("completes when the parent changes, including to a subreaper", () =>
19 Effect.gen(function* () {
20 let parentPid = launcherPid;
21 const fiber = yield* Effect.forkChild(watch(() => parentPid, "linux"));
22 yield* Effect.yieldNow;
23​
24 yield* TestClock.adjust("10 seconds");
25 assert.isUndefined(fiber.pollUnsafe(), "still watching while the launcher is alive");
26​
27 parentPid = subreaperPid;
28 yield* TestClock.adjust("2 seconds");
29 yield* Fiber.join(fiber);
30 }),
31);
32​
33it.effect("notices a launcher that was already gone when the watch began", () =>
34 Effect.gen(function* () {
35 const fiber = yield* Effect.forkChild(watch(() => subreaperPid, "linux"));
36 yield* Effect.yieldNow;
37 yield* Fiber.join(fiber);
38 }),
39);
40​
41it.effect("does not watch a backend that already starts under init", () =>
42 Effect.gen(function* () {
43 let parentPid = 1;
44 const fiber = yield* Effect.forkChild(watch(() => parentPid, "linux", 1));
45 yield* Effect.yieldNow;
46​
47 parentPid = subreaperPid;
48 yield* TestClock.adjust("1 minute");
49 assert.isUndefined(fiber.pollUnsafe());
50 yield* Fiber.interrupt(fiber);
51 }),
52);
53​
54it.effect("does not watch on Windows, where the original parent id outlives the parent", () =>
55 Effect.gen(function* () {
56 let parentPid = launcherPid;
57 const fiber = yield* Effect.forkChild(watch(() => parentPid, "win32"));
58 yield* Effect.yieldNow;
59​
60 parentPid = subreaperPid;
61 yield* TestClock.adjust("1 minute");
62 assert.isUndefined(fiber.pollUnsafe());
63 yield* Fiber.interrupt(fiber);
64 }),
65);