Skip to content

Commit

Permalink
Better test determinism
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreavis committed Oct 11, 2015
1 parent 3d13ffb commit e3e2a9a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/TileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ TileServer.prototype.close = function(callback) {
if (callback) callback(err);
};

async.parallel([
async.series([
function closeBalancer(callback) {
if (!self.balancer) return callback();
clearTimeout(self.balancer_timeout);
Expand Down Expand Up @@ -569,7 +569,7 @@ TileServer.prototype.close = function(callback) {
async.each(_.values(layer.routes), function(route, callback) {
route.handler._destroy(self, callback);
}, function(err) {
// errors are logged in _close()
// errors are logged in _destroy()
if (err) was_error = true;
callback();
});
Expand Down
20 changes: 14 additions & 6 deletions test/balancer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var http = require('http');
var async = require('async');
var log = require('npmlog');
var assert = require('chai').assert;
var version = require('../package.json').version;
var tilestrata = require('../index.js');
Expand All @@ -8,10 +9,15 @@ var balancer, strata;

describe('TileStrata Balancer integration', function() {
afterEach(function(done) {
async.parallel([
function(callback) { if (balancer) { balancer.close(callback); } else { callback(); }},
function(callback) { if (strata) { strata.close(callback); } else { callback(); }}
], function(err) { done(); });
var closed = 0;
async.series([
// close the mock balancer first so that DELETE calls don't come through when strata closed
function(callback) { if (balancer) { closed++; balancer.close(callback); } else { callback(); }},
function(callback) { if (strata) { closed++; strata.close(callback); } else { callback(); }}
], function(err) {
log.info('test', 'afterEach(): ' + closed + ' listener(s) stopped');
done();
});
});

it('should call DELETE /nodes/:id on close()', function(done) {
Expand Down Expand Up @@ -74,6 +80,7 @@ describe('TileStrata Balancer integration', function() {
it('should POST to /nodes until "201 Created" received', function(done) {
this.timeout(1000);
var calls = 0;
var finished = false;

async.series([
function setupBalancer(callback) {
Expand Down Expand Up @@ -108,7 +115,8 @@ describe('TileStrata Balancer integration', function() {
return res.end('err');
}

if (i >= 6) throw new Error('Called /nodes too many times');
if (finished) throw new Error('Called /nodes too many times');
finished = true;
res.writeHead(201, {'Content-Type': 'application/json'})
res.end('{"check_interval": 1000, "token": "a"}');

Expand Down Expand Up @@ -279,7 +287,7 @@ describe('TileStrata Balancer integration', function() {
setTimeout(function() {
assert.isAbove(reconnects, 2);
done();
}, check_interval * 4);
}, check_interval * 10);
}
], function(err) {
if (err) throw err;
Expand Down

0 comments on commit e3e2a9a

Please sign in to comment.