Skip to content

Commit

Permalink
review changes as requested by ivan.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Ricart committed May 5, 2017
1 parent 175d828 commit de189da
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/nats.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,9 @@ Client.prototype.processInbound = function() {
});

if(toAdd.length > 0) {
shuffle(toAdd);
if(client.options.noRandomize !== true) {
shuffle(toAdd);
}
toAdd.forEach(function(s) {
client.servers.push(s);
});
Expand Down
50 changes: 48 additions & 2 deletions test/dyncluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Dynamic Cluster - Connect URLs', function() {

var connectCount = 0;
function connectAndRecordPorts(check) {
var nc = NATS.connect({'port': port, 'reconnectTimeWait': 100, 'protocol': 0});
var nc = NATS.connect({'port': port, 'reconnectTimeWait': 100});
nc.on('connect', function () {
var have = [];
nc.servers.forEach(function (s) {
Expand All @@ -74,7 +74,7 @@ describe('Dynamic Cluster - Connect URLs', function() {
connectCount++;
should.ok(have[0] == port);
var key = have.join("_");
map[key]++;
map[key] = map[key] ? map[key] + 1 : 1;
nc.close();
if (connectCount === 10) {
check();
Expand All @@ -94,4 +94,50 @@ describe('Dynamic Cluster - Connect URLs', function() {
}
});
});

it('added servers not shuffled when noRandomize is set', function(done) {
var route_port = 54320;
var port = 54321;
// start a cluster of one server
var ports = [];
for (var i = 0; i < 10; i++) {
ports.push(port + i);
}
var map = {};
servers = nsc.start_cluster(ports, route_port, function () {
should(servers.length).be.equal(10);

var connectCount = 0;
function connectAndRecordPorts(check) {
var nc = NATS.connect({'port': port, 'reconnectTimeWait': 100, 'noRandomize': true});
nc.on('connect', function () {
var have = [];
nc.servers.forEach(function (s) {
have.push(s.url.port);
});

connectCount++;
should.ok(have[0] == port);
var key = have.join("_");
map[key] = map[key] ? map[key] + 1 : 1;
nc.close();
if (connectCount === 10) {
check();
}
});
}

// we should have more than one property if there was randomization
function check() {
var keys = Object.getOwnPropertyNames(map);
should.ok(keys.length === 1);
should.ok(map[keys[0]] === 10);
done();
}
// connect several times...
for (var i = 0; i < 10; i++) {
connectAndRecordPorts(check);
}
});
});
});

0 comments on commit de189da

Please sign in to comment.