summaryrefslogtreecommitdiff
path: root/test/parallel/test-inspector-workers-flat-list.js
diff options
context:
space:
mode:
authorEugene Ostroukhov <eostroukhov@gmail.com>2019-07-26 16:38:15 -0700
committerRich Trott <rtrott@gmail.com>2019-07-30 21:17:27 -0700
commit7435dc8b2ba2bed4d6a8c1b6474f21c7219b3238 (patch)
treecc4e2a2a067721e7744da872e72914f46ab3277d /test/parallel/test-inspector-workers-flat-list.js
parentc3b2111423c73769680734e1891b43cb3eb9cd4c (diff)
downloadandroid-node-v8-7435dc8b2ba2bed4d6a8c1b6474f21c7219b3238.tar.gz
android-node-v8-7435dc8b2ba2bed4d6a8c1b6474f21c7219b3238.tar.bz2
android-node-v8-7435dc8b2ba2bed4d6a8c1b6474f21c7219b3238.zip
inspector: report all workers
Main thread (the one that WS endpoint connects to) should be able to report all workers. PR-URL: https://github.com/nodejs/node/pull/28872 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'test/parallel/test-inspector-workers-flat-list.js')
-rw-r--r--test/parallel/test-inspector-workers-flat-list.js78
1 files changed, 78 insertions, 0 deletions
diff --git a/test/parallel/test-inspector-workers-flat-list.js b/test/parallel/test-inspector-workers-flat-list.js
new file mode 100644
index 0000000000..9f6495d10f
--- /dev/null
+++ b/test/parallel/test-inspector-workers-flat-list.js
@@ -0,0 +1,78 @@
+'use strict';
+const common = require('../common');
+
+common.skipIfInspectorDisabled();
+
+const { Worker, isMainThread, parentPort, workerData } =
+ require('worker_threads');
+
+if (isMainThread || workerData !== 'launched by test') {
+ common.skipIfWorker();
+}
+
+const { Session } = require('inspector');
+
+const MAX_DEPTH = 3;
+
+let rootWorker = null;
+
+const runTest = common.mustCall(function() {
+ let reportedWorkersCount = 0;
+ const session = new Session();
+ session.connect();
+ session.on('NodeWorker.attachedToWorker', common.mustCall(
+ ({ params: { workerInfo } }) => {
+ console.log(`Worker ${workerInfo.title} was reported`);
+ if (++reportedWorkersCount === MAX_DEPTH) {
+ rootWorker.postMessage({ done: true });
+ }
+ }, MAX_DEPTH));
+ session.post('NodeWorker.enable', { waitForDebuggerOnStart: false });
+});
+
+function processMessage({ child }) {
+ console.log(`Worker ${child} is running`);
+ if (child === MAX_DEPTH) {
+ runTest();
+ }
+}
+
+function workerCallback(message) {
+ parentPort.postMessage(message);
+}
+
+function startWorker(depth, messageCallback) {
+ const worker = new Worker(__filename, { workerData: 'launched by test' });
+ worker.on('message', messageCallback);
+ worker.postMessage({ depth });
+ return worker;
+}
+
+function runMainThread() {
+ rootWorker = startWorker(1, processMessage);
+}
+
+function runChildWorkerThread() {
+ let worker = null;
+ parentPort.on('message', ({ child, depth, done }) => {
+ if (done) {
+ if (worker) {
+ worker.postMessage({ done: true });
+ }
+ parentPort.close();
+ } else if (depth) {
+ parentPort.postMessage({ child: depth });
+ if (depth < MAX_DEPTH) {
+ worker = startWorker(depth + 1, workerCallback);
+ }
+ } else if (child) {
+ parentPort.postMessage({ child });
+ }
+ });
+}
+
+if (isMainThread) {
+ runMainThread();
+} else {
+ runChildWorkerThread();
+}