Browse Source

test: fork-getconnections stricter, less chatty

v0.10.3-release
isaacs 12 years ago
parent
commit
b319264d84
  1. 36
      test/simple/test-child-process-fork-getconnections.js

36
test/simple/test-child-process-fork-getconnections.js

@ -30,8 +30,14 @@ if (process.argv[2] === 'child') {
var id = process.argv[3]; var id = process.argv[3];
process.on('message', function(m, socket) { process.on('message', function(m, socket) {
if (socket) { if (m.cmd === 'new') {
assert(socket);
assert(socket instanceof net.Socket, 'should be a net.Socket');
sockets.push(socket); sockets.push(socket);
socket.on('end', function() {
if (!this.closingOnPurpose)
throw new Error('[c] closing by accident! ' + process._errno);
});
} }
if (m.cmd === 'close') { if (m.cmd === 'close') {
@ -42,9 +48,15 @@ if (process.argv[2] === 'child') {
sockets[m.id].destroy(); sockets[m.id].destroy();
} }
}); });
} else { } else {
var child = fork(process.argv[1], ['child']); var child = fork(process.argv[1], ['child']);
child.on('exit', function(code, signal) {
if (!childKilled)
throw new Error('child died unexpectedly!');
});
var server = net.createServer(); var server = net.createServer();
var sockets = []; var sockets = [];
var sent = 0; var sent = 0;
@ -55,29 +67,31 @@ if (process.argv[2] === 'child') {
if (sockets.length === count) { if (sockets.length === count) {
closeSockets(0); closeSockets(0);
server.close();
} }
}); });
var disconnected = 0; var disconnected = 0;
var clients = [];
server.on('listening', function() { server.on('listening', function() {
var j = count, client; var j = count, client;
while (j--) { while (j--) {
client = net.connect(common.PORT, '127.0.0.1'); client = net.connect(common.PORT, '127.0.0.1');
client.id = j;
client.on('close', function() { client.on('close', function() {
console.error('[m] CLIENT: close event');
disconnected += 1; disconnected += 1;
}); });
// XXX This resume() should be unnecessary. clients.push(client);
// a stream high water mark should be enough to keep
// consuming the input.
client.resume();
} }
}); });
var childKilled = false;
function closeSockets(i) { function closeSockets(i) {
if (i === count) return; if (i === count) {
childKilled = true;
server.close();
child.kill();
return;
}
sent++; sent++;
child.send({ id: i, cmd: 'close' }); child.send({ id: i, cmd: 'close' });
@ -91,10 +105,7 @@ if (process.argv[2] === 'child') {
var closeEmitted = false; var closeEmitted = false;
server.on('close', function() { server.on('close', function() {
console.error('[m] server close');
closeEmitted = true; closeEmitted = true;
child.kill();
}); });
server.listen(common.PORT, '127.0.0.1'); server.listen(common.PORT, '127.0.0.1');
@ -103,5 +114,6 @@ if (process.argv[2] === 'child') {
assert.equal(sent, count); assert.equal(sent, count);
assert.equal(disconnected, count); assert.equal(disconnected, count);
assert.ok(closeEmitted); assert.ok(closeEmitted);
console.log('ok');
}); });
} }

Loading…
Cancel
Save