11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst domain = require('domain'); 61cb0ef41Sopenharmony_ciconst EventEmitter = require('events'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci// Make sure that the domains stack and the active domain is setup properly when 91cb0ef41Sopenharmony_ci// a domain's error handler is called due to an error event being emitted. 101cb0ef41Sopenharmony_ci// More specifically, we want to test that: 111cb0ef41Sopenharmony_ci// - the active domain in the domain's error handler is//not* that domain,//but* 121cb0ef41Sopenharmony_ci// the active domain is a any direct parent domain at the time the error was 131cb0ef41Sopenharmony_ci// emitted. 141cb0ef41Sopenharmony_ci// - the domains stack in the domain's error handler does//not* include that 151cb0ef41Sopenharmony_ci// domain, *but* it includes all parents of that domain when the error was 161cb0ef41Sopenharmony_ci// emitted. 171cb0ef41Sopenharmony_ciconst d1 = domain.create(); 181cb0ef41Sopenharmony_ciconst d2 = domain.create(); 191cb0ef41Sopenharmony_ciconst d3 = domain.create(); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_cifunction checkExpectedDomains(err) { 221cb0ef41Sopenharmony_ci // First, check that domains stack and active domain is as expected when the 231cb0ef41Sopenharmony_ci // event handler is called synchronously via ee.emit('error'). 241cb0ef41Sopenharmony_ci if (domain._stack.length !== err.expectedStackLength) { 251cb0ef41Sopenharmony_ci console.error('expected domains stack length of %d, but instead is %d', 261cb0ef41Sopenharmony_ci err.expectedStackLength, domain._stack.length); 271cb0ef41Sopenharmony_ci process.exit(1); 281cb0ef41Sopenharmony_ci } 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci if (process.domain !== err.expectedActiveDomain) { 311cb0ef41Sopenharmony_ci console.error('expected active domain to be %j, but instead is %j', 321cb0ef41Sopenharmony_ci err.expectedActiveDomain, process.domain); 331cb0ef41Sopenharmony_ci process.exit(1); 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci // Then make sure that the domains stack and active domain is setup as 371cb0ef41Sopenharmony_ci // expected when executing a callback scheduled via nextTick from the error 381cb0ef41Sopenharmony_ci // handler. 391cb0ef41Sopenharmony_ci process.nextTick(() => { 401cb0ef41Sopenharmony_ci const expectedStackLengthInNextTickCb = 411cb0ef41Sopenharmony_ci err.expectedStackLength > 0 ? 1 : 0; 421cb0ef41Sopenharmony_ci if (domain._stack.length !== expectedStackLengthInNextTickCb) { 431cb0ef41Sopenharmony_ci console.error('expected stack length in nextTick cb to be %d, ' + 441cb0ef41Sopenharmony_ci 'but instead is %d', expectedStackLengthInNextTickCb, 451cb0ef41Sopenharmony_ci domain._stack.length); 461cb0ef41Sopenharmony_ci process.exit(1); 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci const expectedActiveDomainInNextTickCb = 501cb0ef41Sopenharmony_ci expectedStackLengthInNextTickCb === 0 ? undefined : 511cb0ef41Sopenharmony_ci err.expectedActiveDomain; 521cb0ef41Sopenharmony_ci if (process.domain !== expectedActiveDomainInNextTickCb) { 531cb0ef41Sopenharmony_ci console.error('expected active domain in nextTick cb to be %j, ' + 541cb0ef41Sopenharmony_ci 'but instead is %j', expectedActiveDomainInNextTickCb, 551cb0ef41Sopenharmony_ci process.domain); 561cb0ef41Sopenharmony_ci process.exit(1); 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci }); 591cb0ef41Sopenharmony_ci} 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_cid1.on('error', common.mustCall((err) => { 621cb0ef41Sopenharmony_ci checkExpectedDomains(err); 631cb0ef41Sopenharmony_ci}, 2)); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_cid2.on('error', common.mustCall((err) => { 661cb0ef41Sopenharmony_ci checkExpectedDomains(err); 671cb0ef41Sopenharmony_ci}, 2)); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_cid3.on('error', common.mustCall((err) => { 701cb0ef41Sopenharmony_ci checkExpectedDomains(err); 711cb0ef41Sopenharmony_ci}, 1)); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_cid1.run(() => { 741cb0ef41Sopenharmony_ci const ee = new EventEmitter(); 751cb0ef41Sopenharmony_ci assert.strictEqual(process.domain, d1); 761cb0ef41Sopenharmony_ci assert.strictEqual(domain._stack.length, 1); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci const err = new Error('oops'); 791cb0ef41Sopenharmony_ci err.expectedStackLength = 0; 801cb0ef41Sopenharmony_ci err.expectedActiveDomain = null; 811cb0ef41Sopenharmony_ci ee.emit('error', err); 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci assert.strictEqual(process.domain, d1); 841cb0ef41Sopenharmony_ci assert.strictEqual(domain._stack.length, 1); 851cb0ef41Sopenharmony_ci}); 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_cid1.run(() => { 881cb0ef41Sopenharmony_ci d1.run(() => { 891cb0ef41Sopenharmony_ci const ee = new EventEmitter(); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci assert.strictEqual(process.domain, d1); 921cb0ef41Sopenharmony_ci assert.strictEqual(domain._stack.length, 2); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci const err = new Error('oops'); 951cb0ef41Sopenharmony_ci err.expectedStackLength = 0; 961cb0ef41Sopenharmony_ci err.expectedActiveDomain = null; 971cb0ef41Sopenharmony_ci ee.emit('error', err); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci assert.strictEqual(process.domain, d1); 1001cb0ef41Sopenharmony_ci assert.strictEqual(domain._stack.length, 2); 1011cb0ef41Sopenharmony_ci }); 1021cb0ef41Sopenharmony_ci}); 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_cid1.run(() => { 1051cb0ef41Sopenharmony_ci d2.run(() => { 1061cb0ef41Sopenharmony_ci const ee = new EventEmitter(); 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci assert.strictEqual(process.domain, d2); 1091cb0ef41Sopenharmony_ci assert.strictEqual(domain._stack.length, 2); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci const err = new Error('oops'); 1121cb0ef41Sopenharmony_ci err.expectedStackLength = 1; 1131cb0ef41Sopenharmony_ci err.expectedActiveDomain = d1; 1141cb0ef41Sopenharmony_ci ee.emit('error', err); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci assert.strictEqual(process.domain, d2); 1171cb0ef41Sopenharmony_ci assert.strictEqual(domain._stack.length, 2); 1181cb0ef41Sopenharmony_ci }); 1191cb0ef41Sopenharmony_ci}); 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_cid1.run(() => { 1221cb0ef41Sopenharmony_ci d2.run(() => { 1231cb0ef41Sopenharmony_ci d2.run(() => { 1241cb0ef41Sopenharmony_ci const ee = new EventEmitter(); 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci assert.strictEqual(process.domain, d2); 1271cb0ef41Sopenharmony_ci assert.strictEqual(domain._stack.length, 3); 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci const err = new Error('oops'); 1301cb0ef41Sopenharmony_ci err.expectedStackLength = 1; 1311cb0ef41Sopenharmony_ci err.expectedActiveDomain = d1; 1321cb0ef41Sopenharmony_ci ee.emit('error', err); 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci assert.strictEqual(process.domain, d2); 1351cb0ef41Sopenharmony_ci assert.strictEqual(domain._stack.length, 3); 1361cb0ef41Sopenharmony_ci }); 1371cb0ef41Sopenharmony_ci }); 1381cb0ef41Sopenharmony_ci}); 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_cid3.run(() => { 1411cb0ef41Sopenharmony_ci d1.run(() => { 1421cb0ef41Sopenharmony_ci d3.run(() => { 1431cb0ef41Sopenharmony_ci d3.run(() => { 1441cb0ef41Sopenharmony_ci const ee = new EventEmitter(); 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci assert.strictEqual(process.domain, d3); 1471cb0ef41Sopenharmony_ci assert.strictEqual(domain._stack.length, 4); 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci const err = new Error('oops'); 1501cb0ef41Sopenharmony_ci err.expectedStackLength = 2; 1511cb0ef41Sopenharmony_ci err.expectedActiveDomain = d1; 1521cb0ef41Sopenharmony_ci ee.emit('error', err); 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci assert.strictEqual(process.domain, d3); 1551cb0ef41Sopenharmony_ci assert.strictEqual(domain._stack.length, 4); 1561cb0ef41Sopenharmony_ci }); 1571cb0ef41Sopenharmony_ci }); 1581cb0ef41Sopenharmony_ci }); 1591cb0ef41Sopenharmony_ci}); 160