// SSE 多进程测试用子进程:sub 模式订阅并逐行输出收到的事件 JSON,收满 n 条退出;pub 模式发布 n 条后退出。 // 用法:deno run -A test/fixtures/sse-bus-proc.ts import type { DB } from "@umt/db"; import { Kysely, PostgresDialect } from "kysely"; import pg from "pg"; import { PgEventBus } from "../../src/http/sse-pg.ts"; import { createLogger } from "../../src/logger.ts"; const [mode, schema, channel, nRaw] = Deno.args; const n = Number(nRaw); const url = Deno.env.get("DATABASE_URL") ?? "postgres://umt:umt@localhost:55433/umt"; const db = new Kysely({ dialect: new PostgresDialect({ pool: new pg.Pool({ connectionString: url, max: 2, options: `-c search_path=${schema},public` }), }), }); const bus = await PgEventBus.start({ db, connectionString: url, logger: createLogger({ level: "warn" }), channel: channel!, }); if (mode === "sub") { let got = 0; const done = new Promise((resolve) => { bus.subscribe((e) => { console.log(JSON.stringify(e)); if (++got >= n) resolve(); }); }); console.log("READY"); const timeout = setTimeout(() => { console.error("timeout"); Deno.exit(3); }, 20_000); await done; clearTimeout(timeout); } else { for (let i = 0; i < n; i++) await bus.publish("test.ping", { i, pid: Deno.pid }); } await bus.stop(); await db.destroy();