|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const cds = require("@sap/cds"); |
| 4 | + |
| 5 | +const redis = require("redis"); |
| 6 | +require("../mocks/redis"); |
| 7 | +jest.mock("redis", () => require("../mocks/redis")); |
| 8 | + |
| 9 | +const { test } = cds.test(__dirname + "/../.."); |
| 10 | + |
| 11 | +const { RedisClient } = require("../../src/redis-client"); |
| 12 | + |
| 13 | +process.env.PORT = 0; // Random |
| 14 | + |
| 15 | +describe("Redis Client (Sentinel)", () => { |
| 16 | + beforeEach(async () => { |
| 17 | + await test.data.reset(); |
| 18 | + await RedisClient.closeAllClients(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe("Sentinel Mode", () => { |
| 22 | + |
| 23 | + it("creates Sentinel client when sentinel_nodes configured", async () => { |
| 24 | + cds.env.requires.redis = { |
| 25 | + credentials: { |
| 26 | + sentinel_nodes: [ |
| 27 | + { hostname: "sentinel1.example.com", port: 26379 }, |
| 28 | + { hostname: "sentinel2.example.com", port: 26379 }, |
| 29 | + ], |
| 30 | + uri: "redis://:secret@sentinel1.example.com:26379#mymaster", |
| 31 | + password: "secret", |
| 32 | + tls: true, |
| 33 | + }, |
| 34 | + }; |
| 35 | + |
| 36 | + const redisClient = RedisClient.create("sentinel-test"); |
| 37 | + const client = await redisClient.createMainClientAndConnect(); |
| 38 | + |
| 39 | + expect(client).toBeDefined(); |
| 40 | + expect(redis.createSentinel).toHaveBeenCalledWith({ |
| 41 | + name: "mymaster", |
| 42 | + sentinelRootNodes: [ |
| 43 | + { host: "sentinel1.example.com", port: 26379 }, |
| 44 | + { host: "sentinel2.example.com", port: 26379 }, |
| 45 | + ], |
| 46 | + nodeClientOptions: expect.objectContaining({ |
| 47 | + password: "secret", |
| 48 | + socket: expect.objectContaining({ tls: true }), |
| 49 | + }), |
| 50 | + sentinelClientOptions: expect.objectContaining({ |
| 51 | + password: "secret", |
| 52 | + }), |
| 53 | + passthroughClientErrorEvents: true, |
| 54 | + }); |
| 55 | + expect(redisClient.isSentinel).toBe(true); |
| 56 | + expect(redisClient.isCluster).toBe(false); |
| 57 | + await redisClient.closeMainClient(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("prefers master_name field over URI fragment", async () => { |
| 61 | + cds.env.requires.redis = { |
| 62 | + credentials: { |
| 63 | + sentinel_nodes: [{ host: "sentinel.local", port: 26379 }], |
| 64 | + master_name: "explicit-master", |
| 65 | + uri: "redis://sentinel.local#uri-master", |
| 66 | + }, |
| 67 | + }; |
| 68 | + |
| 69 | + const redisClient = RedisClient.create("master-name-test"); |
| 70 | + await redisClient.createMainClientAndConnect(); |
| 71 | + |
| 72 | + expect(redis.createSentinel).toHaveBeenCalledWith(expect.objectContaining({ name: "explicit-master" })); |
| 73 | + }); |
| 74 | + |
| 75 | + it("uses default port 26379 when not specified", async () => { |
| 76 | + cds.env.requires.redis = { |
| 77 | + credentials: { |
| 78 | + sentinel_nodes: [{ hostname: "sentinel.local" }], |
| 79 | + master_name: "mymaster", |
| 80 | + }, |
| 81 | + }; |
| 82 | + |
| 83 | + const redisClient = RedisClient.create("default-port-test"); |
| 84 | + await redisClient.createMainClientAndConnect(); |
| 85 | + |
| 86 | + expect(redis.createSentinel).toHaveBeenCalledWith( |
| 87 | + expect.objectContaining({ |
| 88 | + sentinelRootNodes: [{ host: "sentinel.local", port: 26379 }], |
| 89 | + }), |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it("returns undefined if master name not found", async () => { |
| 94 | + cds.env.requires.redis = { |
| 95 | + credentials: { |
| 96 | + sentinel_nodes: [{ hostname: "sentinel.local" }], |
| 97 | + }, |
| 98 | + }; |
| 99 | + |
| 100 | + const redisClient = RedisClient.create("no-master-test"); |
| 101 | + const client = await redisClient.createMainClientAndConnect(); |
| 102 | + expect(client).toBeUndefined(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("prioritizes sentinel over cluster mode", async () => { |
| 106 | + cds.env.requires.redis = { |
| 107 | + credentials: { |
| 108 | + sentinel_nodes: [{ hostname: "sentinel.local" }], |
| 109 | + master_name: "mymaster", |
| 110 | + cluster_mode: true, |
| 111 | + }, |
| 112 | + }; |
| 113 | + |
| 114 | + const redisClient = RedisClient.create("priority-test"); |
| 115 | + await redisClient.createMainClientAndConnect(); |
| 116 | + |
| 117 | + expect(redis.createSentinel).toHaveBeenCalled(); |
| 118 | + expect(redis.createCluster).not.toHaveBeenCalled(); |
| 119 | + expect(redisClient.isSentinel).toBe(true); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments