Skip to content

Commit e36ed44

Browse files
joyeecheungRafaelGSS
authored andcommitted
test: deflake child process exec timeout tests
On Windows it might take too long for the parent to start the communication with a child process, so by the time the parent starts its own timer, the child process might have already completed running, and the parent in those tests won't have a chance to terminate these child processes after the timeout. To address this issue, raise the time for which the child is supposed to run to make sure that the parent starts its own timer before the child terminates in the tests. Also, split the test into smaller ones to reduce the overhead. PR-URL: #44390 Refs: nodejs/reliability#356 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Stephen Belanger <[email protected]>
1 parent 0af15c7 commit e36ed44

5 files changed

+168
-73
lines changed

test/common/child_process.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const common = require('./');
5+
6+
// Workaround for Windows Server 2008R2
7+
// When CMD is used to launch a process and CMD is killed too quickly, the
8+
// process can stay behind running in suspended state, never completing.
9+
function cleanupStaleProcess(filename) {
10+
if (!common.isWindows) {
11+
return;
12+
}
13+
process.once('beforeExit', () => {
14+
const basename = filename.replace(/.*[/\\]/g, '');
15+
require('child_process')
16+
.execFileSync(`${process.env.SystemRoot}\\System32\\wbem\\WMIC.exe`, [
17+
'process',
18+
'where',
19+
`commandline like '%${basename}%child'`,
20+
'delete',
21+
'/nointeractive',
22+
]);
23+
});
24+
}
25+
26+
// This should keep the child process running long enough to expire
27+
// the timeout.
28+
const kExpiringChildRunTime = common.platformTimeout(20 * 1000);
29+
const kExpiringParentTimer = 1;
30+
assert(kExpiringChildRunTime > kExpiringParentTimer);
31+
32+
function logAfterTime(time) {
33+
setTimeout(() => {
34+
// The following console statements are part of the test.
35+
console.log('child stdout');
36+
console.error('child stderr');
37+
}, time);
38+
}
39+
40+
module.exports = {
41+
cleanupStaleProcess,
42+
logAfterTime,
43+
kExpiringChildRunTime,
44+
kExpiringParentTimer
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
// Test exec() with a timeout that expires.
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
const cp = require('child_process');
8+
9+
const {
10+
cleanupStaleProcess,
11+
logAfterTime,
12+
kExpiringChildRunTime,
13+
kExpiringParentTimer
14+
} = require('../common/child_process');
15+
16+
if (process.argv[2] === 'child') {
17+
logAfterTime(kExpiringChildRunTime);
18+
return;
19+
}
20+
21+
const cmd = `"${process.execPath}" "${__filename}" child`;
22+
23+
cp.exec(cmd, {
24+
timeout: kExpiringParentTimer,
25+
}, common.mustCall((err, stdout, stderr) => {
26+
console.log('[stdout]', stdout.trim());
27+
console.log('[stderr]', stderr.trim());
28+
29+
let sigterm = 'SIGTERM';
30+
assert.strictEqual(err.killed, true);
31+
// TODO OpenBSD returns a null signal and 143 for code
32+
if (common.isOpenBSD) {
33+
assert.strictEqual(err.code, 143);
34+
sigterm = null;
35+
} else {
36+
assert.strictEqual(err.code, null);
37+
}
38+
// At least starting with Darwin Kernel Version 16.4.0, sending a SIGTERM to a
39+
// process that is still starting up kills it with SIGKILL instead of SIGTERM.
40+
// See: https://github.com/libuv/libuv/issues/1226
41+
if (common.isOSX)
42+
assert.ok(err.signal === 'SIGTERM' || err.signal === 'SIGKILL');
43+
else
44+
assert.strictEqual(err.signal, sigterm);
45+
assert.strictEqual(err.cmd, cmd);
46+
assert.strictEqual(stdout.trim(), '');
47+
assert.strictEqual(stderr.trim(), '');
48+
}));
49+
50+
cleanupStaleProcess(__filename);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
// Test exec() with both a timeout and a killSignal.
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
const cp = require('child_process');
8+
9+
const {
10+
cleanupStaleProcess,
11+
logInTimeout,
12+
kExpiringChildRunTime,
13+
kExpiringParentTimer,
14+
} = require('../common/child_process');
15+
16+
if (process.argv[2] === 'child') {
17+
logInTimeout(kExpiringChildRunTime);
18+
return;
19+
}
20+
21+
const cmd = `"${process.execPath}" "${__filename}" child`;
22+
23+
// Test with a different kill signal.
24+
cp.exec(cmd, {
25+
timeout: kExpiringParentTimer,
26+
killSignal: 'SIGKILL'
27+
}, common.mustCall((err, stdout, stderr) => {
28+
console.log('[stdout]', stdout.trim());
29+
console.log('[stderr]', stderr.trim());
30+
31+
assert.strictEqual(err.killed, true);
32+
assert.strictEqual(err.code, null);
33+
assert.strictEqual(err.signal, 'SIGKILL');
34+
assert.strictEqual(err.cmd, cmd);
35+
assert.strictEqual(stdout.trim(), '');
36+
assert.strictEqual(stderr.trim(), '');
37+
}));
38+
39+
cleanupStaleProcess(__filename);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
// Test exec() when a timeout is set, but not expired.
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
const cp = require('child_process');
8+
9+
const {
10+
cleanupStaleProcess,
11+
logAfterTime
12+
} = require('../common/child_process');
13+
14+
const kTimeoutNotSupposedToExpire = 2 ** 30;
15+
const childRunTime = common.platformTimeout(100);
16+
17+
// The time spent in the child should be smaller than the timeout below.
18+
assert(childRunTime < kTimeoutNotSupposedToExpire);
19+
20+
if (process.argv[2] === 'child') {
21+
logAfterTime(childRunTime);
22+
return;
23+
}
24+
25+
const cmd = `"${process.execPath}" "${__filename}" child`;
26+
27+
cp.exec(cmd, {
28+
timeout: kTimeoutNotSupposedToExpire
29+
}, common.mustSucceed((stdout, stderr) => {
30+
assert.strictEqual(stdout.trim(), 'child stdout');
31+
assert.strictEqual(stderr.trim(), 'child stderr');
32+
}));
33+
34+
cleanupStaleProcess(__filename);

test/parallel/test-child-process-exec-timeout.js

-73
This file was deleted.

0 commit comments

Comments
 (0)