0

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}));

    }

    ....

}
Kevin Amick
  • 1
  • 1
  • 2

1 Answers1

0

Im working in intellij and it turns out when stopping processes using the red square, it ends them "ungracefully", so the exit hooks do not run. So this code does work as intended.

Kevin Amick
  • 1
  • 1
  • 2