Skip to content

Commit

Permalink
Add tests for re-subscribing
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jul 26, 2015
1 parent 15f50e4 commit 687d3f3
Showing 1 changed file with 64 additions and 14 deletions.
78 changes: 64 additions & 14 deletions test/functional/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,24 +753,19 @@ describe('cluster', function () {

describe('pub/sub', function () {
it('should receive messages', function (done) {
var slotTable = [
[0, 1, ['127.0.0.1', 30001]],
[2, 16383, ['127.0.0.1', 30002]]
];
var node1 = new MockServer(30001, function (argv) {
if (argv[0] === 'cluster' && argv[1] === 'slots') {
return slotTable;
}
});
var node2 = new MockServer(30002, function (argv) {
var handler = function (argv) {
if (argv[0] === 'cluster' && argv[1] === 'slots') {
return slotTable;
return [
[0, 1, ['127.0.0.1', 30001]],
[2, 16383, ['127.0.0.1', 30002]]
];
}
});
};
var node1 = new MockServer(30001, handler);
var node2 = new MockServer(30002, handler);

var options = [ { host: '127.0.0.1', port: '30001' } ];
var sub = new Redis.Cluster(options);
var pub = new Redis.Cluster(options);

sub.subscribe('test cluster', function () {
node1.write(node1.clients[0], ['message', 'test channel', 'hi']);
Expand All @@ -779,10 +774,65 @@ describe('cluster', function () {
expect(channel).to.eql('test channel');
expect(message).to.eql('hi');
sub.disconnect();
pub.disconnect();
disconnect([node1, node2], done);
});
});

it('should re-subscribe after reconnection', function (done) {
var server = new MockServer(30001, function (argv) {
if (argv[0] === 'cluster' && argv[1] === 'slots') {
return [
[0, 16383, ['127.0.0.1', 30001]]
];
} else if (argv[0] === 'subscribe' || argv[0] === 'psubscribe') {
return [argv[0], argv[1]];
}
});
var client = new Redis.Cluster([{ host: '127.0.0.1', port: '30001' }]);

client.subscribe('test cluster', function () {
var subscribe = Redis.prototype.subscribe;
stub(Redis.prototype, 'subscribe', function (channels) {
expect(channels).to.eql(['test cluster']);
Redis.prototype.subscribe.restore();
client.disconnect();
disconnect([server], done);
return Redis.prototype.subscribe.apply(this, arguments);
});
client.on('end', function () {
client.connect();
});
client.disconnect();
});
});

it('should re-psubscribe after reconnection', function (done) {
var server = new MockServer(30001, function (argv) {
if (argv[0] === 'cluster' && argv[1] === 'slots') {
return [
[0, 16383, ['127.0.0.1', 30001]]
];
} else if (argv[0] === 'subscribe' || argv[0] === 'psubscribe') {
return [argv[0], argv[1]];
}
});
var client = new Redis.Cluster([{ host: '127.0.0.1', port: '30001' }]);

client.psubscribe('test?', function () {
var psubscribe = Redis.prototype.psubscribe;
stub(Redis.prototype, 'psubscribe', function (channels) {
expect(channels).to.eql(['test?']);
Redis.prototype.psubscribe.restore();
client.disconnect();
disconnect([server], done);
return Redis.prototype.psubscribe.apply(this, arguments);
});
client.on('end', function () {
client.connect();
});
client.disconnect();
});
});
});

describe('readonly', function() {
Expand Down

0 comments on commit 687d3f3

Please sign in to comment.