Skip to content

Instantly share code, notes, and snippets.

@indutny

indutny/regr.js Secret

Created January 24, 2013 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indutny/074b0d7767dea64c3d0f to your computer and use it in GitHub Desktop.
Save indutny/074b0d7767dea64c3d0f to your computer and use it in GitHub Desktop.
var assert = require('assert');
var child_process = require('child_process');
var net = require('net');
var path = require('path');
var fs = require('fs');
var common = require('./test/common');
var count = 10,
iterations = 10000;
if (process.argv[2] === 'child') {
var sockets = [];
process.on('message', function(msg, socket) {
if (msg === 'socket') {
assert(socket);
socket.once('close', function() {
throw new Error('Socket closed on iteration: ' + iterations);
});
sockets.push(socket);
process.send('got');
} else if (msg === 'end') {
// Cleanup
var list = sockets.slice();
sockets = [];
for (var i = 0; i < sockets.length; i++) {
list[i].removeAllListeners('close');
list[i].once('close', function() {
process.send('closed');
});
list[i].destroy();
}
}
});
return;
}
var child = child_process.fork(__filename, ['child']);
var server = net.createServer(function(c) {
child.on('message', function ongot(msg) {
if (msg !== 'got') return;
left--;
child.removeListener('message', ongot);
if (left === 0) return end();
send();
});
child.send('socket', c);
}).listen(common.PORT);
var left = count;
function send() {
var client = net.connect(common.PORT, '127.0.0.1');
};
function end() {
var toclose = count;
child.on('message', function onclosed(msg) {
assert.equal(msg, 'closed');
if (--toclose === 0) {
child.removeListener('message', onclosed);
if (--iterations === 0) {
child.removeAllListeners('exit');
child.kill();
server.close();
} else {
left = count;
send();
}
}
});
child.send('end');
};
child.once('exit', function() {
throw new Error('Child died on iteration: ' + iterations);
});
send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment