| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b6e174b commit d72b682
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,6 +55,13 @@ class ParentInspectorHandle { | |||
| 55 | 55 | std::shared_ptr<MainThreadHandle> parent_thread, | |
| 56 | 56 | bool wait_for_connect); | |
| 57 | 57 | ~ParentInspectorHandle(); | |
| 58 | + std::unique_ptr<ParentInspectorHandle> NewParentInspectorHandle( | ||
| 59 | + int thread_id, const std::string& url) { | ||
| 60 | + return std::make_unique<ParentInspectorHandle>(thread_id, | ||
| 61 | + url, | ||
| 62 | + parent_thread_, | ||
| 63 | + wait_); | ||
| 64 | + } | ||
| 58 | 65 | void WorkerStarted(std::shared_ptr<MainThreadHandle> worker_thread, | |
| 59 | 66 | bool waiting); | |
| 60 | 67 | bool WaitForConnect() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -237,17 +237,21 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel, | |||
| 237 | 237 | tracing_agent_ = | |
| 238 | 238 | std::make_unique<protocol::TracingAgent>(env, main_thread_); | |
| 239 | 239 | tracing_agent_->Wire(node_dispatcher_.get()); | |
| 240 | - worker_agent_ = std::make_unique<protocol::WorkerAgent>(worker_manager); | ||
| 241 | - worker_agent_->Wire(node_dispatcher_.get()); | ||
| 240 | + if (worker_manager) { | ||
| 241 | + worker_agent_ = std::make_unique<protocol::WorkerAgent>(worker_manager); | ||
| 242 | + worker_agent_->Wire(node_dispatcher_.get()); | ||
| 243 | + } | ||
| 242 | 244 | runtime_agent_ = std::make_unique<protocol::RuntimeAgent>(); | |
| 243 | 245 | runtime_agent_->Wire(node_dispatcher_.get()); | |
| 244 | 246 | } | |
| 245 | 247 | ||
| 246 | 248 | ~ChannelImpl() override { | |
| 247 | 249 | tracing_agent_->disable(); | |
| 248 | 250 | tracing_agent_.reset(); // Dispose before the dispatchers | |
| 249 | - worker_agent_->disable(); | ||
| 250 | - worker_agent_.reset(); // Dispose before the dispatchers | ||
| 251 | + if (worker_agent_) { | ||
| 252 | + worker_agent_->disable(); | ||
| 253 | + worker_agent_.reset(); // Dispose before the dispatchers | ||
| 254 | + } | ||
| 251 | 255 | runtime_agent_->disable(); | |
| 252 | 256 | runtime_agent_.reset(); // Dispose before the dispatchers | |
| 253 | 257 | } | |
@@ -670,6 +674,9 @@ class NodeInspectorClient : public V8InspectorClient { | |||
| 670 | 674 | } | |
| 671 | 675 | ||
| 672 | 676 | std::shared_ptr<WorkerManager> getWorkerManager() { | |
| 677 | + if (!is_main_) { | ||
| 678 | + return nullptr; | ||
| 679 | + } | ||
| 673 | 680 | if (worker_manager_ == nullptr) { | |
| 674 | 681 | worker_manager_ = | |
| 675 | 682 | std::make_shared<WorkerManager>(getThreadHandle()); | |
@@ -968,7 +975,11 @@ void Agent::SetParentHandle( | |||
| 968 | 975 | ||
| 969 | 976 | std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle( | |
| 970 | 977 | int thread_id, const std::string& url) { | |
| 971 | - return client_->getWorkerManager()->NewParentHandle(thread_id, url); | ||
| 978 | + if (!parent_handle_) { | ||
| 979 | + return client_->getWorkerManager()->NewParentHandle(thread_id, url); | ||
| 980 | + } else { | ||
| 981 | + return parent_handle_->NewParentInspectorHandle(thread_id, url); | ||
| 982 | + } | ||
| 972 | 983 | } | |
| 973 | 984 | ||
| 974 | 985 | void Agent::WaitForConnect() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | const common = require('../common'); | |
| 4 | 4 | ||
| 5 | 5 | common.skipIfInspectorDisabled(); | |
| 6 | + common.skipIfWorker(); | ||
| 6 | 7 | ||
| 7 | 8 | const assert = require('assert'); | |
| 8 | 9 | const { Worker } = require('worker_threads'); | |
@@ -12,9 +13,7 @@ const session = new Session(); | |||
| 12 | 13 | ||
| 13 | 14 | let done = false; | |
| 14 | 15 | ||
| 15 | - session.connect(); | ||
| 16 | - | ||
| 17 | - session.on('NodeWorker.attachedToWorker', ({ params: { sessionId } }) => { | ||
| 16 | + function onAttachToWorker({ params: { sessionId } }) { | ||
| 18 | 17 | let id = 1; | |
| 19 | 18 | function postToWorkerInspector(method, params) { | |
| 20 | 19 | session.post('NodeWorker.sendMessageToWorker', { | |
@@ -47,7 +46,11 @@ session.on('NodeWorker.attachedToWorker', ({ params: { sessionId } }) => { | |||
| 47 | 46 | { enabled: true }); | |
| 48 | 47 | // start worker | |
| 49 | 48 | postToWorkerInspector('Runtime.runIfWaitingForDebugger'); | |
| 50 | - }); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + session.connect(); | ||
| 52 | + | ||
| 53 | + session.on('NodeWorker.attachedToWorker', common.mustCall(onAttachToWorker)); | ||
| 51 | 54 | ||
| 52 | 55 | session.post('NodeWorker.enable', { waitForDebuggerOnStart: true }, () => { | |
| 53 | 56 | new Worker('console.log("Worker is done")', { eval: true }) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,78 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + | ||
| 4 | + common.skipIfInspectorDisabled(); | ||
| 5 | + | ||
| 6 | + const { Worker, isMainThread, parentPort, workerData } = | ||
| 7 | + require('worker_threads'); | ||
| 8 | + | ||
| 9 | + if (isMainThread || workerData !== 'launched by test') { | ||
| 10 | + common.skipIfWorker(); | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + const { Session } = require('inspector'); | ||
| 14 | + | ||
| 15 | + const MAX_DEPTH = 3; | ||
| 16 | + | ||
| 17 | + let rootWorker = null; | ||
| 18 | + | ||
| 19 | + const runTest = common.mustCall(function() { | ||
| 20 | + let reportedWorkersCount = 0; | ||
| 21 | + const session = new Session(); | ||
| 22 | + session.connect(); | ||
| 23 | + session.on('NodeWorker.attachedToWorker', common.mustCall( | ||
| 24 | + ({ params: { workerInfo } }) => { | ||
| 25 | + console.log(`Worker ${workerInfo.title} was reported`); | ||
| 26 | + if (++reportedWorkersCount === MAX_DEPTH) { | ||
| 27 | + rootWorker.postMessage({ done: true }); | ||
| 28 | + } | ||
| 29 | + }, MAX_DEPTH)); | ||
| 30 | + session.post('NodeWorker.enable', { waitForDebuggerOnStart: false }); | ||
| 31 | + }); | ||
| 32 | + | ||
| 33 | + function processMessage({ child }) { | ||
| 34 | + console.log(`Worker ${child} is running`); | ||
| 35 | + if (child === MAX_DEPTH) { | ||
| 36 | + runTest(); | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + function workerCallback(message) { | ||
| 41 | + parentPort.postMessage(message); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + function startWorker(depth, messageCallback) { | ||
| 45 | + const worker = new Worker(__filename, { workerData: 'launched by test' }); | ||
| 46 | + worker.on('message', messageCallback); | ||
| 47 | + worker.postMessage({ depth }); | ||
| 48 | + return worker; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + function runMainThread() { | ||
| 52 | + rootWorker = startWorker(1, processMessage); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + function runChildWorkerThread() { | ||
| 56 | + let worker = null; | ||
| 57 | + parentPort.on('message', ({ child, depth, done }) => { | ||
| 58 | + if (done) { | ||
| 59 | + if (worker) { | ||
| 60 | + worker.postMessage({ done: true }); | ||
| 61 | + } | ||
| 62 | + parentPort.close(); | ||
| 63 | + } else if (depth) { | ||
| 64 | + parentPort.postMessage({ child: depth }); | ||
| 65 | + if (depth < MAX_DEPTH) { | ||
| 66 | + worker = startWorker(depth + 1, workerCallback); | ||
| 67 | + } | ||
| 68 | + } else if (child) { | ||
| 69 | + parentPort.postMessage({ child }); | ||
| 70 | + } | ||
| 71 | + }); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + if (isMainThread) { | ||
| 75 | + runMainThread(); | ||
| 76 | + } else { | ||
| 77 | + runChildWorkerThread(); | ||
| 78 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments