11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst inspector = require('inspector');
81cb0ef41Sopenharmony_ciconst stream = require('stream');
91cb0ef41Sopenharmony_ciconst { Worker, workerData } = require('worker_threads');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst session = new inspector.Session();
121cb0ef41Sopenharmony_cisession.connect();
131cb0ef41Sopenharmony_cisession.post('HeapProfiler.enable');
141cb0ef41Sopenharmony_cisession.post('HeapProfiler.startTrackingHeapObjects',
151cb0ef41Sopenharmony_ci             { trackAllocations: true });
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci// Perform some silly heap allocations for the next 100 ms.
181cb0ef41Sopenharmony_ciconst interval = setInterval(() => {
191cb0ef41Sopenharmony_ci  new stream.PassThrough().end('abc').on('data', common.mustCall());
201cb0ef41Sopenharmony_ci}, 1);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cisetTimeout(() => {
231cb0ef41Sopenharmony_ci  clearInterval(interval);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  // Once the main test is done, we re-run it from inside a Worker thread
261cb0ef41Sopenharmony_ci  // and stop early, as that is a good way to make sure the timer handles
271cb0ef41Sopenharmony_ci  // internally created by the inspector are cleaned up properly.
281cb0ef41Sopenharmony_ci  if (workerData === 'stopEarly')
291cb0ef41Sopenharmony_ci    process.exit();
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  let data = '';
321cb0ef41Sopenharmony_ci  session.on('HeapProfiler.addHeapSnapshotChunk',
331cb0ef41Sopenharmony_ci             common.mustCallAtLeast((event) => {
341cb0ef41Sopenharmony_ci               data += event.params.chunk;
351cb0ef41Sopenharmony_ci             }));
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  // TODO(addaleax): Using `{ reportProgress: true }` crashes the process
381cb0ef41Sopenharmony_ci  // because the progress indication event would mean calling into JS while
391cb0ef41Sopenharmony_ci  // a heap snapshot is being taken, which is forbidden.
401cb0ef41Sopenharmony_ci  // What can we do about that?
411cb0ef41Sopenharmony_ci  session.post('HeapProfiler.stopTrackingHeapObjects');
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  assert(data.includes('PassThrough'), data);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  new Worker(__filename, { workerData: 'stopEarly' });
461cb0ef41Sopenharmony_ci}, 100);
47