Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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);
});
14 changes: 14 additions & 0 deletions test/test_delay.js
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 5 additions & 0 deletions test/test_process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const trap = require('../index')

trap.onExit(() => {process.send('exit')})

setTimeout(() => {process.send('normal exit')},1000);