From 47e2ab541f46940977b22412d655c4b196eddbff Mon Sep 17 00:00:00 2001 From: luin Date: Tue, 9 Oct 2018 15:33:38 +0800 Subject: [PATCH] fix(cluster): subscription regards password setting Closes #718 --- lib/cluster/ClusterSubscriber.ts | 9 +++++---- test/functional/cluster/pub_sub.js | 25 +++++++++++++++++++++++++ test/helpers/mock_server.js | 3 ++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/cluster/ClusterSubscriber.ts b/lib/cluster/ClusterSubscriber.ts index eadb4d1e..93bce136 100644 --- a/lib/cluster/ClusterSubscriber.ts +++ b/lib/cluster/ClusterSubscriber.ts @@ -52,16 +52,17 @@ export default class ClusterSubscriber { return } - const {port, host} = sampleNode.options - debug('selected a subscriber %s:%s', host, port) + const {options} = sampleNode + debug('selected a subscriber %s:%s', options.host, options.port) // Create a specialized Redis connection for the subscription. // Note that auto reconnection is enabled here. // `enableReadyCheck` is disabled because subscription is allowed // when redis is loading data from the disk. this.subscriber = new Redis({ - port, - host, + port: options.port, + host: options.host, + password: options.password, enableReadyCheck: false, connectionName: SUBSCRIBER_CONNECTION_NAME, lazyConnect: true diff --git a/test/functional/cluster/pub_sub.js b/test/functional/cluster/pub_sub.js index 86535d56..9ef5596d 100644 --- a/test/functional/cluster/pub_sub.js +++ b/test/functional/cluster/pub_sub.js @@ -45,6 +45,31 @@ describe('cluster:pub/sub', function () { }); }); + it('supports password', function (done) { + const handler = function (argv, c) { + if (argv[0] === 'auth') { + c.password = argv[1] + return + } + if (argv[0] === 'subscribe') { + expect(c.password).to.eql('abc') + expect(c.getConnectionName()).to.eql('ioredisClusterSubscriber') + } + if (argv[0] === 'cluster' && argv[1] === 'slots') { + return [ + [0, 16383, ['127.0.0.1', 30001]] + ]; + } + }; + new MockServer(30001, handler); + + var sub = new Redis.Cluster([{port: '30001', password: 'abc'}]); + + sub.subscribe('test cluster', function () { + done(); + }); + }); + it('should re-subscribe after reconnection', function (done) { new MockServer(30001, function (argv) { if (argv[0] === 'cluster' && argv[1] === 'slots') { diff --git a/test/helpers/mock_server.js b/test/helpers/mock_server.js index 08f7032b..fcca9a6f 100644 --- a/test/helpers/mock_server.js +++ b/test/helpers/mock_server.js @@ -47,6 +47,7 @@ util.inherits(MockServer, EventEmitter); MockServer.prototype.connect = function () { var _this = this; this.socket = net.createServer(function (c) { + c.getConnectionName = () => (c._connectionName) var clientIndex = _this.clients.push(c) - 1; process.nextTick(function () { _this.emit('connect', c); @@ -59,7 +60,7 @@ MockServer.prototype.connect = function () { if (reply.length === 3 && reply[0].toLowerCase() === 'client' && reply[1].toLowerCase() === 'setname') { c._connectionName = reply[2] } - _this.write(c, _this.handler && _this.handler(reply)); + _this.write(c, _this.handler && _this.handler(reply, c)); }, returnError: function () { } });