11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled(); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst { Worker, isMainThread, parentPort, workerData } = 71cb0ef41Sopenharmony_ci require('worker_threads'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciif (isMainThread || workerData !== 'launched by test') { 101cb0ef41Sopenharmony_ci common.skipIfWorker(); 111cb0ef41Sopenharmony_ci} 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst { Session } = require('inspector'); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciconst MAX_DEPTH = 3; 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cilet rootWorker = null; 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciconst runTest = common.mustCall(function() { 201cb0ef41Sopenharmony_ci let reportedWorkersCount = 0; 211cb0ef41Sopenharmony_ci const session = new Session(); 221cb0ef41Sopenharmony_ci session.connect(); 231cb0ef41Sopenharmony_ci session.on('NodeWorker.attachedToWorker', common.mustCall( 241cb0ef41Sopenharmony_ci ({ params: { workerInfo } }) => { 251cb0ef41Sopenharmony_ci console.log(`Worker ${workerInfo.title} was reported`); 261cb0ef41Sopenharmony_ci if (++reportedWorkersCount === MAX_DEPTH) { 271cb0ef41Sopenharmony_ci rootWorker.postMessage({ done: true }); 281cb0ef41Sopenharmony_ci } 291cb0ef41Sopenharmony_ci }, MAX_DEPTH)); 301cb0ef41Sopenharmony_ci session.post('NodeWorker.enable', { waitForDebuggerOnStart: false }); 311cb0ef41Sopenharmony_ci}); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_cifunction processMessage({ child }) { 341cb0ef41Sopenharmony_ci console.log(`Worker ${child} is running`); 351cb0ef41Sopenharmony_ci if (child === MAX_DEPTH) { 361cb0ef41Sopenharmony_ci runTest(); 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci} 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_cifunction workerCallback(message) { 411cb0ef41Sopenharmony_ci parentPort.postMessage(message); 421cb0ef41Sopenharmony_ci} 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_cifunction startWorker(depth, messageCallback) { 451cb0ef41Sopenharmony_ci const worker = new Worker(__filename, { workerData: 'launched by test' }); 461cb0ef41Sopenharmony_ci worker.on('message', messageCallback); 471cb0ef41Sopenharmony_ci worker.postMessage({ depth }); 481cb0ef41Sopenharmony_ci return worker; 491cb0ef41Sopenharmony_ci} 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_cifunction runMainThread() { 521cb0ef41Sopenharmony_ci rootWorker = startWorker(1, processMessage); 531cb0ef41Sopenharmony_ci} 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_cifunction runChildWorkerThread() { 561cb0ef41Sopenharmony_ci let worker = null; 571cb0ef41Sopenharmony_ci parentPort.on('message', ({ child, depth, done }) => { 581cb0ef41Sopenharmony_ci if (done) { 591cb0ef41Sopenharmony_ci if (worker) { 601cb0ef41Sopenharmony_ci worker.postMessage({ done: true }); 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci parentPort.close(); 631cb0ef41Sopenharmony_ci } else if (depth) { 641cb0ef41Sopenharmony_ci parentPort.postMessage({ child: depth }); 651cb0ef41Sopenharmony_ci if (depth < MAX_DEPTH) { 661cb0ef41Sopenharmony_ci worker = startWorker(depth + 1, workerCallback); 671cb0ef41Sopenharmony_ci } 681cb0ef41Sopenharmony_ci } else if (child) { 691cb0ef41Sopenharmony_ci parentPort.postMessage({ child }); 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci }); 721cb0ef41Sopenharmony_ci} 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ciif (isMainThread) { 751cb0ef41Sopenharmony_ci runMainThread(); 761cb0ef41Sopenharmony_ci} else { 771cb0ef41Sopenharmony_ci runChildWorkerThread(); 781cb0ef41Sopenharmony_ci} 79