|
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 | | - |
6 | 1 | import { assert, describe, it } from "@effect/vitest"; |
7 | | -import * as HostProcess from "@t3tools/shared/HostProcess"; |
8 | 2 | import * as Deferred from "effect/Deferred"; |
9 | 3 | import * as Effect from "effect/Effect"; |
10 | | -import * as Fiber from "effect/Fiber"; |
11 | 4 | import * as Layer from "effect/Layer"; |
12 | 5 | import * as Ref from "effect/Ref"; |
13 | 6 |
|
@@ -110,67 +103,6 @@ function layerDesktopWindow( |
110 | 103 | } |
111 | 104 |
|
112 | 105 | describe("DesktopLifecycle", () => { |
113 | | - it.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
114 | | - "leaves SIGTERM to the lifecycle until its graceful shutdown completes", |
115 | | - async ({ onTestFinished }) => { |
116 | | - const runner = new URL("./DesktopRuntime.ts", import.meta.url).href; |
117 | | - const child = NodeChildProcess.spawn( |
118 | | - process.execPath, |
119 | | - [ |
120 | | - "--experimental-strip-types", |
121 | | - "--input-type=module", |
122 | | - "-e", |
123 | | - ` |
124 | | - import * as Effect from 'effect/Effect'; |
125 | | - import * as Deferred from 'effect/Deferred'; |
126 | | - import { runMain } from ${JSON.stringify(runner)}; |
127 | | - process.stdin.resume(); |
128 | | - let released = false; |
129 | | - runMain(Effect.scoped(Effect.gen(function* () { |
130 | | - const done = yield* Deferred.make(); |
131 | | - yield* Effect.addFinalizer(() => Effect.sync(() => console.log(released ? 'CLEANED' : 'PREMATURE'))); |
132 | | - process.once('SIGTERM', () => { |
133 | | - console.log('STOPPING'); |
134 | | - process.stdin.once('data', () => { |
135 | | - released = true; |
136 | | - void Effect.runPromise(Deferred.succeed(done, undefined)).then(() => process.stdin.destroy()); |
137 | | - }); |
138 | | - }); |
139 | | - console.log('READY'); |
140 | | - yield* Deferred.await(done); |
141 | | - }))); |
142 | | - `, |
143 | | - ], |
144 | | - { |
145 | | - cwd: NodeURL.fileURLToPath(new URL("../../", import.meta.url)), |
146 | | - stdio: ["pipe", "pipe", "pipe"], |
147 | | - }, |
148 | | - ); |
149 | | - const exited = new Promise<readonly [number | null, string | null]>((resolve, reject) => { |
150 | | - child.once("exit", (code, signal) => resolve([code, signal])); |
151 | | - child.once("error", reject); |
152 | | - }); |
153 | | - const lines: string[] = []; |
154 | | - let stderr = ""; |
155 | | - child.stderr.on("data", (chunk) => { |
156 | | - stderr += chunk; |
157 | | - }); |
158 | | - onTestFinished(async () => { |
159 | | - if (child.exitCode === null && child.signalCode === null) { |
160 | | - child.kill("SIGKILL"); |
161 | | - await exited; |
162 | | - } |
163 | | - }); |
164 | | - for await (const line of NodeReadline.createInterface({ input: child.stdout })) { |
165 | | - lines.push(line); |
166 | | - if (line === "READY") child.kill("SIGTERM"); |
167 | | - if (line === "STOPPING") child.stdin.write("finish"); |
168 | | - } |
169 | | - assert.deepEqual(await exited, [0, null], stderr); |
170 | | - assert.deepEqual(lines, ["READY", "STOPPING", "CLEANED"]); |
171 | | - }, |
172 | | - ); |
173 | | - |
174 | 106 | it.effect.each(["darwin", "win32", "linux"] satisfies ReadonlyArray<NodeJS.Platform>)( |
175 | 107 | "lets the updater's quit event proceed on %s", |
176 | 108 | (platform) => { |
@@ -226,18 +158,12 @@ describe("DesktopLifecycle", () => { |
226 | 158 | }, |
227 | 159 | ); |
228 | 160 |
|
229 | | - it.effect("destroys windows and retains signal ownership until backend shutdown completes", () => |
| 161 | + it.effect("destroys windows before waiting for backend shutdown", () => |
230 | 162 | Effect.gen(function* () { |
231 | 163 | const appListeners = new Map<string, (...args: readonly unknown[]) => void>(); |
232 | 164 | const shutdownRequested = yield* Deferred.make<void>(); |
233 | 165 | const allowShutdown = yield* Deferred.make<void>(); |
234 | 166 | const quitRequested = yield* Deferred.make<void>(); |
235 | | - const backendStopping = yield* Deferred.make<void>(); |
236 | | - const finishBackendShutdown = yield* Deferred.make<void>(); |
237 | | - const originalSignals = { |
238 | | - SIGINT: process.listeners("SIGINT"), |
239 | | - SIGTERM: process.listeners("SIGTERM"), |
240 | | - }; |
241 | 167 | const events: string[] = []; |
242 | 168 |
|
243 | 169 | const quit = Effect.sync(() => { |
@@ -278,47 +204,20 @@ describe("DesktopLifecycle", () => { |
278 | 204 | yield* Effect.scoped( |
279 | 205 | Effect.gen(function* () { |
280 | 206 | const lifecycle = yield* DesktopLifecycle.DesktopLifecycle; |
281 | | - const shutdown = yield* DesktopShutdown.DesktopShutdown; |
282 | | - const program = yield* Effect.scoped( |
283 | | - Effect.gen(function* () { |
284 | | - yield* Effect.addFinalizer(() => |
285 | | - Deferred.succeed(backendStopping, undefined).pipe( |
286 | | - Effect.andThen(Deferred.await(finishBackendShutdown)), |
287 | | - Effect.andThen(shutdown.markComplete), |
288 | | - ), |
289 | | - ); |
290 | | - yield* lifecycle.register; |
291 | | - const event = { preventDefault: () => undefined } as Electron.Event; |
292 | | - appListeners.get("before-quit")?.(event); |
293 | | - yield* shutdown.awaitRequest; |
294 | | - }), |
295 | | - ).pipe(Effect.forkScoped); |
296 | | - |
297 | | - yield* Deferred.await(backendStopping); |
298 | | - yield* Effect.addFinalizer(() => |
299 | | - Deferred.succeed(finishBackendShutdown, undefined).pipe(Effect.asVoid), |
300 | | - ); |
| 207 | + yield* lifecycle.register; |
301 | 208 |
|
302 | | - for (const signal of ["SIGINT", "SIGTERM"] as const) { |
303 | | - const listeners = process |
304 | | - .listeners(signal) |
305 | | - .filter((listener) => !originalSignals[signal].includes(listener)); |
306 | | - assert.lengthOf(listeners, 1, `${signal} must remain owned during backend cleanup`); |
307 | | - listeners[0]?.(signal); |
308 | | - } |
309 | | - assert.deepEqual(events, ["flush", "destroy", "request"]); |
310 | | - assert.equal(appListeners.size, 0, "Electron listeners still follow the program scope"); |
311 | | - assert.isFalse(yield* shutdown.isComplete); |
| 209 | + const event = { preventDefault: () => undefined } as Electron.Event; |
| 210 | + appListeners.get("before-quit")?.(event); |
312 | 211 |
|
313 | | - yield* Deferred.succeed(finishBackendShutdown, undefined); |
314 | | - yield* Fiber.join(program); |
| 212 | + yield* Deferred.await(shutdownRequested); |
| 213 | + const eventsBeforeCleanup = [...events]; |
| 214 | + yield* Deferred.succeed(allowShutdown, undefined); |
315 | 215 | yield* Deferred.await(quitRequested); |
| 216 | + |
| 217 | + assert.deepEqual(eventsBeforeCleanup, ["flush", "destroy", "request"]); |
316 | 218 | assert.deepEqual(events, ["flush", "destroy", "request", "quit"]); |
317 | 219 | }), |
318 | 220 | ).pipe(Effect.provide(layer)); |
319 | | - |
320 | | - assert.deepEqual(process.listeners("SIGINT"), originalSignals.SIGINT); |
321 | | - assert.deepEqual(process.listeners("SIGTERM"), originalSignals.SIGTERM); |
322 | 221 | }), |
323 | 222 | ); |
324 | 223 |
|
|
0 commit comments