1// Flags: --expose-internals 2'use strict'; 3 4const common = require('../common'); 5const assert = require('assert'); 6const EventEmitter = require('events'); 7const SocketListReceive = require('internal/socket_list').SocketListReceive; 8 9const 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 list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_GET_COUNT' }); 21} 22 23// Verify that a "NODE_SOCKET_ALL_CLOSED" message will be sent. 24{ 25 const child = Object.assign(new EventEmitter(), { 26 connected: true, 27 _send: common.mustCall((msg) => { 28 assert.strictEqual(msg.cmd, 'NODE_SOCKET_ALL_CLOSED'); 29 assert.strictEqual(msg.key, key); 30 }) 31 }); 32 33 const list = new SocketListReceive(child, key); 34 list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_NOTIFY_CLOSE' }); 35} 36 37// Verify that a "NODE_SOCKET_COUNT" message will be sent. 38{ 39 const child = Object.assign(new EventEmitter(), { 40 connected: true, 41 _send: common.mustCall((msg) => { 42 assert.strictEqual(msg.cmd, 'NODE_SOCKET_COUNT'); 43 assert.strictEqual(msg.key, key); 44 assert.strictEqual(msg.count, 0); 45 }) 46 }); 47 48 const list = new SocketListReceive(child, key); 49 list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_GET_COUNT' }); 50} 51 52// Verify that the connections count is added and an "empty" event 53// will be emitted when all sockets in obj were closed. 54{ 55 const child = new EventEmitter(); 56 const obj = { socket: new EventEmitter() }; 57 58 const list = new SocketListReceive(child, key); 59 assert.strictEqual(list.connections, 0); 60 61 list.add(obj); 62 assert.strictEqual(list.connections, 1); 63 64 list.on('empty', common.mustCall((self) => assert.strictEqual(self, list))); 65 66 obj.socket.emit('close'); 67 assert.strictEqual(list.connections, 0); 68} 69