release: WRNexusJS 0.8.0
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { createPubSub, kafkaDriver, natsDriver } from "../src/index.ts";
|
||||
|
||||
test("NATS adapter serializes messages and maps namespace wildcards", async () => {
|
||||
let subscription = "";
|
||||
let receive: ((data: Uint8Array, subject: string) => void) | undefined;
|
||||
const bus = createPubSub(
|
||||
natsDriver({
|
||||
async publish(subject, data) {
|
||||
receive?.(data, subject);
|
||||
},
|
||||
subscribe(subject, handler) {
|
||||
subscription = subject;
|
||||
receive = handler;
|
||||
return () => {
|
||||
receive = undefined;
|
||||
};
|
||||
},
|
||||
}),
|
||||
);
|
||||
const values: unknown[] = [];
|
||||
bus.subscribe("room:*", (value) => {
|
||||
values.push(value);
|
||||
});
|
||||
await bus.publish("room:one", { online: 2 });
|
||||
expect(subscription).toBe("room:>");
|
||||
expect(values).toEqual([{ online: 2 }]);
|
||||
});
|
||||
|
||||
test("Kafka adapter preserves topic and JSON payload contracts", async () => {
|
||||
let receive: ((value: string, topic: string) => void) | undefined;
|
||||
const bus = createPubSub(
|
||||
kafkaDriver({
|
||||
async publish(topic, value) {
|
||||
receive?.(value, topic);
|
||||
},
|
||||
subscribe(_pattern, handler) {
|
||||
receive = handler;
|
||||
return () => {
|
||||
receive = undefined;
|
||||
};
|
||||
},
|
||||
}),
|
||||
);
|
||||
let received = "";
|
||||
bus.subscribe("events", (value, topic) => {
|
||||
received = `${topic}:${(value as { id: number }).id}`;
|
||||
});
|
||||
await bus.publish("events", { id: 7 });
|
||||
expect(received).toBe("events:7");
|
||||
});
|
||||
@@ -36,6 +36,21 @@ test("unsubscribe stops delivery", async () => {
|
||||
expect(n).toBe(1);
|
||||
});
|
||||
|
||||
test("publish awaits async handlers and close rejects new work", async () => {
|
||||
const bus = createPubSub();
|
||||
let completed = false;
|
||||
bus.subscribe("task", async () => {
|
||||
await Promise.resolve();
|
||||
completed = true;
|
||||
});
|
||||
await bus.publish("task", {});
|
||||
expect(completed).toBe(true);
|
||||
await bus.close();
|
||||
await bus.close();
|
||||
await expect(bus.publish("task", {})).rejects.toThrow("WRN-PUBSUB-CLOSED");
|
||||
expect(() => bus.subscribe("task", () => {})).toThrow("WRN-PUBSUB-CLOSED");
|
||||
});
|
||||
|
||||
test("resilient pubsub validates retry and presence settings", () => {
|
||||
const driver = {
|
||||
publish: async () => {},
|
||||
|
||||
@@ -31,3 +31,43 @@ test("bounds writes queued while Redis is unavailable", () => {
|
||||
expect(() => driver.publish("topic", "overflow")).toThrow("queue is full");
|
||||
driver.close();
|
||||
});
|
||||
|
||||
test("validates reconnect and backpressure options", () => {
|
||||
bun.connect = (() => new Promise(() => {})) as typeof bun.connect;
|
||||
expect(() => redisDriver(undefined, { maxPending: 0 })).toThrow("maxPending");
|
||||
expect(() => redisDriver(undefined, { reconnectDelayMs: 20, reconnectMaxDelayMs: 10 })).toThrow(
|
||||
"reconnectMaxDelayMs",
|
||||
);
|
||||
});
|
||||
|
||||
test("reconnects and replays subscriptions after a socket closes", async () => {
|
||||
const connections: Array<Record<string, any>> = [];
|
||||
bun.connect = ((options: Record<string, any>) => {
|
||||
connections.push(options);
|
||||
return Promise.resolve({});
|
||||
}) as typeof bun.connect;
|
||||
const driver = redisDriver("redis://localhost:6379", {
|
||||
reconnectDelayMs: 1,
|
||||
reconnectMaxDelayMs: 1,
|
||||
});
|
||||
expect(connections).toHaveLength(2);
|
||||
|
||||
const firstWrites: string[] = [];
|
||||
connections[0].socket.open({
|
||||
write: (bytes: Uint8Array) => firstWrites.push(new TextDecoder().decode(bytes)),
|
||||
end() {},
|
||||
});
|
||||
driver.subscribe("order:*", () => {});
|
||||
expect(firstWrites.some((write) => write.includes("PSUBSCRIBE"))).toBe(true);
|
||||
|
||||
connections[0].socket.close();
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
expect(connections.length).toBeGreaterThanOrEqual(3);
|
||||
const replayed: string[] = [];
|
||||
connections[2].socket.open({
|
||||
write: (bytes: Uint8Array) => replayed.push(new TextDecoder().decode(bytes)),
|
||||
end() {},
|
||||
});
|
||||
expect(replayed.some((write) => write.includes("PSUBSCRIBE"))).toBe(true);
|
||||
driver.close();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user