Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 07e4131

Browse filesBrowse files
jasnellMylesBorins
authored andcommitted
test: refactoring / cleanup on child-process tests
PR-URL: #32078 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
1 parent dfcc3e8 commit 07e4131
Copy full SHA for 07e4131
Expand file treeCollapse file tree

17 files changed

+309
-261
lines changed
Open diff view settings
Collapse file

‎test/fixtures/child-process-spawn-node.js‎

Copy file name to clipboardExpand all lines: test/fixtures/child-process-spawn-node.js
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const assert = require('assert');
2+
const debug = require('util').debuglog('test');
23

34
function onmessage(m) {
4-
console.log('CHILD got message:', m);
5+
debug('CHILD got message:', m);
56
assert.ok(m.hello);
67
process.removeListener('message', onmessage);
78
}
Collapse file

‎test/parallel/test-bash-completion.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-bash-completion.js
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
require('../common');
33
const assert = require('assert');
44
const child_process = require('child_process');
5-
const { inspect } = require('util');
5+
const { debuglog, inspect } = require('util');
6+
const debug = debuglog('test');
67

78
const p = child_process.spawnSync(
89
process.execPath, [ '--completion-bash' ]);
910
assert.ifError(p.error);
1011

1112
const output = p.stdout.toString().trim().replace(/\r/g, '');
12-
console.log(output);
13+
debug(output);
1314

1415
const prefix = `_node_complete() {
1516
local cur_word options
Collapse file

‎test/parallel/test-child-process-default-options.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-child-process-default-options.js
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
const { isWindows } = require('../common');
2424
const assert = require('assert');
2525

2626
const spawn = require('child_process').spawn;
27+
const debug = require('util').debuglog('test');
2728

2829
process.env.HELLO = 'WORLD';
2930

3031
let child;
31-
if (common.isWindows) {
32+
if (isWindows) {
3233
child = spawn('cmd.exe', ['/c', 'set'], {});
3334
} else {
3435
child = spawn('/usr/bin/env', [], {});
@@ -39,7 +40,7 @@ let response = '';
3940
child.stdout.setEncoding('utf8');
4041

4142
child.stdout.on('data', function(chunk) {
42-
console.log(`stdout: ${chunk}`);
43+
debug(`stdout: ${chunk}`);
4344
response += chunk;
4445
});
4546

Collapse file

‎test/parallel/test-child-process-double-pipe.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-child-process-double-pipe.js
+38-29Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,22 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
const {
24+
isWindows,
25+
mustCall,
26+
mustCallAtLeast,
27+
} = require('../common');
2428
const assert = require('assert');
2529
const os = require('os');
2630
const spawn = require('child_process').spawn;
31+
const debug = require('util').debuglog('test');
2732

2833
// We're trying to reproduce:
2934
// $ echo "hello\nnode\nand\nworld" | grep o | sed s/o/a/
3035

3136
let grep, sed, echo;
3237

33-
if (common.isWindows) {
38+
if (isWindows) {
3439
grep = spawn('grep', ['--binary', 'o']);
3540
sed = spawn('sed', ['--binary', 's/o/O/']);
3641
echo = spawn('cmd.exe',
@@ -54,62 +59,66 @@ if (common.isWindows) {
5459

5560

5661
// pipe echo | grep
57-
echo.stdout.on('data', function(data) {
58-
console.error(`grep stdin write ${data.length}`);
62+
echo.stdout.on('data', mustCallAtLeast((data) => {
63+
debug(`grep stdin write ${data.length}`);
5964
if (!grep.stdin.write(data)) {
6065
echo.stdout.pause();
6166
}
62-
});
67+
}));
6368

64-
grep.stdin.on('drain', function(data) {
69+
// TODO(@jasnell): This does not appear to ever be
70+
// emitted. It's not clear if it is necessary.
71+
grep.stdin.on('drain', (data) => {
6572
echo.stdout.resume();
6673
});
6774

6875
// Propagate end from echo to grep
69-
echo.stdout.on('end', function(code) {
76+
echo.stdout.on('end', mustCall((code) => {
7077
grep.stdin.end();
71-
});
78+
}));
7279

73-
echo.on('exit', function() {
74-
console.error('echo exit');
75-
});
80+
echo.on('exit', mustCall(() => {
81+
debug('echo exit');
82+
}));
7683

77-
grep.on('exit', function() {
78-
console.error('grep exit');
79-
});
84+
grep.on('exit', mustCall(() => {
85+
debug('grep exit');
86+
}));
8087

81-
sed.on('exit', function() {
82-
console.error('sed exit');
83-
});
88+
sed.on('exit', mustCall(() => {
89+
debug('sed exit');
90+
}));
8491

8592

8693
// pipe grep | sed
87-
grep.stdout.on('data', function(data) {
88-
console.error(`grep stdout ${data.length}`);
94+
grep.stdout.on('data', mustCallAtLeast((data) => {
95+
debug(`grep stdout ${data.length}`);
8996
if (!sed.stdin.write(data)) {
9097
grep.stdout.pause();
9198
}
92-
});
99+
}));
93100

94-
sed.stdin.on('drain', function(data) {
101+
// TODO(@jasnell): This does not appear to ever be
102+
// emitted. It's not clear if it is necessary.
103+
sed.stdin.on('drain', (data) => {
95104
grep.stdout.resume();
96105
});
97106

98107
// Propagate end from grep to sed
99-
grep.stdout.on('end', function(code) {
100-
console.error('grep stdout end');
108+
grep.stdout.on('end', mustCall((code) => {
109+
debug('grep stdout end');
101110
sed.stdin.end();
102-
});
111+
}));
103112

104113

105114
let result = '';
106115

107116
// print sed's output
108-
sed.stdout.on('data', function(data) {
117+
sed.stdout.on('data', mustCallAtLeast((data) => {
109118
result += data.toString('utf8', 0, data.length);
110-
console.log(data);
111-
});
119+
debug(data);
120+
}));
112121

113-
sed.stdout.on('end', function(code) {
122+
sed.stdout.on('end', mustCall((code) => {
114123
assert.strictEqual(result, `hellO${os.EOL}nOde${os.EOL}wOrld${os.EOL}`);
115-
});
124+
}));
Collapse file

‎test/parallel/test-child-process-env.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-child-process-env.js
+12-7Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
const {
24+
isWindows,
25+
mustCall,
26+
mustCallAtLeast,
27+
} = require('../common');
2428
const assert = require('assert');
2529
const os = require('os');
30+
const debug = require('util').debuglog('test');
2631

2732
const spawn = require('child_process').spawn;
2833

@@ -38,7 +43,7 @@ Object.setPrototypeOf(env, {
3843
});
3944

4045
let child;
41-
if (common.isWindows) {
46+
if (isWindows) {
4247
child = spawn('cmd.exe', ['/c', 'set'], { env });
4348
} else {
4449
child = spawn('/usr/bin/env', [], { env });
@@ -49,15 +54,15 @@ let response = '';
4954

5055
child.stdout.setEncoding('utf8');
5156

52-
child.stdout.on('data', (chunk) => {
53-
console.log(`stdout: ${chunk}`);
57+
child.stdout.on('data', mustCallAtLeast((chunk) => {
58+
debug(`stdout: ${chunk}`);
5459
response += chunk;
55-
});
60+
}));
5661

57-
process.on('exit', () => {
62+
child.stdout.on('end', mustCall(() => {
5863
assert.ok(response.includes('HELLO=WORLD'));
5964
assert.ok(response.includes('FOO=BAR'));
6065
assert.ok(!response.includes('UNDEFINED=undefined'));
6166
assert.ok(response.includes('NULL=null'));
6267
assert.ok(response.includes(`EMPTY=${os.EOL}`));
63-
});
68+
}));
Collapse file

‎test/parallel/test-child-process-exec-env.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-child-process-exec-env.js
+8-6Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
const { isWindows } = require('../common');
2424
const assert = require('assert');
2525
const exec = require('child_process').exec;
26+
const debug = require('util').debuglog('test');
27+
2628
let success_count = 0;
2729
let error_count = 0;
2830
let response = '';
@@ -31,17 +33,17 @@ let child;
3133
function after(err, stdout, stderr) {
3234
if (err) {
3335
error_count++;
34-
console.log(`error!: ${err.code}`);
35-
console.log(`stdout: ${JSON.stringify(stdout)}`);
36-
console.log(`stderr: ${JSON.stringify(stderr)}`);
36+
debug(`error!: ${err.code}`);
37+
debug(`stdout: ${JSON.stringify(stdout)}`);
38+
debug(`stderr: ${JSON.stringify(stderr)}`);
3739
assert.strictEqual(err.killed, false);
3840
} else {
3941
success_count++;
4042
assert.notStrictEqual(stdout, '');
4143
}
4244
}
4345

44-
if (!common.isWindows) {
46+
if (!isWindows) {
4547
child = exec('/usr/bin/env', { env: { 'HELLO': 'WORLD' } }, after);
4648
} else {
4749
child = exec('set',
@@ -55,7 +57,7 @@ child.stdout.on('data', function(chunk) {
5557
});
5658

5759
process.on('exit', function() {
58-
console.log('response: ', response);
60+
debug('response: ', response);
5961
assert.strictEqual(success_count, 1);
6062
assert.strictEqual(error_count, 0);
6163
assert.ok(response.includes('HELLO=WORLD'));
Collapse file

‎test/parallel/test-child-process-fork-getconnections.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-child-process-fork-getconnections.js
+17-30Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525
const fork = require('child_process').fork;
2626
const net = require('net');
@@ -29,7 +29,7 @@ const count = 12;
2929
if (process.argv[2] === 'child') {
3030
const sockets = [];
3131

32-
process.on('message', function(m, socket) {
32+
process.on('message', common.mustCall((m, socket) => {
3333
function sendClosed(id) {
3434
process.send({ id: id, status: 'closed' });
3535
}
@@ -52,41 +52,39 @@ if (process.argv[2] === 'child') {
5252
sockets[m.id].destroy();
5353
}
5454
}
55-
});
55+
}));
5656

5757
} else {
5858
const child = fork(process.argv[1], ['child']);
5959

60-
child.on('exit', function(code, signal) {
60+
child.on('exit', common.mustCall((code, signal) => {
6161
if (!subprocessKilled) {
6262
assert.fail('subprocess died unexpectedly! ' +
6363
`code: ${code} signal: ${signal}`);
6464
}
65-
});
65+
}));
6666

6767
const server = net.createServer();
6868
const sockets = [];
69-
let sent = 0;
7069

71-
server.on('connection', function(socket) {
70+
server.on('connection', common.mustCall((socket) => {
7271
child.send({ cmd: 'new' }, socket);
7372
sockets.push(socket);
7473

7574
if (sockets.length === count) {
7675
closeSockets(0);
7776
}
78-
});
77+
}, count));
7978

80-
let disconnected = 0;
81-
server.on('listening', function() {
79+
const onClose = common.mustCall(count);
80+
81+
server.on('listening', common.mustCall(() => {
8282
let j = count;
8383
while (j--) {
8484
const client = net.connect(server.address().port, '127.0.0.1');
85-
client.on('close', function() {
86-
disconnected += 1;
87-
});
85+
client.on('close', onClose);
8886
}
89-
});
87+
}));
9088

9189
let subprocessKilled = false;
9290
function closeSockets(i) {
@@ -97,29 +95,18 @@ if (process.argv[2] === 'child') {
9795
return;
9896
}
9997

100-
child.once('message', function(m) {
98+
child.once('message', common.mustCall((m) => {
10199
assert.strictEqual(m.status, 'closed');
102-
server.getConnections(function(err, num) {
100+
server.getConnections(common.mustCall((err, num) => {
103101
assert.ifError(err);
104102
assert.strictEqual(num, count - (i + 1));
105103
closeSockets(i + 1);
106-
});
107-
});
108-
sent++;
104+
}));
105+
}));
109106
child.send({ id: i, cmd: 'close' });
110107
}
111108

112-
let closeEmitted = false;
113-
server.on('close', function() {
114-
closeEmitted = true;
115-
});
109+
server.on('close', common.mustCall());
116110

117111
server.listen(0, '127.0.0.1');
118-
119-
process.on('exit', function() {
120-
assert.strictEqual(sent, count);
121-
assert.strictEqual(disconnected, count);
122-
assert.ok(closeEmitted);
123-
console.log('ok');
124-
});
125112
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.