Skip to content

Commit 47e2ab5

Browse files
committed
fix(cluster): subscription regards password setting
Closes #718
1 parent 65b8204 commit 47e2ab5

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

lib/cluster/ClusterSubscriber.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ export default class ClusterSubscriber {
5252
return
5353
}
5454

55-
const {port, host} = sampleNode.options
56-
debug('selected a subscriber %s:%s', host, port)
55+
const {options} = sampleNode
56+
debug('selected a subscriber %s:%s', options.host, options.port)
5757

5858
// Create a specialized Redis connection for the subscription.
5959
// Note that auto reconnection is enabled here.
6060
// `enableReadyCheck` is disabled because subscription is allowed
6161
// when redis is loading data from the disk.
6262
this.subscriber = new Redis({
63-
port,
64-
host,
63+
port: options.port,
64+
host: options.host,
65+
password: options.password,
6566
enableReadyCheck: false,
6667
connectionName: SUBSCRIBER_CONNECTION_NAME,
6768
lazyConnect: true

test/functional/cluster/pub_sub.js

+25
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,31 @@ describe('cluster:pub/sub', function () {
4545
});
4646
});
4747

48+
it('supports password', function (done) {
49+
const handler = function (argv, c) {
50+
if (argv[0] === 'auth') {
51+
c.password = argv[1]
52+
return
53+
}
54+
if (argv[0] === 'subscribe') {
55+
expect(c.password).to.eql('abc')
56+
expect(c.getConnectionName()).to.eql('ioredisClusterSubscriber')
57+
}
58+
if (argv[0] === 'cluster' && argv[1] === 'slots') {
59+
return [
60+
[0, 16383, ['127.0.0.1', 30001]]
61+
];
62+
}
63+
};
64+
new MockServer(30001, handler);
65+
66+
var sub = new Redis.Cluster([{port: '30001', password: 'abc'}]);
67+
68+
sub.subscribe('test cluster', function () {
69+
done();
70+
});
71+
});
72+
4873
it('should re-subscribe after reconnection', function (done) {
4974
new MockServer(30001, function (argv) {
5075
if (argv[0] === 'cluster' && argv[1] === 'slots') {

test/helpers/mock_server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ util.inherits(MockServer, EventEmitter);
4747
MockServer.prototype.connect = function () {
4848
var _this = this;
4949
this.socket = net.createServer(function (c) {
50+
c.getConnectionName = () => (c._connectionName)
5051
var clientIndex = _this.clients.push(c) - 1;
5152
process.nextTick(function () {
5253
_this.emit('connect', c);
@@ -59,7 +60,7 @@ MockServer.prototype.connect = function () {
5960
if (reply.length === 3 && reply[0].toLowerCase() === 'client' && reply[1].toLowerCase() === 'setname') {
6061
c._connectionName = reply[2]
6162
}
62-
_this.write(c, _this.handler && _this.handler(reply));
63+
_this.write(c, _this.handler && _this.handler(reply, c));
6364
},
6465
returnError: function () { }
6566
});

0 commit comments

Comments
 (0)