From de1fb3ff22dbdcb61db4742cd56b24f884167515 Mon Sep 17 00:00:00 2001 From: bonte Date: Wed, 14 Jun 2017 15:35:23 +0200 Subject: [PATCH] test(*): create test script --- test/test.js | 52 ++++++++++++++++++++++++++++++++++++++++++++ test/test_delay.js | 14 ++++++++++++ test/test_process.js | 5 +++++ 3 files changed, 71 insertions(+) create mode 100644 test/test.js create mode 100644 test/test_delay.js create mode 100644 test/test_process.js diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..3b9873d --- /dev/null +++ b/test/test.js @@ -0,0 +1,52 @@ +const { test } = require('ava'); +const childProcess = require('child_process'); + +test.cb('I can execute a function when i kill process with SIGINT signal', t => { + const child = childProcess.fork(`${__dirname}/test_process.js`); + child.on('message', message => { + t.deepEqual(message, 'exit') + t.end() + }) + setTimeout(() => {child.kill('SIGINT')},1000); +}); + +test.cb('I can execute a function when i kill process with SIGTERM signal', t => { + const child = childProcess.fork(`${__dirname}/test_process.js`); + child.on('message', message => { + t.deepEqual(message, 'exit') + t.end() + }) + + setTimeout(() => {child.kill('SIGTERM')},500); +}); + +test.cb('I can not execute a function when i kill process without SIGTERM or SIGINT signal', t => { + const child = childProcess.fork(`${__dirname}/test_process.js`); + child.on('message', message => { + t.fail() + t.end() + }) + child.on('exit', message => { + t.pass() + t.end() + }) + setTimeout(() => {child.kill('SIGALRM')},500); +}); + +test.cb('I can kill the process before with a max delay', t => { + let timeout = true + const env = { EXIT_DELAY: 2000 } + const child = childProcess.fork(`${__dirname}/test_delay.js`, { env: env }); + child.on('exit', message => { + t.pass() + t.end() + timeout = false + }) + setTimeout(() => {child.kill('SIGTERM')},500); + setTimeout(() => { + if(timeout) + { + t.fail() + t.end() + }}, 2550); +}); \ No newline at end of file diff --git a/test/test_delay.js b/test/test_delay.js new file mode 100644 index 0000000..4a313a5 --- /dev/null +++ b/test/test_delay.js @@ -0,0 +1,14 @@ +const trap = require('../index') + +function wait(){ + return new Promise((resolve, reject) => { + setTimeout(() => { + resolve() + },5000); + }) +} +trap.setExitDelay(parseInt(process.env.EXIT_DELAY)) + +trap.onExit(async () => {await wait()}) + +setTimeout(() => {process.send('normal exit')},6000); \ No newline at end of file diff --git a/test/test_process.js b/test/test_process.js new file mode 100644 index 0000000..b3b8b32 --- /dev/null +++ b/test/test_process.js @@ -0,0 +1,5 @@ +const trap = require('../index') + +trap.onExit(() => {process.send('exit')}) + +setTimeout(() => {process.send('normal exit')},1000); \ No newline at end of file