Skip to content

Commit b319264

Browse files
committed
test: fork-getconnections stricter, less chatty
1 parent 5902bc4 commit b319264

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

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

+24-12
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ if (process.argv[2] === 'child') {
3030
var id = process.argv[3];
3131

3232
process.on('message', function(m, socket) {
33-
if (socket) {
33+
if (m.cmd === 'new') {
34+
assert(socket);
35+
assert(socket instanceof net.Socket, 'should be a net.Socket');
3436
sockets.push(socket);
37+
socket.on('end', function() {
38+
if (!this.closingOnPurpose)
39+
throw new Error('[c] closing by accident! ' + process._errno);
40+
});
3541
}
3642

3743
if (m.cmd === 'close') {
@@ -42,9 +48,15 @@ if (process.argv[2] === 'child') {
4248
sockets[m.id].destroy();
4349
}
4450
});
51+
4552
} else {
4653
var child = fork(process.argv[1], ['child']);
4754

55+
child.on('exit', function(code, signal) {
56+
if (!childKilled)
57+
throw new Error('child died unexpectedly!');
58+
});
59+
4860
var server = net.createServer();
4961
var sockets = [];
5062
var sent = 0;
@@ -55,29 +67,31 @@ if (process.argv[2] === 'child') {
5567

5668
if (sockets.length === count) {
5769
closeSockets(0);
58-
server.close();
5970
}
6071
});
6172

6273
var disconnected = 0;
74+
var clients = [];
6375
server.on('listening', function() {
64-
6576
var j = count, client;
6677
while (j--) {
6778
client = net.connect(common.PORT, '127.0.0.1');
79+
client.id = j;
6880
client.on('close', function() {
69-
console.error('[m] CLIENT: close event');
7081
disconnected += 1;
7182
});
72-
// XXX This resume() should be unnecessary.
73-
// a stream high water mark should be enough to keep
74-
// consuming the input.
75-
client.resume();
83+
clients.push(client);
7684
}
7785
});
7886

87+
var childKilled = false;
7988
function closeSockets(i) {
80-
if (i === count) return;
89+
if (i === count) {
90+
childKilled = true;
91+
server.close();
92+
child.kill();
93+
return;
94+
}
8195

8296
sent++;
8397
child.send({ id: i, cmd: 'close' });
@@ -91,10 +105,7 @@ if (process.argv[2] === 'child') {
91105

92106
var closeEmitted = false;
93107
server.on('close', function() {
94-
console.error('[m] server close');
95108
closeEmitted = true;
96-
97-
child.kill();
98109
});
99110

100111
server.listen(common.PORT, '127.0.0.1');
@@ -103,5 +114,6 @@ if (process.argv[2] === 'child') {
103114
assert.equal(sent, count);
104115
assert.equal(disconnected, count);
105116
assert.ok(closeEmitted);
117+
console.log('ok');
106118
});
107119
}

0 commit comments

Comments
 (0)