Skip to content

Commit 9d41ab4

Browse 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

5 files changed

Lines changed: 338 additions & 311 deletions

‎test/parallel/parallel.status‎

Lines 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

Lines 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+
constcommon=require('../common');
24+
constassert=require('assert');
25+
constfork=require('child_process').fork;
26+
constnet=require('net');
27+
28+
functionProgressTracker(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+
letserverScope;
43+
44+
process.on('message',functiononServer(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',functiononClose(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+
constchild=fork(process.argv[1],['child']);
75+
76+
child.on('exit',common.mustCall(function(code,signal){
77+
constmessage=`CHILD: died with ${code}, ${signal}`;
78+
assert.strictEqual(code,0,message);
79+
}));
80+
81+
// Send net.Server to child and test by connecting.
82+
functiontestServer(callback){
83+
84+
// Destroy server execute callback when done.
85+
constprogress=newProgressTracker(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+
constconnections=newProgressTracker(4,progress.done.bind(progress));
95+
constclosed=newProgressTracker(4,progress.done.bind(progress));
96+
97+
// Create server and send it to child.
98+
constserver=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+
functionmessageHandlers(msg){
112+
113+
if(msg.what==='listening'){
114+
// Make connections.
115+
letsocket;
116+
for(leti=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+
}elseif(msg.what==='connection'){
127+
// child got connection
128+
connections.done();
129+
}elseif(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',functiononReady(msg){
140+
if(msg.what!=='ready')return;
141+
child.removeListener('message',onReady);
142+
143+
testServer(common.mustCall());
144+
});
145+
}
Lines 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+
constcommon=require('../common');
24+
constassert=require('assert');
25+
constfork=require('child_process').fork;
26+
constnet=require('net');
27+
28+
if(process.argv[2]==='child'){
29+
30+
process.on('message',functiononSocket(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+
constchild=fork(process.argv[1],['child']);
41+
42+
child.on('exit',common.mustCall(function(code,signal){
43+
constmessage=`CHILD: died with ${code}, ${signal}`;
44+
assert.strictEqual(code,0,message);
45+
}));
46+
47+
// Send net.Socket to child.
48+
functiontestSocket(callback){
49+
50+
// Create a new server and connect to it,
51+
// but the socket will be handled by the child.
52+
constserver=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+
constconnect=net.connect(server.address().port);
67+
letstore='';
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',functiononReady(msg){
82+
if(msg.what!=='ready')return;
83+
child.removeListener('message',onReady);
84+
85+
testSocket(common.mustCall());
86+
});
87+
}

0 commit comments

Comments
 (0)