Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
feat(mock): support a means of consistently cleaning up mock servers
Browse files Browse the repository at this point in the history
NODE-1132
  • Loading branch information
mbroadst committed Sep 16, 2017
1 parent 654802e commit ab3b70b
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion test/mock/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
var Server = require('./lib/server');

const cleanup = (servers, spy, callback) => {
if (!Array.isArray(servers)) {
throw new Error('First argument must be an array of mock servers');
}

if (spy) {
const alreadyDrained = spy.connectionCount() === 0;
const finish = () => {
callback(null, null);
};

if (!alreadyDrained) {
spy.once('drained', () => finish());
}

const cleanupPromise = Promise.all(servers.map(server => server.destroy())).catch(err =>
callback(err, null)
);

if (alreadyDrained) {
cleanupPromise.then(() => finish());
}
} else {
Promise.all(servers.map(server => server.destroy()))
.then(() => callback(null, null))
.catch(err => callback(err, null));
}
};

/*
* Main module
*/
module.exports = {
createServer: function(port, host, options) {
options = options || {};
return new Server(port, host, options).start();
}
},

cleanup: cleanup
};

0 comments on commit ab3b70b

Please sign in to comment.