44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
// SSE 多进程测试用子进程:sub 模式订阅并逐行输出收到的事件 JSON,收满 n 条退出;pub 模式发布 n 条后退出。
|
|
// 用法:bun test/fixtures/sse-bus-proc.ts <sub|pub> <schema> <channel> <n>
|
|
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] = process.argv.slice(2);
|
|
const n = Number(nRaw);
|
|
const url = process.env.DATABASE_URL ?? "postgres://umt:umt@localhost:55433/umt";
|
|
const db = new Kysely<DB>({
|
|
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<void>((resolve) => {
|
|
bus.subscribe((e) => {
|
|
console.log(JSON.stringify(e));
|
|
if (++got >= n) resolve();
|
|
});
|
|
});
|
|
console.log("READY");
|
|
const timeout = setTimeout(() => {
|
|
console.error("timeout");
|
|
process.exit(3);
|
|
}, 20_000);
|
|
await done;
|
|
clearTimeout(timeout);
|
|
} else {
|
|
for (let i = 0; i < n; i++) await bus.publish("test.ping", { i, pid: process.pid });
|
|
}
|
|
await bus.stop();
|
|
await db.destroy();
|