11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
61cb0ef41Sopenharmony_cicommon.skipIfWorker();
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst { Worker } = require('worker_threads');
101cb0ef41Sopenharmony_ciconst { Session } = require('inspector');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst session = new Session();
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cilet done = false;
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cifunction onAttachToWorker({ params: { sessionId } }) {
171cb0ef41Sopenharmony_ci  let id = 1;
181cb0ef41Sopenharmony_ci  function postToWorkerInspector(method, params) {
191cb0ef41Sopenharmony_ci    session.post('NodeWorker.sendMessageToWorker', {
201cb0ef41Sopenharmony_ci      sessionId,
211cb0ef41Sopenharmony_ci      message: JSON.stringify({ id: id++, method, params })
221cb0ef41Sopenharmony_ci    }, () => console.log(`Message ${method} received the response`));
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  // Wait for the notification
261cb0ef41Sopenharmony_ci  function onMessageReceived({ params: { message } }) {
271cb0ef41Sopenharmony_ci    if (!message ||
281cb0ef41Sopenharmony_ci      JSON.parse(message).method !== 'NodeRuntime.waitingForDisconnect') {
291cb0ef41Sopenharmony_ci      session.once('NodeWorker.receivedMessageFromWorker', onMessageReceived);
301cb0ef41Sopenharmony_ci      return;
311cb0ef41Sopenharmony_ci    }
321cb0ef41Sopenharmony_ci    // Force a call to node::inspector::Agent::ToggleAsyncHook by changing the
331cb0ef41Sopenharmony_ci    // async call stack depth
341cb0ef41Sopenharmony_ci    postToWorkerInspector('Debugger.setAsyncCallStackDepth', { maxDepth: 1 });
351cb0ef41Sopenharmony_ci    // This is were the original crash happened
361cb0ef41Sopenharmony_ci    session.post('NodeWorker.detach', { sessionId }, () => {
371cb0ef41Sopenharmony_ci      done = true;
381cb0ef41Sopenharmony_ci    });
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  onMessageReceived({ params: { message: null } });
421cb0ef41Sopenharmony_ci  // Enable the debugger, otherwise setAsyncCallStackDepth does nothing
431cb0ef41Sopenharmony_ci  postToWorkerInspector('Debugger.enable');
441cb0ef41Sopenharmony_ci  // Start waiting for disconnect notification
451cb0ef41Sopenharmony_ci  postToWorkerInspector('NodeRuntime.notifyWhenWaitingForDisconnect',
461cb0ef41Sopenharmony_ci                        { enabled: true });
471cb0ef41Sopenharmony_ci  // start worker
481cb0ef41Sopenharmony_ci  postToWorkerInspector('Runtime.runIfWaitingForDebugger');
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_cisession.connect();
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cisession.on('NodeWorker.attachedToWorker', common.mustCall(onAttachToWorker));
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cisession.post('NodeWorker.enable', { waitForDebuggerOnStart: true }, () => {
561cb0ef41Sopenharmony_ci  new Worker('console.log("Worker is done")', { eval: true })
571cb0ef41Sopenharmony_ci    .once('exit', () => {
581cb0ef41Sopenharmony_ci      setTimeout(() => {
591cb0ef41Sopenharmony_ci        assert.strictEqual(done, true);
601cb0ef41Sopenharmony_ci        console.log('Test is done');
611cb0ef41Sopenharmony_ci      }, 0);
621cb0ef41Sopenharmony_ci    });
631cb0ef41Sopenharmony_ci});
64