Skip to content

Commit 1358ddb

Browse files
committed
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: nodejs#21012
1 parent f86e5fc commit 1358ddb

4 files changed

+342
-310
lines changed
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
// Don't listen on the same port, because SmartOS sometimes says
64+
// that the server's fd is closed, but it still cannot listen
65+
// on the same port again.
66+
//
67+
// An isolated test for this would be lovely, but for now, this
68+
// will have to do.
69+
server.listen(0, function() {
70+
console.log('testSocket, listening');
71+
const connect = net.connect(server.address().port);
72+
let store = '';
73+
connect.on('data', function(chunk) {
74+
store += chunk;
75+
console.log('CLIENT: got data');
76+
});
77+
connect.on('close', function() {
78+
console.log('CLIENT: closed');
79+
assert.strictEqual(store, 'echo');
80+
server.close();
81+
});
82+
});
83+
}
84+
85+
// Create socket and send it to child.
86+
child.on('message', function onReady(msg) {
87+
if (msg.what !== 'ready') return;
88+
child.removeListener('message', onReady);
89+
90+
testSocket(common.mustCall());
91+
});
92+
}

0 commit comments

Comments
 (0)