-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FIX #103 - Implementing discovered servers API #143
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* jslint node: true */ | ||
/* global describe: false, before: false, after: false, it: false, afterEach: false, beforeEach: false */ | ||
/* jshint -W030 */ | ||
'use strict'; | ||
|
||
var NATS = require ('../'), | ||
nsc = require('./support/nats_server_control'), | ||
should = require('should'); | ||
|
||
describe('Dynamic Cluster - Connect URLs', function() { | ||
this.timeout(10000); | ||
|
||
// this to enable per test cleanup | ||
var servers; | ||
// Shutdown our servers | ||
afterEach(function() { | ||
nsc.stop_cluster(servers); | ||
servers = []; | ||
}); | ||
|
||
it('adding cluster performs update', function(done) { | ||
var route_port = 54220; | ||
var port = 54221; | ||
|
||
// start a new cluster with single server | ||
servers = nsc.start_cluster([port], route_port, function() { | ||
should(servers.length).be.equal(1); | ||
|
||
// connect the client | ||
var nc = NATS.connect({'port': port, 'reconnectTimeWait': 100}); | ||
nc.on('connect', function () { | ||
// start adding servers | ||
process.nextTick(function () { | ||
var others = nsc.add_member_with_delay([port + 1, port + 2], route_port, 250, function () { | ||
// verify that 2 servers were added | ||
should(others.length).be.equal(2); | ||
others.forEach(function (o) { | ||
// add them so they can be reaped | ||
servers.push(o); | ||
}); | ||
// give some time for the server to send infos | ||
setTimeout(function () { | ||
// we should know of 3 servers - the one we connected and the 2 we added | ||
should(nc.servers.length).be.equal(3); | ||
done(); | ||
}, 1000); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('added servers are shuffled at the end of the list', 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}); | ||
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); | ||
done(); | ||
} | ||
// connect several times... | ||
for (var i = 0; i < 10; i++) { | ||
connectAndRecordPorts(check); | ||
} | ||
}); | ||
}); | ||
|
||
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); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First, indentation looks strange.
Second, are you not going to do that for any protocol message received by the client? Looks to me that you should ensure that protocol being processed is an INFO here.
Also, not sure if you support randomization, but what the other clients do is randomize (if allowed) the array or connect_urls prior to add the URLs to the server pool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I preserved the indentation that was in the file :)
The block we are in is only handling INFO messages. See https://sourcegraph.com/github.com/nats-io/node-nats@b5db73ee06428471568a730c3b0ff7f907fa6943/-/blob/lib/nats.js#L798:1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even with this link, I am sorry but it looks horrible. It is very difficult to know what is in which block.