|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { Cluster, createRedisClusterClient, defaultReconnectOnError } from "./index.js"; |
| 3 | + |
| 4 | +// Port 6399 is deliberately closed: these assertions are about the client the factory builds, not |
| 5 | +// about a connection. |
| 6 | +const NODES = [{ host: "127.0.0.1", port: 6399 }]; |
| 7 | + |
| 8 | +type InnerOptions = { |
| 9 | + options: { |
| 10 | + redisOptions?: { |
| 11 | + reconnectOnError?: unknown; |
| 12 | + maxRetriesPerRequest?: number; |
| 13 | + retryStrategy?: unknown; |
| 14 | + keyPrefix?: string; |
| 15 | + }; |
| 16 | + }; |
| 17 | +}; |
| 18 | + |
| 19 | +function innerOptionsOf(client: Cluster) { |
| 20 | + return (client as unknown as InnerOptions).options.redisOptions; |
| 21 | +} |
| 22 | + |
| 23 | +describe("createRedisClusterClient", () => { |
| 24 | + it("returns a Cluster instance", async () => { |
| 25 | + const client = createRedisClusterClient({ nodes: NODES }); |
| 26 | + try { |
| 27 | + expect(client).toBeInstanceOf(Cluster); |
| 28 | + } finally { |
| 29 | + await client.quit().catch(() => undefined); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + it("installs defaultReconnectOnError on the inner per-node options", async () => { |
| 34 | + const client = createRedisClusterClient({ nodes: NODES }); |
| 35 | + try { |
| 36 | + expect(innerOptionsOf(client)?.reconnectOnError).toBe(defaultReconnectOnError); |
| 37 | + } finally { |
| 38 | + await client.quit().catch(() => undefined); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + it("carries the retry defaults onto the inner options", async () => { |
| 43 | + const client = createRedisClusterClient({ nodes: NODES }); |
| 44 | + try { |
| 45 | + const inner = innerOptionsOf(client); |
| 46 | + expect(inner?.maxRetriesPerRequest).toBeTypeOf("number"); |
| 47 | + expect(inner?.retryStrategy).toBeTypeOf("function"); |
| 48 | + } finally { |
| 49 | + await client.quit().catch(() => undefined); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it("lets caller redisOptions override the defaults", async () => { |
| 54 | + const client = createRedisClusterClient({ |
| 55 | + nodes: NODES, |
| 56 | + redisOptions: { keyPrefix: "engine:", maxRetriesPerRequest: 3 }, |
| 57 | + }); |
| 58 | + try { |
| 59 | + const inner = innerOptionsOf(client); |
| 60 | + expect(inner?.keyPrefix).toBe("engine:"); |
| 61 | + expect(inner?.maxRetriesPerRequest).toBe(3); |
| 62 | + } finally { |
| 63 | + await client.quit().catch(() => undefined); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + it("keeps mapping READONLY, LOADING and UNBLOCKED to a reconnect-and-retry", () => { |
| 68 | + expect(defaultReconnectOnError(new Error("READONLY against a read only replica"))).toBe(2); |
| 69 | + expect(defaultReconnectOnError(new Error("LOADING Redis is loading the dataset"))).toBe(2); |
| 70 | + expect(defaultReconnectOnError(new Error("UNBLOCKED force unblock"))).toBe(2); |
| 71 | + expect(defaultReconnectOnError(new Error("ERR unknown command"))).toBe(false); |
| 72 | + }); |
| 73 | +}); |
0 commit comments