Skip to content

Commit

Permalink
Testing if container is reused
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Ricart committed Oct 18, 2017
1 parent f97b8b0 commit 04a789a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -15,3 +15,6 @@ script:
- if [[ "$TRAVIS_NODE_VERSION" == 4 ]]; then npm test; fi
- if [[ "$TRAVIS_NODE_VERSION" == 6 ]]; then npm test; fi
- if [[ "$TRAVIS_NODE_VERSION" == 8 ]]; then npm run coveralls; fi

after_failure:
- cat "/tmp/test_ports.txt"
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -291,3 +291,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

9 changes: 9 additions & 0 deletions lib/nats.js
Expand Up @@ -404,8 +404,10 @@ Client.prototype.setupHandlers = function() {
});

stream.on('close', function(hadError) {
console.log(Date.now() + " stream close");
client.closeStream();
client.emit('disconnect');
console.log(Date.now() + " notified disconnected close", client.listeners('disconnect').length);
if (client.closed === true ||
client.options.reconnect === false ||
((client.reconnects >= client.options.maxReconnectAttempts) && client.options.maxReconnectAttempts !== -1)) {
Expand Down Expand Up @@ -995,14 +997,18 @@ Client.prototype.processMsg = function() {
* @api private
*/
Client.prototype.processErr = function(s) {
console.log(Date.now() + " PROCESS ERR");

// current NATS clients, will raise an error and close on any errors
// except stale connection and permission errors
var m = s ? s.toLowerCase() : '';
if (m.indexOf(STALE_CONNECTION_ERR) !== -1) {
this.scheduleReconnect();
} else if (m.indexOf(PERMISSIONS_ERR) !== -1) {
console.log(Date.now() + " PERMISSION ERROR FROM SOCKET");
this.emit('permission_error', new NatsError(s, NATS_PROTOCOL_ERR));
} else {
console.log(Date.now() + " Error");
this.emit('error', new NatsError(s, NATS_PROTOCOL_ERR));
this.closeStream();
}
Expand Down Expand Up @@ -1316,6 +1322,9 @@ Client.prototype.reconnect = function() {
return;
}
this.reconnects += 1;

console.log(Date.now(), "reconnect", this.reconnects);

this.createConnection();
if (this.currentServer.didConnect === true) {
this.emit('reconnecting');
Expand Down
30 changes: 22 additions & 8 deletions test/dyncluster.js
Expand Up @@ -175,6 +175,8 @@ describe('Dynamic Cluster - Connect URLs', function() {
});

function reconnectTest(port, route_port, use_certs, done) {
var start = Date.now();
var m = use_certs ? "[tls]" : "[non tls]";
var config = {
tls: {
cert_file: path.resolve(process.cwd(), "./test/certs/server-cert.pem"),
Expand Down Expand Up @@ -207,23 +209,28 @@ describe('Dynamic Cluster - Connect URLs', function() {
short.tls.timeout = 0.0001;
}
short.authorization.timeout = 0.0001;

var short_conf = path.resolve(os.tmpdir(), 'shortconf_' + nuid.next() + '.conf');
ncu.writeFile(short_conf, ncu.j(short));

// start a new cluster with single server
var memberPort = nsc.alloc_next_port();
servers = nsc.start_cluster([port], route_port, ['-c', normal_conf], function(err) {
if(err) {
console.log(m, 'cluster startup failed: ', err.stack);
done(err);
}
console.log('server started at port', port);
process.nextTick(function() {
var others = nsc.add_member_with_delay([memberPort], route_port, 250, ['-c', short_conf], function(err) {
if(err) {
console.log(m, 'cluster member startup failed: ', err.stack);
done(err);
}
console.log(m, 'cluster member added at port', memberPort);
// add the second server
servers.push(others[0]);
setTimeout(startClient, 1000);
process.nextTick(startClient);
});
});
});
Expand All @@ -247,10 +254,11 @@ describe('Dynamic Cluster - Connect URLs', function() {

function testClusterOK(nc) {
if(nc.servers.length === 2) {
console.log(m, 'cluster has two members');
killServer();
} else {
setTimeout(function(){
console.log('cluster not formed yet', nc.servers);
console.log(m, 'cluster not formed yet');
testClusterOK(nc);
}, 250);
}
Expand All @@ -260,6 +268,7 @@ describe('Dynamic Cluster - Connect URLs', function() {
connected = true;
// now we disconnect first server
setTimeout(function() {
console.log(m, 'killing server 0');
servers[0].kill();
}, 100);
}
Expand All @@ -268,28 +277,33 @@ describe('Dynamic Cluster - Connect URLs', function() {
var connected = false;
nc.on('connect', function(nc) {
if (!connected) {
console.log(m, 'first connect');
testClusterOK(nc);
}
});

var errors = [];
nc.on('error', function(e) {
console.log(m, 'connection got error', e);
// save the error
errors.push(e);
});
nc.on('close', function() {
should.ok(connected);
console.log(m, 'got close');
should.ok(connected, 'connected');
// for tls the error isn't always raised so we'll just trust
// that we we tried connecting to the bad server
should.ok(errors.length === 1 || disconnects.indexOf((memberPort) + '') !== -1);
should.ok(errors.length === 1 || disconnects.indexOf(memberPort) > -1, 'got error or disconnect');

console.log(m, '>>> took', Date.now() - start);

done();
});
var disconnects = [];
nc.on('disconnect', function() {
var p = nc.currentServer.url.port;
if (disconnects.indexOf(p) === -1) {
disconnects.push(p);
}
var p = parseInt(nc.currentServer.url.port);
console.log(m, 'got disconnect from', p);
disconnects.push(p);
});
}
}
Expand Down
35 changes: 35 additions & 0 deletions test/support/nats_server_control.js
Expand Up @@ -3,10 +3,15 @@

var spawn = require('child_process').spawn;
var net = require('net');
var fs = require('fs');
var path = require('path');
var os = require('os');

var SERVER = (process.env.TRAVIS) ? 'gnatsd/gnatsd' : 'gnatsd';
var DEFAULT_PORT = 4222;

console.log(path.resolve(os.tmpdir(), 'test_ports.txt'));

// select some random start port between 40000 and 50000
var next = 40000;
//Math.floor(Math.random()*(50000-40000+1)+40000);
Expand All @@ -24,6 +29,34 @@ function alloc_next_port(n) {
return a;
}

function log(port, extra) {
var lines = new Error('debug').stack.split('\n');
lines = lines.filter(function(s) {
return s.indexOf('/nats-io/node-nats/test') > -1;
});

var ctx = lines[lines.length-1];
ctx = ctx.substr(ctx.lastIndexOf('/')+1);
ctx = ctx.substr(0, ctx.length-1);

extra = extra || '';
var cf = path.resolve(os.tmpdir(), 'test_ports.txt');
fs.appendFileSync(cf, port + ' ' + extra + ' ' + ctx + '\n');
}

function printLog() {
var cf = path.resolve(os.tmpdir(), 'test_ports.txt');
fs.readFile(cf, 'utf8', function(err, data){
if(err){
console.log('Error reading log', err);
return;
}
console.og(data);
});
}

exports.printLog = printLog;

exports.alloc_next_port = alloc_next_port;

function start_server(port, opt_flags, done) {
Expand All @@ -41,6 +74,8 @@ function start_server(port, opt_flags, done) {
flags = flags.concat(opt_flags);
}

log(port + ' ' + JSON.stringify(flags));

if (process.env.PRINT_LAUNCH_CMD) {
console.log(flags.join(" "));
}
Expand Down

0 comments on commit 04a789a

Please sign in to comment.