diff options
author | DavidCai <davidcai1993@yahoo.com> | 2017-03-22 22:08:02 +0800 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-03-24 08:46:05 -0700 |
commit | 4929d12e999aaab08b0ed90e8a6080e139ca62d1 (patch) | |
tree | cddf253818f3b3639130007958917715fe2ffd67 /test/parallel/test-internal-socket-list-send.js | |
parent | 64af398c263105653dd0ba255a078c305b28e1e9 (diff) | |
download | node-new-4929d12e999aaab08b0ed90e8a6080e139ca62d1.tar.gz |
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-send.js')
-rw-r--r-- | test/parallel/test-internal-socket-list-send.js | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/test/parallel/test-internal-socket-list-send.js b/test/parallel/test-internal-socket-list-send.js new file mode 100644 index 0000000000..a5020a431c --- /dev/null +++ b/test/parallel/test-internal-socket-list-send.js @@ -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); + })); +} |