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 SocketListSend = require('internal/socket_list').SocketListSend; 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst key = 'test-key'; 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci// Verify that an error will be received in callback when child is not 121cb0ef41Sopenharmony_ci// connected. 131cb0ef41Sopenharmony_ci{ 141cb0ef41Sopenharmony_ci const child = Object.assign(new EventEmitter(), { connected: false }); 151cb0ef41Sopenharmony_ci assert.strictEqual(child.listenerCount('internalMessage'), 0); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci const list = new SocketListSend(child, 'test'); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci list._request('msg', 'cmd', false, common.mustCall((err) => { 201cb0ef41Sopenharmony_ci common.expectsError({ 211cb0ef41Sopenharmony_ci code: 'ERR_CHILD_CLOSED_BEFORE_REPLY', 221cb0ef41Sopenharmony_ci name: 'Error', 231cb0ef41Sopenharmony_ci message: 'Child closed before reply received' 241cb0ef41Sopenharmony_ci })(err); 251cb0ef41Sopenharmony_ci assert.strictEqual(child.listenerCount('internalMessage'), 0); 261cb0ef41Sopenharmony_ci })); 271cb0ef41Sopenharmony_ci} 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci// Verify that the given message will be received in callback. 301cb0ef41Sopenharmony_ci{ 311cb0ef41Sopenharmony_ci const child = Object.assign(new EventEmitter(), { 321cb0ef41Sopenharmony_ci connected: true, 331cb0ef41Sopenharmony_ci _send: function(msg) { 341cb0ef41Sopenharmony_ci process.nextTick(() => 351cb0ef41Sopenharmony_ci this.emit('internalMessage', { key, cmd: 'cmd' }) 361cb0ef41Sopenharmony_ci ); 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci }); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci const list = new SocketListSend(child, key); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci list._request('msg', 'cmd', false, common.mustCall((err, msg) => { 431cb0ef41Sopenharmony_ci assert.strictEqual(err, null); 441cb0ef41Sopenharmony_ci assert.strictEqual(msg.cmd, 'cmd'); 451cb0ef41Sopenharmony_ci assert.strictEqual(msg.key, key); 461cb0ef41Sopenharmony_ci assert.strictEqual(child.listenerCount('internalMessage'), 0); 471cb0ef41Sopenharmony_ci assert.strictEqual(child.listenerCount('disconnect'), 0); 481cb0ef41Sopenharmony_ci })); 491cb0ef41Sopenharmony_ci} 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci// Verify that an error will be received in callback when child was 521cb0ef41Sopenharmony_ci// disconnected. 531cb0ef41Sopenharmony_ci{ 541cb0ef41Sopenharmony_ci const child = Object.assign(new EventEmitter(), { 551cb0ef41Sopenharmony_ci connected: true, 561cb0ef41Sopenharmony_ci _send: function(msg) { process.nextTick(() => this.emit('disconnect')); } 571cb0ef41Sopenharmony_ci }); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci const list = new SocketListSend(child, key); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci list._request('msg', 'cmd', false, common.mustCall((err) => { 621cb0ef41Sopenharmony_ci common.expectsError({ 631cb0ef41Sopenharmony_ci code: 'ERR_CHILD_CLOSED_BEFORE_REPLY', 641cb0ef41Sopenharmony_ci name: 'Error', 651cb0ef41Sopenharmony_ci message: 'Child closed before reply received' 661cb0ef41Sopenharmony_ci })(err); 671cb0ef41Sopenharmony_ci assert.strictEqual(child.listenerCount('internalMessage'), 0); 681cb0ef41Sopenharmony_ci })); 691cb0ef41Sopenharmony_ci} 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci// Verify that a "NODE_SOCKET_ALL_CLOSED" message will be received 721cb0ef41Sopenharmony_ci// in callback. 731cb0ef41Sopenharmony_ci{ 741cb0ef41Sopenharmony_ci const child = Object.assign(new EventEmitter(), { 751cb0ef41Sopenharmony_ci connected: true, 761cb0ef41Sopenharmony_ci _send: function(msg) { 771cb0ef41Sopenharmony_ci assert.strictEqual(msg.cmd, 'NODE_SOCKET_NOTIFY_CLOSE'); 781cb0ef41Sopenharmony_ci assert.strictEqual(msg.key, key); 791cb0ef41Sopenharmony_ci process.nextTick(() => 801cb0ef41Sopenharmony_ci this.emit('internalMessage', { key, cmd: 'NODE_SOCKET_ALL_CLOSED' }) 811cb0ef41Sopenharmony_ci ); 821cb0ef41Sopenharmony_ci } 831cb0ef41Sopenharmony_ci }); 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci const list = new SocketListSend(child, key); 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci list.close(common.mustCall((err, msg) => { 881cb0ef41Sopenharmony_ci assert.strictEqual(err, null); 891cb0ef41Sopenharmony_ci assert.strictEqual(msg.cmd, 'NODE_SOCKET_ALL_CLOSED'); 901cb0ef41Sopenharmony_ci assert.strictEqual(msg.key, key); 911cb0ef41Sopenharmony_ci assert.strictEqual(child.listenerCount('internalMessage'), 0); 921cb0ef41Sopenharmony_ci assert.strictEqual(child.listenerCount('disconnect'), 0); 931cb0ef41Sopenharmony_ci })); 941cb0ef41Sopenharmony_ci} 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci// Verify that the count of connections will be received in callback. 971cb0ef41Sopenharmony_ci{ 981cb0ef41Sopenharmony_ci const count = 1; 991cb0ef41Sopenharmony_ci const child = Object.assign(new EventEmitter(), { 1001cb0ef41Sopenharmony_ci connected: true, 1011cb0ef41Sopenharmony_ci _send: function(msg) { 1021cb0ef41Sopenharmony_ci assert.strictEqual(msg.cmd, 'NODE_SOCKET_GET_COUNT'); 1031cb0ef41Sopenharmony_ci assert.strictEqual(msg.key, key); 1041cb0ef41Sopenharmony_ci process.nextTick(() => 1051cb0ef41Sopenharmony_ci this.emit('internalMessage', { 1061cb0ef41Sopenharmony_ci key, 1071cb0ef41Sopenharmony_ci count, 1081cb0ef41Sopenharmony_ci cmd: 'NODE_SOCKET_COUNT' 1091cb0ef41Sopenharmony_ci }) 1101cb0ef41Sopenharmony_ci ); 1111cb0ef41Sopenharmony_ci } 1121cb0ef41Sopenharmony_ci }); 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci const list = new SocketListSend(child, key); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci list.getConnections(common.mustCall((err, msg) => { 1171cb0ef41Sopenharmony_ci assert.strictEqual(err, null); 1181cb0ef41Sopenharmony_ci assert.strictEqual(msg, count); 1191cb0ef41Sopenharmony_ci assert.strictEqual(child.listenerCount('internalMessage'), 0); 1201cb0ef41Sopenharmony_ci assert.strictEqual(child.listenerCount('disconnect'), 0); 1211cb0ef41Sopenharmony_ci })); 1221cb0ef41Sopenharmony_ci} 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci// Verify that an error will be received in callback when child is 1251cb0ef41Sopenharmony_ci// disconnected after sending a message and before getting the reply. 1261cb0ef41Sopenharmony_ci{ 1271cb0ef41Sopenharmony_ci const count = 1; 1281cb0ef41Sopenharmony_ci const child = Object.assign(new EventEmitter(), { 1291cb0ef41Sopenharmony_ci connected: true, 1301cb0ef41Sopenharmony_ci _send: function() { 1311cb0ef41Sopenharmony_ci process.nextTick(() => { 1321cb0ef41Sopenharmony_ci this.emit('disconnect'); 1331cb0ef41Sopenharmony_ci this.emit('internalMessage', { key, count, cmd: 'NODE_SOCKET_COUNT' }); 1341cb0ef41Sopenharmony_ci }); 1351cb0ef41Sopenharmony_ci } 1361cb0ef41Sopenharmony_ci }); 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci const list = new SocketListSend(child, key); 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci list.getConnections(common.mustCall((err) => { 1411cb0ef41Sopenharmony_ci common.expectsError({ 1421cb0ef41Sopenharmony_ci code: 'ERR_CHILD_CLOSED_BEFORE_REPLY', 1431cb0ef41Sopenharmony_ci name: 'Error', 1441cb0ef41Sopenharmony_ci message: 'Child closed before reply received' 1451cb0ef41Sopenharmony_ci })(err); 1461cb0ef41Sopenharmony_ci assert.strictEqual(child.listenerCount('internalMessage'), 0); 1471cb0ef41Sopenharmony_ci })); 1481cb0ef41Sopenharmony_ci} 149