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 9d41ab4

Browse filesBrowse files
TrottMylesBorins
authored andcommitted
test: refactor child-process-fork-net
Split test-child-process-fork-net into test-child-process-fork-net-server and test-child-process-fork-net-socket. Rename test-child-process-fork-net2.js to test-child-process-fork-net.js. Refs: #21012 PR-URL: #21095 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 820236f commit 9d41ab4
Copy full SHA for 9d41ab4

File tree

Expand file treeCollapse file tree

5 files changed

+338
-311
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+338
-311
lines changed
Open diff view settings
Collapse file

‎test/parallel/parallel.status‎

Copy file name to clipboardExpand all lines: test/parallel/parallel.status
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ prefix parallel
99
test-postmortem-metadata: PASS,FLAKY
1010

1111
[$system==win32]
12-
test-child-process-fork-net: PASS,FLAKY
12+
test-child-process-fork-net-socket: PASS,FLAKY
1313

1414
[$system==linux]
1515

Collapse file
+145Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
'use strict';
23+
const common = require('../common');
24+
const assert = require('assert');
25+
const fork = require('child_process').fork;
26+
const net = require('net');
27+
28+
function ProgressTracker(missing, callback) {
29+
this.missing = missing;
30+
this.callback = callback;
31+
}
32+
ProgressTracker.prototype.done = function() {
33+
this.missing -= 1;
34+
this.check();
35+
};
36+
ProgressTracker.prototype.check = function() {
37+
if (this.missing === 0) this.callback();
38+
};
39+
40+
if (process.argv[2] === 'child') {
41+
42+
let serverScope;
43+
44+
process.on('message', function onServer(msg, server) {
45+
if (msg.what !== 'server') return;
46+
process.removeListener('message', onServer);
47+
48+
serverScope = server;
49+
50+
server.on('connection', function(socket) {
51+
console.log('CHILD: got connection');
52+
process.send({ what: 'connection' });
53+
socket.destroy();
54+
});
55+
56+
// Start making connection from parent.
57+
console.log('CHILD: server listening');
58+
process.send({ what: 'listening' });
59+
});
60+
61+
process.on('message', function onClose(msg) {
62+
if (msg.what !== 'close') return;
63+
process.removeListener('message', onClose);
64+
65+
serverScope.on('close', function() {
66+
process.send({ what: 'close' });
67+
});
68+
serverScope.close();
69+
});
70+
71+
process.send({ what: 'ready' });
72+
} else {
73+
74+
const child = fork(process.argv[1], ['child']);
75+
76+
child.on('exit', common.mustCall(function(code, signal) {
77+
const message = `CHILD: died with ${code}, ${signal}`;
78+
assert.strictEqual(code, 0, message);
79+
}));
80+
81+
// Send net.Server to child and test by connecting.
82+
function testServer(callback) {
83+
84+
// Destroy server execute callback when done.
85+
const progress = new ProgressTracker(2, function() {
86+
server.on('close', function() {
87+
console.log('PARENT: server closed');
88+
child.send({ what: 'close' });
89+
});
90+
server.close();
91+
});
92+
93+
// We expect 4 connections and close events.
94+
const connections = new ProgressTracker(4, progress.done.bind(progress));
95+
const closed = new ProgressTracker(4, progress.done.bind(progress));
96+
97+
// Create server and send it to child.
98+
const server = net.createServer();
99+
server.on('connection', function(socket) {
100+
console.log('PARENT: got connection');
101+
socket.destroy();
102+
connections.done();
103+
});
104+
server.on('listening', function() {
105+
console.log('PARENT: server listening');
106+
child.send({ what: 'server' }, server);
107+
});
108+
server.listen(0);
109+
110+
// Handle client messages.
111+
function messageHandlers(msg) {
112+
113+
if (msg.what === 'listening') {
114+
// Make connections.
115+
let socket;
116+
for (let i = 0; i < 4; i++) {
117+
socket = net.connect(server.address().port, function() {
118+
console.log('CLIENT: connected');
119+
});
120+
socket.on('close', function() {
121+
closed.done();
122+
console.log('CLIENT: closed');
123+
});
124+
}
125+
126+
} else if (msg.what === 'connection') {
127+
// child got connection
128+
connections.done();
129+
} else if (msg.what === 'close') {
130+
child.removeListener('message', messageHandlers);
131+
callback();
132+
}
133+
}
134+
135+
child.on('message', messageHandlers);
136+
}
137+
138+
// Create server and send it to child.
139+
child.on('message', function onReady(msg) {
140+
if (msg.what !== 'ready') return;
141+
child.removeListener('message', onReady);
142+
143+
testServer(common.mustCall());
144+
});
145+
}
Collapse file
+87Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
'use strict';
23+
const common = require('../common');
24+
const assert = require('assert');
25+
const fork = require('child_process').fork;
26+
const net = require('net');
27+
28+
if (process.argv[2] === 'child') {
29+
30+
process.on('message', function onSocket(msg, socket) {
31+
if (msg.what !== 'socket') return;
32+
process.removeListener('message', onSocket);
33+
socket.end('echo');
34+
console.log('CHILD: got socket');
35+
});
36+
37+
process.send({ what: 'ready' });
38+
} else {
39+
40+
const child = fork(process.argv[1], ['child']);
41+
42+
child.on('exit', common.mustCall(function(code, signal) {
43+
const message = `CHILD: died with ${code}, ${signal}`;
44+
assert.strictEqual(code, 0, message);
45+
}));
46+
47+
// Send net.Socket to child.
48+
function testSocket(callback) {
49+
50+
// Create a new server and connect to it,
51+
// but the socket will be handled by the child.
52+
const server = net.createServer();
53+
server.on('connection', function(socket) {
54+
socket.on('close', function() {
55+
console.log('CLIENT: socket closed');
56+
});
57+
child.send({ what: 'socket' }, socket);
58+
});
59+
server.on('close', function() {
60+
console.log('PARENT: server closed');
61+
callback();
62+
});
63+
64+
server.listen(0, function() {
65+
console.log('testSocket, listening');
66+
const connect = net.connect(server.address().port);
67+
let store = '';
68+
connect.on('data', function(chunk) {
69+
store += chunk;
70+
console.log('CLIENT: got data');
71+
});
72+
connect.on('close', function() {
73+
console.log('CLIENT: closed');
74+
assert.strictEqual(store, 'echo');
75+
server.close();
76+
});
77+
});
78+
}
79+
80+
// Create socket and send it to child.
81+
child.on('message', function onReady(msg) {
82+
if (msg.what !== 'ready') return;
83+
child.removeListener('message', onReady);
84+
85+
testSocket(common.mustCall());
86+
});
87+
}

0 commit comments

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