11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst fs = require('fs');
41cb0ef41Sopenharmony_ciconst cp = require('child_process');
51cb0ef41Sopenharmony_ciconst path = require('path');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
81cb0ef41Sopenharmony_citmpdir.refresh();
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst LOG_FILE = path.join(tmpdir.path, 'tick-processor.log');
111cb0ef41Sopenharmony_ciconst RETRY_TIMEOUT = 150;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifunction runTest(test) {
141cb0ef41Sopenharmony_ci  const proc = cp.spawn(process.execPath, [
151cb0ef41Sopenharmony_ci    '--no_logfile_per_isolate',
161cb0ef41Sopenharmony_ci    '--logfile=-',
171cb0ef41Sopenharmony_ci    '--prof',
181cb0ef41Sopenharmony_ci    '-pe', test.code,
191cb0ef41Sopenharmony_ci  ], {
201cb0ef41Sopenharmony_ci    stdio: [ 'ignore', 'pipe', 'inherit' ],
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  let ticks = '';
241cb0ef41Sopenharmony_ci  proc.stdout.on('data', (chunk) => ticks += chunk);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  // Try to match after timeout
271cb0ef41Sopenharmony_ci  setTimeout(() => {
281cb0ef41Sopenharmony_ci    match(test.pattern, proc, () => ticks, test.profProcessFlags);
291cb0ef41Sopenharmony_ci  }, RETRY_TIMEOUT);
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cifunction match(pattern, parent, ticks, flags = []) {
331cb0ef41Sopenharmony_ci  // Store current ticks log
341cb0ef41Sopenharmony_ci  fs.writeFileSync(LOG_FILE, ticks());
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  const proc = cp.spawn(process.execPath, [
371cb0ef41Sopenharmony_ci    '--prof-process',
381cb0ef41Sopenharmony_ci    '--call-graph-size=10',
391cb0ef41Sopenharmony_ci    ...flags,
401cb0ef41Sopenharmony_ci    LOG_FILE,
411cb0ef41Sopenharmony_ci  ], {
421cb0ef41Sopenharmony_ci    stdio: [ 'ignore', 'pipe', 'inherit' ],
431cb0ef41Sopenharmony_ci  });
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  let out = '';
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  proc.stdout.on('data', (chunk) => out += chunk);
481cb0ef41Sopenharmony_ci  proc.stdout.once('end', () => {
491cb0ef41Sopenharmony_ci    proc.once('exit', () => {
501cb0ef41Sopenharmony_ci      fs.unlinkSync(LOG_FILE);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci      // Retry after timeout
531cb0ef41Sopenharmony_ci      if (!pattern.test(out))
541cb0ef41Sopenharmony_ci        return setTimeout(() => match(pattern, parent, ticks), RETRY_TIMEOUT);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci      parent.stdout.removeAllListeners();
571cb0ef41Sopenharmony_ci      parent.kill();
581cb0ef41Sopenharmony_ci    });
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci    proc.stdout.removeAllListeners();
611cb0ef41Sopenharmony_ci    proc.kill();
621cb0ef41Sopenharmony_ci  });
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciexports.runTest = runTest;
66