|
| 1 | +import MockServer, { getConnectionName } from "../../helpers/mock_server"; |
| 2 | +import { expect } from "chai"; |
| 3 | +import { Cluster } from "../../../lib"; |
| 4 | +import * as sinon from "sinon"; |
| 5 | +import Redis from "../../../lib/Redis"; |
| 6 | +import { noop } from "../../../lib/utils"; |
| 7 | + |
| 8 | +describe("cluster:spub/ssub", function () { |
| 9 | + it("should receive messages", (done) => { |
| 10 | + const handler = function (argv) { |
| 11 | + if (argv[0] === "cluster" && argv[1] === "SLOTS") { |
| 12 | + return [ |
| 13 | + [0, 1, ["127.0.0.1", 30001]], |
| 14 | + [2, 16383, ["127.0.0.1", 30002]], |
| 15 | + ]; |
| 16 | + } |
| 17 | + }; |
| 18 | + const node1 = new MockServer(30001, handler); |
| 19 | + new MockServer(30002, handler); |
| 20 | + |
| 21 | + const options = [{ host: "127.0.0.1", port: "30001" }]; |
| 22 | + const ssub = new Cluster(options); |
| 23 | + |
| 24 | + ssub.ssubscribe("test cluster", function () { |
| 25 | + node1.write(node1.findClientByName("ioredis-cluster(subscriber)"), [ |
| 26 | + "smessage", |
| 27 | + "test shard channel", |
| 28 | + "hi", |
| 29 | + ]); |
| 30 | + }); |
| 31 | + ssub.on("smessage", function (channel, message) { |
| 32 | + expect(channel).to.eql("test shard channel"); |
| 33 | + expect(message).to.eql("hi"); |
| 34 | + ssub.disconnect(); |
| 35 | + done(); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should works when sending regular commands", (done) => { |
| 40 | + const handler = function (argv) { |
| 41 | + if (argv[0] === "cluster" && argv[1] === "SLOTS") { |
| 42 | + return [[0, 16383, ["127.0.0.1", 30001]]]; |
| 43 | + } |
| 44 | + }; |
| 45 | + new MockServer(30001, handler); |
| 46 | + |
| 47 | + const ssub = new Cluster([{ port: "30001" }]); |
| 48 | + |
| 49 | + ssub.ssubscribe("test cluster", function () { |
| 50 | + ssub.set("foo", "bar").then((res) => { |
| 51 | + expect(res).to.eql("OK"); |
| 52 | + ssub.disconnect(); |
| 53 | + done(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("supports password", (done) => { |
| 59 | + const handler = function (argv, c) { |
| 60 | + if (argv[0] === "auth") { |
| 61 | + c.password = argv[1]; |
| 62 | + return; |
| 63 | + } |
| 64 | + if (argv[0] === "ssubscribe") { |
| 65 | + expect(c.password).to.eql("abc"); |
| 66 | + expect(getConnectionName(c)).to.eql("ioredis-cluster(subscriber)"); |
| 67 | + } |
| 68 | + if (argv[0] === "cluster" && argv[1] === "SLOTS") { |
| 69 | + return [[0, 16383, ["127.0.0.1", 30001]]]; |
| 70 | + } |
| 71 | + }; |
| 72 | + new MockServer(30001, handler); |
| 73 | + |
| 74 | + const ssub = new Redis.Cluster([{ port: "30001", password: "abc" }]); |
| 75 | + |
| 76 | + ssub.ssubscribe("test cluster", function () { |
| 77 | + ssub.disconnect(); |
| 78 | + done(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should re-ssubscribe after reconnection", (done) => { |
| 83 | + new MockServer(30001, function (argv) { |
| 84 | + if (argv[0] === "cluster" && argv[1] === "SLOTS") { |
| 85 | + return [[0, 16383, ["127.0.0.1", 30001]]]; |
| 86 | + } else if (argv[0] === "ssubscribe" || argv[0] === "psubscribe") { |
| 87 | + return [argv[0], argv[1]]; |
| 88 | + } |
| 89 | + }); |
| 90 | + const client = new Cluster([{ host: "127.0.0.1", port: "30001" }]); |
| 91 | + |
| 92 | + client.ssubscribe("test cluster", function () { |
| 93 | + const stub = sinon |
| 94 | + .stub(Redis.prototype, "ssubscribe") |
| 95 | + .callsFake((channels) => { |
| 96 | + expect(channels).to.eql(["test cluster"]); |
| 97 | + stub.restore(); |
| 98 | + client.disconnect(); |
| 99 | + done(); |
| 100 | + return Redis.prototype.ssubscribe.apply(this, arguments); |
| 101 | + }); |
| 102 | + client.once("end", function () { |
| 103 | + client.connect().catch(noop); |
| 104 | + }); |
| 105 | + client.disconnect(); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments