summaryrefslogtreecommitdiff
path: root/test/parallel/test-internal-socket-list-receive.js
diff options
context:
space:
mode:
authorDavidCai <davidcai1993@yahoo.com>2017-03-22 22:08:02 +0800
committerJames M Snell <jasnell@gmail.com>2017-03-24 08:46:05 -0700
commit4929d12e999aaab08b0ed90e8a6080e139ca62d1 (patch)
treecddf253818f3b3639130007958917715fe2ffd67 /test/parallel/test-internal-socket-list-receive.js
parent64af398c263105653dd0ba255a078c305b28e1e9 (diff)
downloadandroid-node-v8-4929d12e999aaab08b0ed90e8a6080e139ca62d1.tar.gz
android-node-v8-4929d12e999aaab08b0ed90e8a6080e139ca62d1.tar.bz2
android-node-v8-4929d12e999aaab08b0ed90e8a6080e139ca62d1.zip
test: add internal/socket_list tests
PR-URL: https://github.com/nodejs/node/pull/11989 Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-internal-socket-list-receive.js')
-rw-r--r--test/parallel/test-internal-socket-list-receive.js67
1 files changed, 67 insertions, 0 deletions
diff --git a/test/parallel/test-internal-socket-list-receive.js b/test/parallel/test-internal-socket-list-receive.js
new file mode 100644
index 0000000000..5315adbfd4
--- /dev/null
+++ b/test/parallel/test-internal-socket-list-receive.js
@@ -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);
+}