|
| 1 | +#!/usr/bin/env node |
| 2 | +// Signal-delivery acceptance gate (plan Phase 3.5). |
| 3 | +// |
| 4 | +// Proves the thing unit tests structurally cannot: that a REAL SIGTERM reaches |
| 5 | +// the Node process under the exec-direct command and triggers the graceful Loki |
| 6 | +// flush + clean exit. The unit suite calls close() in-process; only this gate |
| 7 | +// exercises the OS signal path. |
| 8 | +// |
| 9 | +// Two arms, both run the exec-direct command `node --import tsx src/bin/<entry>`: |
| 10 | +// - FRONTEND: registers its handler synchronously after server.listen(); a live |
| 11 | +// /api/logs-live SSE connection is opened first so the bounded httpServer.close() |
| 12 | +// path is exercised, not bypassed. |
| 13 | +// - INDEXER: registers its handler BEFORE the top-level await of the RPC connect. |
| 14 | +// RPC points at a black-hole server (accepts, never responds) so the process is |
| 15 | +// parked mid-connect when SIGTERM lands — proving the handler attaches at import |
| 16 | +// time, not after the connect resolves. |
| 17 | +// |
| 18 | +// CONTAINER form (CI gate against the built image), same shape: |
| 19 | +// docker run -d --network host -e GRAFANA_LOKI_URL=http://127.0.0.1:<port>/loki/api/v1/push \ |
| 20 | +// -e LOG_SERVICE=indexer-api -e DATABASE_URL=... -e STORE_ADDRESS=0x... \ |
| 21 | +// <image> node --import tsx src/bin/postgres-frontend.ts |
| 22 | +// docker kill --signal=TERM <cid> # SIGTERM to PID 1 (node, via init: true) |
| 23 | +// docker wait <cid> # must print 0 |
| 24 | + |
| 25 | +import { createServer } from "node:http"; |
| 26 | +import { spawn } from "node:child_process"; |
| 27 | +import { fileURLToPath } from "node:url"; |
| 28 | +import path from "node:path"; |
| 29 | + |
| 30 | +const pkgDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
| 31 | +const delay = (ms) => new Promise((r) => setTimeout(r, ms)); |
| 32 | + |
| 33 | +const received = []; |
| 34 | +const loki = createServer((req, res) => { |
| 35 | + let raw = ""; |
| 36 | + req.on("data", (c) => (raw += c)); |
| 37 | + req.on("end", () => { |
| 38 | + received.push(raw); |
| 39 | + res.statusCode = 204; |
| 40 | + res.end("ok"); |
| 41 | + }); |
| 42 | +}); |
| 43 | +await new Promise((r) => loki.listen(0, "127.0.0.1", r)); |
| 44 | +const lokiUrl = `http://127.0.0.1:${loki.address().port}/loki/api/v1/push`; |
| 45 | + |
| 46 | +// Black-hole RPC: accepts the connection, never responds, so getChainId parks. |
| 47 | +const blackholeSockets = new Set(); |
| 48 | +const blackhole = createServer(() => {}); |
| 49 | +blackhole.on("connection", (s) => { |
| 50 | + blackholeSockets.add(s); |
| 51 | + s.on("close", () => blackholeSockets.delete(s)); |
| 52 | +}); |
| 53 | +await new Promise((r) => blackhole.listen(0, "127.0.0.1", r)); |
| 54 | +const blackholeUrl = `http://127.0.0.1:${blackhole.address().port}`; |
| 55 | + |
| 56 | +const baseEnv = { |
| 57 | + ...process.env, |
| 58 | + GRAFANA_LOKI_URL: lokiUrl, |
| 59 | + GRAFANA_LOKI_USER: "u", |
| 60 | + GRAFANA_LOKI_API_KEY: "k", |
| 61 | + GRAFANA_LOKI_ENV: "acctest", |
| 62 | + LOG_LEVEL: "info", |
| 63 | + DATABASE_URL: "postgres://127.0.0.1:1/none", |
| 64 | + STORE_ADDRESS: "0x0000000000000000000000000000000000000001", |
| 65 | + START_BLOCK: "0", |
| 66 | + POLLING_INTERVAL: "600000", |
| 67 | +}; |
| 68 | + |
| 69 | +function shutdownServers() { |
| 70 | + for (const s of blackholeSockets) s.destroy(); |
| 71 | + blackhole.close(); |
| 72 | + loki.close(); |
| 73 | +} |
| 74 | + |
| 75 | +function abort(child, msg) { |
| 76 | + console.error(`FAIL: ${msg}`); |
| 77 | + try { |
| 78 | + child?.kill("SIGKILL"); |
| 79 | + } catch {} |
| 80 | + shutdownServers(); |
| 81 | + process.exit(1); |
| 82 | +} |
| 83 | + |
| 84 | +async function runArm({ label, entry, env, needle, openSse }) { |
| 85 | + const before = received.length; |
| 86 | + const child = spawn("node", ["--import", "tsx", `src/bin/${entry}`], { |
| 87 | + cwd: pkgDir, |
| 88 | + env: { ...baseEnv, ...env }, |
| 89 | + stdio: ["ignore", "pipe", "pipe"], |
| 90 | + }); |
| 91 | + let stdout = ""; |
| 92 | + child.stdout.on("data", (d) => (stdout += d)); |
| 93 | + child.stderr.on("data", () => {}); |
| 94 | + let earlyExit = null; |
| 95 | + child.on("exit", (code) => (earlyExit = code)); |
| 96 | + |
| 97 | + const bootStart = Date.now(); |
| 98 | + while (!stdout.includes(needle) && earlyExit === null && Date.now() - bootStart < 20000) { |
| 99 | + await delay(150); |
| 100 | + } |
| 101 | + if (earlyExit !== null) abort(child, `[${label}] exited early (code ${earlyExit}) before SIGTERM`); |
| 102 | + if (!stdout.includes(needle)) abort(child, `[${label}] did not boot / emit startup log via exec-direct command`); |
| 103 | + console.log(`[${label}] boot OK: exec-direct command started node and emitted the startup log`); |
| 104 | + |
| 105 | + let sseController; |
| 106 | + if (openSse) { |
| 107 | + sseController = new AbortController(); |
| 108 | + fetch(`http://127.0.0.1:${env.PORT}/api/logs-live?input=${encodeURIComponent('{"filters":[]}')}&block_num=0`, { |
| 109 | + signal: sseController.signal, |
| 110 | + }).catch(() => {}); |
| 111 | + await delay(300); |
| 112 | + } else { |
| 113 | + // Give the indexer a moment to reach its parked top-level await. |
| 114 | + await delay(400); |
| 115 | + } |
| 116 | + |
| 117 | + const killAt = Date.now(); |
| 118 | + child.kill("SIGTERM"); |
| 119 | + const exitCode = await new Promise((resolve) => child.on("exit", (code) => resolve(code))); |
| 120 | + const elapsed = Date.now() - killAt; |
| 121 | + sseController?.abort(); |
| 122 | + |
| 123 | + if (exitCode !== 0) abort(child, `[${label}] expected exit 0 on SIGTERM, got ${exitCode}`); |
| 124 | + if (elapsed > 5000) abort(child, `[${label}] shutdown took ${elapsed}ms (> bounded grace)`); |
| 125 | + if (!received.slice(before).join("").includes(needle)) { |
| 126 | + abort(child, `[${label}] fake Loki never received the startup batch — SIGTERM->flush did not run`); |
| 127 | + } |
| 128 | + console.log(`[${label}] PASS: SIGTERM -> exit 0 in ${elapsed}ms; Loki received the final batch`); |
| 129 | +} |
| 130 | + |
| 131 | +await runArm({ |
| 132 | + label: "frontend", |
| 133 | + entry: "postgres-frontend.ts", |
| 134 | + env: { LOG_SERVICE: "indexer-api", HOST: "127.0.0.1", PORT: "37337" }, |
| 135 | + needle: "starting postgres-frontend", |
| 136 | + openSse: true, |
| 137 | +}); |
| 138 | + |
| 139 | +await runArm({ |
| 140 | + label: "indexer", |
| 141 | + entry: "postgres-decoded-indexer.ts", |
| 142 | + env: { LOG_SERVICE: "indexer-store", RPC_HTTP_URL: blackholeUrl }, |
| 143 | + needle: "starting postgres-decoded-indexer", |
| 144 | + openSse: false, |
| 145 | +}); |
| 146 | + |
| 147 | +console.log("ALL ARMS PASS"); |
| 148 | +shutdownServers(); |
| 149 | +process.exit(0); |
0 commit comments