-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add internal/socket_list tests
PR-URL: #11989 Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
64af398
commit 4929d12
Showing
2 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const EventEmitter = require('events'); | ||
const SocketListReceive = require('internal/socket_list').SocketListReceive; | ||
|
||
const key = 'test-key'; | ||
|
||
// Verify that the message won't be sent when child is not connected. | ||
{ | ||
const child = Object.assign(new EventEmitter(), { | ||
connected: false, | ||
send: common.mustNotCall() | ||
}); | ||
|
||
const list = new SocketListReceive(child, key); | ||
list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_NOTIFY_CLOSE' }); | ||
} | ||
|
||
// Verify that a "NODE_SOCKET_ALL_CLOSED" message will be sent. | ||
{ | ||
const child = Object.assign(new EventEmitter(), { | ||
connected: true, | ||
send: common.mustCall((msg) => { | ||
assert.strictEqual(msg.cmd, 'NODE_SOCKET_ALL_CLOSED'); | ||
assert.strictEqual(msg.key, key); | ||
}) | ||
}); | ||
|
||
const list = new SocketListReceive(child, key); | ||
list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_NOTIFY_CLOSE' }); | ||
} | ||
|
||
// Verify that a "NODE_SOCKET_COUNT" message will be sent. | ||
{ | ||
const child = Object.assign(new EventEmitter(), { | ||
connected: true, | ||
send: common.mustCall((msg) => { | ||
assert.strictEqual(msg.cmd, 'NODE_SOCKET_COUNT'); | ||
assert.strictEqual(msg.key, key); | ||
assert.strictEqual(msg.count, 0); | ||
}) | ||
}); | ||
|
||
const list = new SocketListReceive(child, key); | ||
list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_GET_COUNT' }); | ||
} | ||
|
||
// Verify that the connections count is added and an "empty" event | ||
// will be emitted when all sockets in obj were closed. | ||
{ | ||
const child = new EventEmitter(); | ||
const obj = { socket: new EventEmitter() }; | ||
|
||
const list = new SocketListReceive(child, key); | ||
assert.strictEqual(list.connections, 0); | ||
|
||
list.add(obj); | ||
assert.strictEqual(list.connections, 1); | ||
|
||
list.on('empty', common.mustCall((self) => assert.strictEqual(self, list))); | ||
|
||
obj.socket.emit('close'); | ||
assert.strictEqual(list.connections, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const EventEmitter = require('events'); | ||
const SocketListSend = require('internal/socket_list').SocketListSend; | ||
|
||
const key = 'test-key'; | ||
|
||
// Verify that an error will be received in callback when child is not | ||
// connected. | ||
{ | ||
const child = Object.assign(new EventEmitter(), { connected: false }); | ||
assert.strictEqual(child.listenerCount('internalMessage'), 0); | ||
|
||
const list = new SocketListSend(child, 'test'); | ||
|
||
list._request('msg', 'cmd', common.mustCall((err) => { | ||
assert.strictEqual(err.message, 'child closed before reply'); | ||
assert.strictEqual(child.listenerCount('internalMessage'), 0); | ||
})); | ||
} | ||
|
||
// Verify that the given message will be received in callback. | ||
{ | ||
const child = Object.assign(new EventEmitter(), { | ||
connected: true, | ||
send: function(msg) { | ||
process.nextTick(() => | ||
this.emit('internalMessage', { key, cmd: 'cmd' }) | ||
); | ||
} | ||
}); | ||
|
||
const list = new SocketListSend(child, key); | ||
|
||
list._request('msg', 'cmd', common.mustCall((err, msg) => { | ||
assert.strictEqual(err, null); | ||
assert.strictEqual(msg.cmd, 'cmd'); | ||
assert.strictEqual(msg.key, key); | ||
assert.strictEqual(child.listenerCount('internalMessage'), 0); | ||
assert.strictEqual(child.listenerCount('disconnect'), 0); | ||
})); | ||
} | ||
|
||
// Verify that an error will be received in callback when child was | ||
// disconnected. | ||
{ | ||
const child = Object.assign(new EventEmitter(), { | ||
connected: true, | ||
send: function(msg) { process.nextTick(() => this.emit('disconnect')); } | ||
}); | ||
|
||
const list = new SocketListSend(child, key); | ||
|
||
list._request('msg', 'cmd', common.mustCall((err) => { | ||
assert.strictEqual(err.message, 'child closed before reply'); | ||
assert.strictEqual(child.listenerCount('internalMessage'), 0); | ||
})); | ||
} | ||
|
||
// Verify that a "NODE_SOCKET_ALL_CLOSED" message will be received | ||
// in callback. | ||
{ | ||
const child = Object.assign(new EventEmitter(), { | ||
connected: true, | ||
send: function(msg) { | ||
assert.strictEqual(msg.cmd, 'NODE_SOCKET_NOTIFY_CLOSE'); | ||
assert.strictEqual(msg.key, key); | ||
process.nextTick(() => | ||
this.emit('internalMessage', { key, cmd: 'NODE_SOCKET_ALL_CLOSED' }) | ||
); | ||
} | ||
}); | ||
|
||
const list = new SocketListSend(child, key); | ||
|
||
list.close(common.mustCall((err, msg) => { | ||
assert.strictEqual(err, null); | ||
assert.strictEqual(msg.cmd, 'NODE_SOCKET_ALL_CLOSED'); | ||
assert.strictEqual(msg.key, key); | ||
assert.strictEqual(child.listenerCount('internalMessage'), 0); | ||
assert.strictEqual(child.listenerCount('disconnect'), 0); | ||
})); | ||
} | ||
|
||
// Verify that the count of connections will be received in callback. | ||
{ | ||
const count = 1; | ||
const child = Object.assign(new EventEmitter(), { | ||
connected: true, | ||
send: function(msg) { | ||
assert.strictEqual(msg.cmd, 'NODE_SOCKET_GET_COUNT'); | ||
assert.strictEqual(msg.key, key); | ||
process.nextTick(() => | ||
this.emit('internalMessage', { | ||
key, | ||
count, | ||
cmd: 'NODE_SOCKET_COUNT' | ||
}) | ||
); | ||
} | ||
}); | ||
|
||
const list = new SocketListSend(child, key); | ||
|
||
list.getConnections(common.mustCall((err, msg) => { | ||
assert.strictEqual(err, null); | ||
assert.strictEqual(msg, count); | ||
assert.strictEqual(child.listenerCount('internalMessage'), 0); | ||
assert.strictEqual(child.listenerCount('disconnect'), 0); | ||
})); | ||
} |