2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2024-02-23 01:12:31 +00:00
|
|
|
// This tests that a socket sent to the forked process works.
|
|
|
|
// See https://github.com/nodejs/node/commit/dceebbfa
|
|
|
|
|
2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2020-03-23 20:06:24 +00:00
|
|
|
const {
|
|
|
|
mustCall,
|
|
|
|
mustCallAtLeast,
|
|
|
|
platformTimeout,
|
|
|
|
} = require('../common');
|
2016-05-29 07:06:56 +00:00
|
|
|
const assert = require('assert');
|
2016-04-09 23:25:18 +00:00
|
|
|
const fork = require('child_process').fork;
|
|
|
|
const net = require('net');
|
2020-03-23 20:06:24 +00:00
|
|
|
const debug = require('util').debuglog('test');
|
2018-06-02 10:34:06 +00:00
|
|
|
const count = 12;
|
2012-04-12 07:23:07 +00:00
|
|
|
|
|
|
|
if (process.argv[2] === 'child') {
|
2018-06-02 10:34:06 +00:00
|
|
|
const needEnd = [];
|
|
|
|
const id = process.argv[3];
|
|
|
|
|
2020-03-23 20:06:24 +00:00
|
|
|
process.on('message', mustCall((m, socket) => {
|
2018-06-02 10:34:06 +00:00
|
|
|
if (!socket) return;
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2020-03-23 20:06:24 +00:00
|
|
|
debug(`[${id}] got socket ${m}`);
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2019-01-21 00:22:27 +00:00
|
|
|
// Will call .end('end') or .write('write');
|
2018-06-02 10:34:06 +00:00
|
|
|
socket[m](m);
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2018-06-02 10:34:06 +00:00
|
|
|
socket.resume();
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2020-03-23 20:06:24 +00:00
|
|
|
socket.on('data', mustCallAtLeast(() => {
|
|
|
|
debug(`[${id}] socket.data ${m}`);
|
|
|
|
}));
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2020-03-23 20:06:24 +00:00
|
|
|
socket.on('end', mustCall(() => {
|
|
|
|
debug(`[${id}] socket.end ${m}`);
|
|
|
|
}));
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2019-03-22 02:44:26 +00:00
|
|
|
// Store the unfinished socket
|
2018-06-02 10:34:06 +00:00
|
|
|
if (m === 'write') {
|
|
|
|
needEnd.push(socket);
|
|
|
|
}
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2020-03-23 20:06:24 +00:00
|
|
|
socket.on('close', mustCall((had_error) => {
|
|
|
|
debug(`[${id}] socket.close ${had_error} ${m}`);
|
|
|
|
}));
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2020-03-23 20:06:24 +00:00
|
|
|
socket.on('finish', mustCall(() => {
|
|
|
|
debug(`[${id}] socket finished ${m}`);
|
|
|
|
}));
|
2024-02-23 01:12:31 +00:00
|
|
|
}, 4));
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2020-03-23 20:06:24 +00:00
|
|
|
process.on('message', mustCall((m) => {
|
2018-06-02 10:34:06 +00:00
|
|
|
if (m !== 'close') return;
|
2020-03-23 20:06:24 +00:00
|
|
|
debug(`[${id}] got close message`);
|
|
|
|
needEnd.forEach((endMe, i) => {
|
|
|
|
debug(`[${id}] ending ${i}/${needEnd.length}`);
|
2018-06-02 10:34:06 +00:00
|
|
|
endMe.end('end');
|
|
|
|
});
|
2024-02-23 01:12:31 +00:00
|
|
|
}, 4));
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2020-03-23 20:06:24 +00:00
|
|
|
process.on('disconnect', mustCall(() => {
|
|
|
|
debug(`[${id}] process disconnect, ending`);
|
|
|
|
needEnd.forEach((endMe, i) => {
|
|
|
|
debug(`[${id}] ending ${i}/${needEnd.length}`);
|
2018-06-02 10:34:06 +00:00
|
|
|
endMe.end('end');
|
|
|
|
});
|
2020-03-23 20:06:24 +00:00
|
|
|
}));
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2018-06-02 10:34:06 +00:00
|
|
|
} else {
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2018-06-02 10:34:06 +00:00
|
|
|
const child1 = fork(process.argv[1], ['child', '1']);
|
|
|
|
const child2 = fork(process.argv[1], ['child', '2']);
|
|
|
|
const child3 = fork(process.argv[1], ['child', '3']);
|
|
|
|
|
|
|
|
const server = net.createServer();
|
|
|
|
|
|
|
|
let connected = 0;
|
|
|
|
let closed = 0;
|
|
|
|
server.on('connection', function(socket) {
|
|
|
|
switch (connected % 6) {
|
|
|
|
case 0:
|
|
|
|
child1.send('end', socket); break;
|
|
|
|
case 1:
|
|
|
|
child1.send('write', socket); break;
|
|
|
|
case 2:
|
|
|
|
child2.send('end', socket); break;
|
|
|
|
case 3:
|
|
|
|
child2.send('write', socket); break;
|
|
|
|
case 4:
|
|
|
|
child3.send('end', socket); break;
|
|
|
|
case 5:
|
|
|
|
child3.send('write', socket); break;
|
|
|
|
}
|
|
|
|
connected += 1;
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2020-03-23 20:06:24 +00:00
|
|
|
// TODO(@jasnell): This is not actually being called.
|
|
|
|
// It is not clear if it is needed.
|
|
|
|
socket.once('close', () => {
|
|
|
|
debug(`[m] socket closed, total ${++closed}`);
|
2012-04-12 07:23:07 +00:00
|
|
|
});
|
|
|
|
|
2018-06-02 10:34:06 +00:00
|
|
|
if (connected === count) {
|
|
|
|
closeServer();
|
2017-04-28 00:27:05 +00:00
|
|
|
}
|
2018-06-02 10:34:06 +00:00
|
|
|
});
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2018-06-02 10:34:06 +00:00
|
|
|
let disconnected = 0;
|
2020-03-23 20:06:24 +00:00
|
|
|
server.on('listening', mustCall(() => {
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2018-06-02 10:34:06 +00:00
|
|
|
let j = count;
|
|
|
|
while (j--) {
|
2020-03-23 20:06:24 +00:00
|
|
|
const client = net.connect(server.address().port, '127.0.0.1');
|
|
|
|
client.on('error', () => {
|
2018-08-14 06:38:51 +00:00
|
|
|
// This can happen if we kill the subprocess too early.
|
2018-06-02 10:34:06 +00:00
|
|
|
// The client should still get a close event afterwards.
|
2020-03-23 20:06:24 +00:00
|
|
|
// It likely won't so don't wrap in a mustCall.
|
|
|
|
debug('[m] CLIENT: error event');
|
2012-04-12 07:23:07 +00:00
|
|
|
});
|
2020-03-23 20:06:24 +00:00
|
|
|
client.on('close', mustCall(() => {
|
|
|
|
debug('[m] CLIENT: close event');
|
2018-06-02 10:34:06 +00:00
|
|
|
disconnected += 1;
|
2020-03-23 20:06:24 +00:00
|
|
|
}));
|
2018-06-02 10:34:06 +00:00
|
|
|
client.resume();
|
|
|
|
}
|
2020-03-23 20:06:24 +00:00
|
|
|
}));
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2018-06-02 10:34:06 +00:00
|
|
|
let closeEmitted = false;
|
2020-03-23 20:06:24 +00:00
|
|
|
server.on('close', mustCall(function() {
|
2018-06-02 10:34:06 +00:00
|
|
|
closeEmitted = true;
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2024-02-23 01:12:31 +00:00
|
|
|
// Clean up child processes.
|
|
|
|
try {
|
|
|
|
child1.kill();
|
|
|
|
} catch {
|
|
|
|
debug('child process already terminated');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
child2.kill();
|
|
|
|
} catch {
|
|
|
|
debug('child process already terminated');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
child3.kill();
|
|
|
|
} catch {
|
|
|
|
debug('child process already terminated');
|
|
|
|
}
|
2018-06-02 10:34:06 +00:00
|
|
|
}));
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2018-06-02 10:34:06 +00:00
|
|
|
server.listen(0, '127.0.0.1');
|
2012-04-12 07:23:07 +00:00
|
|
|
|
2018-06-02 10:34:06 +00:00
|
|
|
function closeServer() {
|
|
|
|
server.close();
|
|
|
|
|
2020-03-23 20:06:24 +00:00
|
|
|
setTimeout(() => {
|
2018-06-02 10:34:06 +00:00
|
|
|
assert(!closeEmitted);
|
|
|
|
child1.send('close');
|
|
|
|
child2.send('close');
|
|
|
|
child3.disconnect();
|
2020-03-23 20:06:24 +00:00
|
|
|
}, platformTimeout(200));
|
2018-06-02 10:34:06 +00:00
|
|
|
}
|
2012-04-12 07:23:07 +00:00
|
|
|
|
|
|
|
process.on('exit', function() {
|
2018-06-02 10:34:06 +00:00
|
|
|
assert.strictEqual(server._workers.length, 0);
|
|
|
|
assert.strictEqual(disconnected, count);
|
|
|
|
assert.strictEqual(connected, count);
|
2012-04-12 07:23:07 +00:00
|
|
|
});
|
|
|
|
}
|