Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c279760

Browse files
axxieremy
authored andcommitted
test: make sigint test to actually check child pid (#1656)
1 parent cd45d74 commit c279760

File tree

3 files changed

+52
-31
lines changed

3 files changed

+52
-31
lines changed

test/fixtures/sigint.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
process.on('SIGINT', function() {
2-
// do nothing here
2+
if (process.argv.length === 3 && process.argv[2] === '--dont-exit') return;
3+
process.exit();
34
});
45
// timer, to keep process running
56
setInterval(function() {

test/misc/sigint.test.js

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,51 @@ var utils = require('../utils'),
66
appjs = path.relative(process.cwd(), path.resolve(__dirname, '..', 'fixtures', 'sigint.js')),
77
match = utils.match,
88
cleanup = utils.cleanup,
9-
run = utils.run;
9+
run = utils.run,
10+
isRunning = utils.isRunning;
1011

11-
describe('terminal signals', function () {
12-
it('should kill child with SIGINT (after ~10 seconds)', function (done) {
13-
var childPID = null;
12+
function runAndKill(done, cmdline, exitcb)
13+
{
14+
var childPID = null;
1415

15-
var p = run(appjs, {
16-
output: function (data) {
17-
if (match(data, 'pid: ')) {
18-
data.replace(/pid: (\d+)/, function (m) {
19-
childPID = m;
20-
});
21-
}
22-
},
23-
error: function (data) {
24-
assert(false, 'nodemon failed with ' + data);
25-
cleanup(p, done);
16+
var p = run(cmdline, {
17+
output: function (data) {
18+
if (match(data, 'pid: ')) {
19+
data.replace(/pid: (\d+)/, function (_, p1) {
20+
childPID = p1;
21+
});
2622
}
27-
});
23+
},
24+
error: function (data) {
25+
assert(false, 'nodemon failed with ' + data);
26+
cleanup(p, done);
27+
}
28+
});
2829

29-
p.on('message', function (event) {
30-
if (event.type === 'start') {
31-
setTimeout(function () {
32-
p.kill('SIGINT');
33-
}, 1000);
34-
}
35-
}).on('exit', function () {
36-
// check if the child process is still running
37-
try {
38-
process.kill(childPID, 0);
39-
assert(false, 'child is still running at ' + childPID);
40-
} catch (e) {
41-
assert(true, 'child process was not running');
42-
}
30+
p.on('message', function (event) {
31+
if (event.type === 'start') {
32+
setTimeout(function () {
33+
p.kill('SIGINT');
34+
}, 1000);
35+
}
36+
}).on('exit', function () {
37+
exitcb(childPID);
38+
});
39+
}
40+
41+
describe('terminal signals', function () {
42+
it('should kill child with SIGINT', function (done) {
43+
runAndKill(done, appjs, function (childPID) {
44+
assert(!isRunning(childPID), 'child is still running at ' + childPID);
4345
done();
4446
});
4547
});
4648

49+
it('should terminate nodemon (after ~10 seconds)', function (done) {
50+
runAndKill(done, appjs + ' --dont-exit', function (childPID) {
51+
// make sure we don't keep abandoned child
52+
process.kill(childPID, 'SIGTERM');
53+
done();
54+
});
55+
});
4756
});

test/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ function getTriggerCount(msg) {
121121
return changes.pop();
122122
}
123123

124+
function isRunning(pid) {
125+
try {
126+
process.kill(pid, 0)
127+
return true
128+
} catch (error) {
129+
if (error.code && error.code === 'ESRCH') return false
130+
throw error
131+
}
132+
}
133+
124134
module.exports = {
125135
getTriggerCount: getTriggerCount,
126136
Plan: Plan,
@@ -132,4 +142,5 @@ module.exports = {
132142
appcoffee: appcoffee,
133143
monitorForChange: monitorForChange,
134144
port: port,
145+
isRunning: isRunning
135146
};

0 commit comments

Comments
 (0)