|
1 | 1 | // @effect-diagnostics nodeBuiltinImport:off - Exercise the launcher's native filesystem and SQLite process boundary. |
| 2 | +import * as NodeChildProcess from "node:child_process"; |
2 | 3 | import * as NodeFSP from "node:fs/promises"; |
3 | 4 | import * as NodeOS from "node:os"; |
4 | 5 | import * as NodePath from "node:path"; |
5 | 6 | import * as NodeSqlite from "node:sqlite"; |
6 | 7 |
|
7 | 8 | import { afterEach, expect, it } from "@effect/vitest"; |
| 9 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
8 | 10 |
|
9 | 11 | import { Launcher, readServiceState, writeServiceState } from "./serviceLauncher.ts"; |
10 | 12 | import { backupDatabaseForMigrationsFork } from "./serviceLauncherBackup.fork.ts"; |
@@ -193,3 +195,69 @@ process.exit(1); |
193 | 195 | expect(await NodeFSP.readFile(livePath, "utf8")).toBe("live"); |
194 | 196 | expect(backedUp).toBe(true); |
195 | 197 | }); |
| 198 | + |
| 199 | +// A FIFO database keeps `cp` reading until the test closes its writer, so the stop lands mid-copy. |
| 200 | +it.runIf(HostProcess.Platform.defaultValue() === "linux")( |
| 201 | + "a stop during the backup aborts the copy and leaves the update pending", |
| 202 | + async () => { |
| 203 | + const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-launcher-fork-stop-")); |
| 204 | + roots.push(root); |
| 205 | + const databasePath = NodePath.join(root, "state.sqlite"); |
| 206 | + NodeChildProcess.execFileSync("mkfifo", [databasePath]); |
| 207 | + const trialStarted = NodePath.join(root, "trial-started"); |
| 208 | + for (const version of ["1.0.0", "1.1.0"]) { |
| 209 | + const versionDir = NodePath.join(root, "runtime", "versions", version); |
| 210 | + await NodeFSP.mkdir(versionDir, { recursive: true }); |
| 211 | + await NodeFSP.writeFile(NodePath.join(versionDir, ".install-complete"), version); |
| 212 | + await NodeFSP.writeFile( |
| 213 | + NodePath.join(versionDir, "t3"), |
| 214 | + `#!${process.execPath} |
| 215 | +if (process.argv[2] === "__service-preflight") process.exit(1); |
| 216 | +const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT); |
| 217 | +if (context.update?.status === "pending") require("node:fs").writeFileSync(${JSON.stringify(trialStarted)}, ""); |
| 218 | +`, |
| 219 | + { mode: 0o755 }, |
| 220 | + ); |
| 221 | + } |
| 222 | + const statePath = NodePath.join(root, "runtime", "service-state.json"); |
| 223 | + await writeServiceState(statePath, { |
| 224 | + protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 225 | + activeVersion: "1.0.0", |
| 226 | + update: { |
| 227 | + id: "update-1", |
| 228 | + fromVersion: "1.0.0", |
| 229 | + targetVersion: "1.1.0", |
| 230 | + dbPath: databasePath, |
| 231 | + status: "pending", |
| 232 | + }, |
| 233 | + }); |
| 234 | + const launcher = new Launcher(root, await readServiceState(statePath)); |
| 235 | + const running = launcher.run(); |
| 236 | + // Opening the FIFO for writing resolves once `cp` has opened it to read. Holding it open |
| 237 | + // keeps the copy blocked, so only an abort lets the stop finish. |
| 238 | + const writer = await NodeFSP.open(databasePath, "w"); |
| 239 | + try { |
| 240 | + await launcher.stop("SIGTERM"); |
| 241 | + await running; |
| 242 | + } finally { |
| 243 | + await writer.close(); |
| 244 | + } |
| 245 | + |
| 246 | + const state = await readServiceState(statePath); |
| 247 | + expect(state.activeVersion).toBe("1.0.0"); |
| 248 | + expect(state.update?.status).toBe("pending"); |
| 249 | + const backupDir = NodePath.join(root, "runtime", "db-backup", "update-1"); |
| 250 | + for (const path of [trialStarted, backupDir, `${backupDir}.staging`]) { |
| 251 | + await expect(NodeFSP.stat(path)).rejects.toMatchObject({ code: "ENOENT" }); |
| 252 | + } |
| 253 | + |
| 254 | + // The next start resumes the update: a full backup, then the trial. |
| 255 | + await NodeFSP.rm(databasePath); |
| 256 | + await NodeFSP.writeFile(databasePath, "database"); |
| 257 | + const resumed = new Launcher(root, await readServiceState(statePath)); |
| 258 | + await expect(resumed.run()).rejects.toThrow("Active child exited unexpectedly"); |
| 259 | + await NodeFSP.stat(trialStarted); |
| 260 | + expect((await readServiceState(statePath)).update?.status).toBe("rolled-back"); |
| 261 | + expect(await NodeFSP.readFile(databasePath, "utf8")).toBe("database"); |
| 262 | + }, |
| 263 | +); |
0 commit comments