Skip to content

Commit 4929d12

Browse files
DavidCai1111jasnell
authored andcommitted
test: add internal/socket_list tests
PR-URL: #11989 Reviewed-By: James M Snell <[email protected]>
1 parent 64af398 commit 4929d12

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
const EventEmitter = require('events');
7+
const SocketListReceive = require('internal/socket_list').SocketListReceive;
8+
9+
const key = 'test-key';
10+
11+
// Verify that the message won't be sent when child is not connected.
12+
{
13+
const child = Object.assign(new EventEmitter(), {
14+
connected: false,
15+
send: common.mustNotCall()
16+
});
17+
18+
const list = new SocketListReceive(child, key);
19+
list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_NOTIFY_CLOSE' });
20+
}
21+
22+
// Verify that a "NODE_SOCKET_ALL_CLOSED" message will be sent.
23+
{
24+
const child = Object.assign(new EventEmitter(), {
25+
connected: true,
26+
send: common.mustCall((msg) => {
27+
assert.strictEqual(msg.cmd, 'NODE_SOCKET_ALL_CLOSED');
28+
assert.strictEqual(msg.key, key);
29+
})
30+
});
31+
32+
const list = new SocketListReceive(child, key);
33+
list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_NOTIFY_CLOSE' });
34+
}
35+
36+
// Verify that a "NODE_SOCKET_COUNT" message will be sent.
37+
{
38+
const child = Object.assign(new EventEmitter(), {
39+
connected: true,
40+
send: common.mustCall((msg) => {
41+
assert.strictEqual(msg.cmd, 'NODE_SOCKET_COUNT');
42+
assert.strictEqual(msg.key, key);
43+
assert.strictEqual(msg.count, 0);
44+
})
45+
});
46+
47+
const list = new SocketListReceive(child, key);
48+
list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_GET_COUNT' });
49+
}
50+
51+
// Verify that the connections count is added and an "empty" event
52+
// will be emitted when all sockets in obj were closed.
53+
{
54+
const child = new EventEmitter();
55+
const obj = { socket: new EventEmitter() };
56+
57+
const list = new SocketListReceive(child, key);
58+
assert.strictEqual(list.connections, 0);
59+
60+
list.add(obj);
61+
assert.strictEqual(list.connections, 1);
62+
63+
list.on('empty', common.mustCall((self) => assert.strictEqual(self, list)));
64+
65+
obj.socket.emit('close');
66+
assert.strictEqual(list.connections, 0);
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
const EventEmitter = require('events');
7+
const SocketListSend = require('internal/socket_list').SocketListSend;
8+
9+
const key = 'test-key';
10+
11+
// Verify that an error will be received in callback when child is not
12+
// connected.
13+
{
14+
const child = Object.assign(new EventEmitter(), { connected: false });
15+
assert.strictEqual(child.listenerCount('internalMessage'), 0);
16+
17+
const list = new SocketListSend(child, 'test');
18+
19+
list._request('msg', 'cmd', common.mustCall((err) => {
20+
assert.strictEqual(err.message, 'child closed before reply');
21+
assert.strictEqual(child.listenerCount('internalMessage'), 0);
22+
}));
23+
}
24+
25+
// Verify that the given message will be received in callback.
26+
{
27+
const child = Object.assign(new EventEmitter(), {
28+
connected: true,
29+
send: function(msg) {
30+
process.nextTick(() =>
31+
this.emit('internalMessage', { key, cmd: 'cmd' })
32+
);
33+
}
34+
});
35+
36+
const list = new SocketListSend(child, key);
37+
38+
list._request('msg', 'cmd', common.mustCall((err, msg) => {
39+
assert.strictEqual(err, null);
40+
assert.strictEqual(msg.cmd, 'cmd');
41+
assert.strictEqual(msg.key, key);
42+
assert.strictEqual(child.listenerCount('internalMessage'), 0);
43+
assert.strictEqual(child.listenerCount('disconnect'), 0);
44+
}));
45+
}
46+
47+
// Verify that an error will be received in callback when child was
48+
// disconnected.
49+
{
50+
const child = Object.assign(new EventEmitter(), {
51+
connected: true,
52+
send: function(msg) { process.nextTick(() => this.emit('disconnect')); }
53+
});
54+
55+
const list = new SocketListSend(child, key);
56+
57+
list._request('msg', 'cmd', common.mustCall((err) => {
58+
assert.strictEqual(err.message, 'child closed before reply');
59+
assert.strictEqual(child.listenerCount('internalMessage'), 0);
60+
}));
61+
}
62+
63+
// Verify that a "NODE_SOCKET_ALL_CLOSED" message will be received
64+
// in callback.
65+
{
66+
const child = Object.assign(new EventEmitter(), {
67+
connected: true,
68+
send: function(msg) {
69+
assert.strictEqual(msg.cmd, 'NODE_SOCKET_NOTIFY_CLOSE');
70+
assert.strictEqual(msg.key, key);
71+
process.nextTick(() =>
72+
this.emit('internalMessage', { key, cmd: 'NODE_SOCKET_ALL_CLOSED' })
73+
);
74+
}
75+
});
76+
77+
const list = new SocketListSend(child, key);
78+
79+
list.close(common.mustCall((err, msg) => {
80+
assert.strictEqual(err, null);
81+
assert.strictEqual(msg.cmd, 'NODE_SOCKET_ALL_CLOSED');
82+
assert.strictEqual(msg.key, key);
83+
assert.strictEqual(child.listenerCount('internalMessage'), 0);
84+
assert.strictEqual(child.listenerCount('disconnect'), 0);
85+
}));
86+
}
87+
88+
// Verify that the count of connections will be received in callback.
89+
{
90+
const count = 1;
91+
const child = Object.assign(new EventEmitter(), {
92+
connected: true,
93+
send: function(msg) {
94+
assert.strictEqual(msg.cmd, 'NODE_SOCKET_GET_COUNT');
95+
assert.strictEqual(msg.key, key);
96+
process.nextTick(() =>
97+
this.emit('internalMessage', {
98+
key,
99+
count,
100+
cmd: 'NODE_SOCKET_COUNT'
101+
})
102+
);
103+
}
104+
});
105+
106+
const list = new SocketListSend(child, key);
107+
108+
list.getConnections(common.mustCall((err, msg) => {
109+
assert.strictEqual(err, null);
110+
assert.strictEqual(msg, count);
111+
assert.strictEqual(child.listenerCount('internalMessage'), 0);
112+
assert.strictEqual(child.listenerCount('disconnect'), 0);
113+
}));
114+
}

0 commit comments

Comments
 (0)