11cb0ef41Sopenharmony_ci// Flags: --expose-internals 21cb0ef41Sopenharmony_ci'use strict'; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst common = require('../common'); 51cb0ef41Sopenharmony_ciconst assert = require('assert'); 61cb0ef41Sopenharmony_ciconst EventEmitter = require('events'); 71cb0ef41Sopenharmony_ciconst SocketListReceive = require('internal/socket_list').SocketListReceive; 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst key = 'test-key'; 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci// Verify that the message won't be sent when child is not connected. 121cb0ef41Sopenharmony_ci{ 131cb0ef41Sopenharmony_ci const child = Object.assign(new EventEmitter(), { 141cb0ef41Sopenharmony_ci connected: false, 151cb0ef41Sopenharmony_ci _send: common.mustNotCall() 161cb0ef41Sopenharmony_ci }); 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci const list = new SocketListReceive(child, key); 191cb0ef41Sopenharmony_ci list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_NOTIFY_CLOSE' }); 201cb0ef41Sopenharmony_ci list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_GET_COUNT' }); 211cb0ef41Sopenharmony_ci} 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci// Verify that a "NODE_SOCKET_ALL_CLOSED" message will be sent. 241cb0ef41Sopenharmony_ci{ 251cb0ef41Sopenharmony_ci const child = Object.assign(new EventEmitter(), { 261cb0ef41Sopenharmony_ci connected: true, 271cb0ef41Sopenharmony_ci _send: common.mustCall((msg) => { 281cb0ef41Sopenharmony_ci assert.strictEqual(msg.cmd, 'NODE_SOCKET_ALL_CLOSED'); 291cb0ef41Sopenharmony_ci assert.strictEqual(msg.key, key); 301cb0ef41Sopenharmony_ci }) 311cb0ef41Sopenharmony_ci }); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci const list = new SocketListReceive(child, key); 341cb0ef41Sopenharmony_ci list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_NOTIFY_CLOSE' }); 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci// Verify that a "NODE_SOCKET_COUNT" message will be sent. 381cb0ef41Sopenharmony_ci{ 391cb0ef41Sopenharmony_ci const child = Object.assign(new EventEmitter(), { 401cb0ef41Sopenharmony_ci connected: true, 411cb0ef41Sopenharmony_ci _send: common.mustCall((msg) => { 421cb0ef41Sopenharmony_ci assert.strictEqual(msg.cmd, 'NODE_SOCKET_COUNT'); 431cb0ef41Sopenharmony_ci assert.strictEqual(msg.key, key); 441cb0ef41Sopenharmony_ci assert.strictEqual(msg.count, 0); 451cb0ef41Sopenharmony_ci }) 461cb0ef41Sopenharmony_ci }); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci const list = new SocketListReceive(child, key); 491cb0ef41Sopenharmony_ci list.child.emit('internalMessage', { key, cmd: 'NODE_SOCKET_GET_COUNT' }); 501cb0ef41Sopenharmony_ci} 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci// Verify that the connections count is added and an "empty" event 531cb0ef41Sopenharmony_ci// will be emitted when all sockets in obj were closed. 541cb0ef41Sopenharmony_ci{ 551cb0ef41Sopenharmony_ci const child = new EventEmitter(); 561cb0ef41Sopenharmony_ci const obj = { socket: new EventEmitter() }; 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci const list = new SocketListReceive(child, key); 591cb0ef41Sopenharmony_ci assert.strictEqual(list.connections, 0); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci list.add(obj); 621cb0ef41Sopenharmony_ci assert.strictEqual(list.connections, 1); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci list.on('empty', common.mustCall((self) => assert.strictEqual(self, list))); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci obj.socket.emit('close'); 671cb0ef41Sopenharmony_ci assert.strictEqual(list.connections, 0); 681cb0ef41Sopenharmony_ci} 69