Skip to content

Commit

Permalink
fix(cluster): subscription regards password setting
Browse files Browse the repository at this point in the history
Closes #718
  • Loading branch information
luin committed Oct 9, 2018
1 parent 65b8204 commit 47e2ab5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/cluster/ClusterSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/functional/cluster/pub_sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/mock_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 () { }
});
Expand Down

0 comments on commit 47e2ab5

Please sign in to comment.