I am performing acceptance testing on a chat application in Meteor with chimp. I create test users at server startup, and would like to remove them on shutdown.
I've found what should be a solution here: doing a cleanup action just before node.js exits,
but this does not fire when the server is shutdown.
The code is as follows:
Meteor.startup(function() {
....
if (process.env.TEST_MODE === 'true') {
process.stdin.resume();//so the program will not close instantly
function exitHandler(options, err) {
if (options.cleanup) //Teardown test users;
if (err) console.log(err.stack);
if (options.exit) process.exit();
}
//do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, {exit:true}));
process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
}
....
}