Skip to content

Commit

Permalink
[FIX] Made all server stops sync (#166)
Browse files Browse the repository at this point in the history
* [FIX] Made all server stops sync, otherwise test setup/destroy had a chance of interfering with the wrong process.
[FIX] Removed test that is effectively non-sensical callback test.

* [FIX] Made all server stops sync, otherwise test setup/destroy had a chance of interfering with the wrong process.
  • Loading branch information
Alberto Ricart authored Oct 20, 2017
1 parent db697ec commit 09418f6
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 192 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ sudo: false
node_js:
- "4"
- "6"
- "7"
- "8"

before_script:
- wget https://github.com/nats-io/gnatsd/releases/download/v1.0.4/gnatsd-v1.0.4-linux-amd64.zip -qO tmp.zip
- unzip tmp.zip
- mv gnatsd-v1.0.4-linux-amd64 gnatsd

after_success:
- npm run coveralls
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
9 changes: 4 additions & 5 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('Authorization', function() {
});

// Shutdown our server after we are done
after(function() {
server.kill();
after(function(done) {
nsc.stop_server(server, done);
});

it('should fail to connect with no credentials ', function(done) {
Expand Down Expand Up @@ -83,8 +83,8 @@ describe('Token Authorization', function() {
});

// Shutdown our server after we are done
after(function() {
server.kill();
after(function(done) {
server = nsc.stop_server(server, done);
});

it('should fail to connect with no credentials ', function(done) {
Expand Down Expand Up @@ -128,5 +128,4 @@ describe('Token Authorization', function() {
setTimeout(done, 100);
});
});

});
5 changes: 2 additions & 3 deletions test/autounsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('Max responses and Auto-unsub', function() {
});

// Shutdown our server after we are done
after(function() {
server.kill();
after(function(done) {
nsc.stop_server(server, done);
});

it('should only received max responses requested', function(done) {
Expand Down Expand Up @@ -188,5 +188,4 @@ describe('Max responses and Auto-unsub', function() {
});

});

});
4 changes: 2 additions & 2 deletions test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('Basics', function() {
});

// Shutdown our server
after(function() {
nsc.stop_server(server);
after(function(done) {
nsc.stop_server(server, done);
});

it('should do basic subscribe and unsubscribe', function(done) {
Expand Down
4 changes: 2 additions & 2 deletions test/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe('Binary', function() {
});

// Shutdown our server
after(function() {
server.kill();
after(function(done) {
nsc.stop_server(server, done);
});


Expand Down
4 changes: 2 additions & 2 deletions test/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('Buffer', function() {
});

// Shutdown our server
after(function() {
server.kill();
after(function(done) {
nsc.stop_server(server, done);
});

it('should allow sending and receiving raw buffers', function(done) {
Expand Down
4 changes: 2 additions & 2 deletions test/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('Callbacks', function() {
});

// Shutdown our server
after(function() {
server.kill();
after(function(done) {
nsc.stop_server(server, done);
});

it('should properly do a publish callback after connection is closed', function(done) {
Expand Down
15 changes: 9 additions & 6 deletions test/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ describe('Cluster', function() {
});

// Shutdown our server
after(function() {
s1.kill();
s2.kill();
after(function(done) {
nsc.stop_cluster([s1, s2], done);
});

it('should accept servers options', function(done) {
Expand Down Expand Up @@ -152,11 +151,15 @@ describe('Cluster', function() {
var startTime;
var numAttempts = 0;
nc.on('connect', function() {
s1.kill();
startTime = new Date();
nsc.stop_server(s1, function(){
s1 = null;
startTime = new Date();
});
});
nc.on('reconnect', function() {
s2.kill();
nsc.stop_server(s2, function(){
s2 = null;
});
});
nc.on('reconnecting', function(client) {
var elapsed = new Date() - startTime;
Expand Down
4 changes: 2 additions & 2 deletions test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe('Basic Connectivity', function() {
});

// Shutdown our server after we are done
after(function() {
server.kill();
after(function(done) {
nsc.stop_server(server, done);
});


Expand Down
4 changes: 2 additions & 2 deletions test/dsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe('Double SUBS', function() {
});

// Shutdown our server after we are done
after(function() {
server.kill();
after(function(done) {
nsc.stop_server(server, done);
});

it('should not send multiple subscriptions on startup', function(done) {
Expand Down
117 changes: 5 additions & 112 deletions test/dyncluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ describe('Dynamic Cluster - Connect URLs', function() {
// this to enable per test cleanup
var servers;
// Shutdown our servers
afterEach(function() {
nsc.stop_cluster(servers);
servers = [];
afterEach(function(done) {
nsc.stop_cluster(servers, function() {
servers = [];
done();
});
});

it('adding cluster performs update', function(done) {
Expand Down Expand Up @@ -159,113 +161,4 @@ describe('Dynamic Cluster - Connect URLs', function() {
}
});
});

it('error connecting raises error and closes', function(done) {
reconnectTest(55421, 55420, true, done);
});

it('error connecting raises error and closes - non tls', function(done) {
reconnectTest(55521, 55520, false, done);
});

function reconnectTest(port, route_port, use_certs, done) {
var config = {
tls: {
cert_file: path.resolve(process.cwd(), "./test/certs/server-cert.pem"),
key_file: path.resolve(process.cwd(), "./test/certs/server-key.pem"),
ca_file: path.resolve(process.cwd(), "./test/certs/ca.pem"),
verify: false,
timeout: 2.0
},
authorization: {
user: "test",
// password is 'test'
password: "$2a$10$P6A99S9.wOT3yzNcz0OY/OBatni9Jl01AMuzRUkhXei6nOadn6R4C",
timeout: 2.0
}
};

if (!use_certs) {
delete config.tls;
}

// write two normal configs
var normal = JSON.parse(JSON.stringify(config));
var normal_conf = path.resolve(os.tmpdir(), 'normalauth_' + nuid.next() + '.conf');
ncu.writeFile(normal_conf, ncu.j(normal));

// one that has bad timeout
var short = JSON.parse(JSON.stringify(normal));

if (use_certs) {
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
servers = nsc.start_cluster([port], route_port, ['-c', normal_conf], function() {
process.nextTick(function() {
var others = nsc.add_member_with_delay([port + 1], route_port, 250, ['-c', short_conf], function() {
// add the second server
servers.push(others[0]);
process.nextTick(function() {
startClient();
});
});
});
});

function startClient() {
// connect the client
var opts = {
port: port,
reconnectTimeWait: 100,
maxReconnectAttempts: 2,
user: 'test',
password: 'test',
noRandomize: true,
tls: {
rejectUnauthorized: false
}
};
if (!use_certs) {
delete opts.tls;
}

var nc = NATS.connect(opts);
var connected = false;
nc.on('connect', function(c) {
if (!connected) {
// should(nc.servers.length).be.equal(2);
// now we disconnect first server
connected = true;
process.nextTick(function() {
servers[0].kill();
});
}
});

var errors = [];
nc.on('error', function(e) {
// save the error
errors.push(e);
});
nc.on('close', function() {
should.ok(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((port + 1) + '') !== -1);
done();
});
var disconnects = [];
nc.on('disconnect', function() {
var p = nc.currentServer.url.port;
if (disconnects.indexOf(p) === -1) {
disconnects.push(p);
}
});
}
}
});
4 changes: 2 additions & 2 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('Errors', function() {
});

// Shutdown our server after we are done
after(function() {
server.kill();
after(function(done) {
nsc.stop_server(server, done);
});

it('should throw errors on connect', function(done) {
Expand Down
4 changes: 2 additions & 2 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe('JSON payloads', function() {
});

// Shutdown our server
after(function() {
server.kill();
after(function(done) {
nsc.stop_server(server, done);
});


Expand Down
6 changes: 3 additions & 3 deletions test/perms.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Auth Basics', function() {
},
SUB: {
subscribe: "bar",
publish: "bar",
publish: "bar"
},
PUB: {
subscribe: "foo",
Expand Down Expand Up @@ -58,8 +58,8 @@ describe('Auth Basics', function() {
});

// Shutdown our server
after(function() {
nsc.stop_server(server);
after(function(done) {
nsc.stop_server(server, done);
});

it('bar cannot subscribe/pub foo', function(done) {
Expand Down
4 changes: 2 additions & 2 deletions test/queues.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('Queues', function() {
});

// Shutdown our server
after(function() {
server.kill();
after(function(done) {
nsc.stop_server(server, done);
});

it('should deliver a message to single member of a queue group', function(done) {
Expand Down
Loading

0 comments on commit 09418f6

Please sign in to comment.