11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst cp = require('child_process');
51cb0ef41Sopenharmony_ciconst fs = require('fs');
61cb0ef41Sopenharmony_ciconst path = require('path');
71cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
101cb0ef41Sopenharmony_ci  const p = Promise.reject(1);  // Handled later
111cb0ef41Sopenharmony_ci  Promise.reject(2);  // Unhandled
121cb0ef41Sopenharmony_ci  setImmediate(() => {
131cb0ef41Sopenharmony_ci    p.catch(() => { /* intentional noop */ });
141cb0ef41Sopenharmony_ci  });
151cb0ef41Sopenharmony_ci} else {
161cb0ef41Sopenharmony_ci  tmpdir.refresh();
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  const proc = cp.fork(__filename,
191cb0ef41Sopenharmony_ci                       [ 'child' ], {
201cb0ef41Sopenharmony_ci                         cwd: tmpdir.path,
211cb0ef41Sopenharmony_ci                         execArgv: [
221cb0ef41Sopenharmony_ci                           '--no-warnings',
231cb0ef41Sopenharmony_ci                           '--trace-event-categories',
241cb0ef41Sopenharmony_ci                           'node.promises.rejections',
251cb0ef41Sopenharmony_ci                         ]
261cb0ef41Sopenharmony_ci                       });
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  proc.once('exit', common.mustCall(() => {
291cb0ef41Sopenharmony_ci    const file = path.join(tmpdir.path, 'node_trace.1.log');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    assert(fs.existsSync(file));
321cb0ef41Sopenharmony_ci    fs.readFile(file, common.mustCall((err, data) => {
331cb0ef41Sopenharmony_ci      const traces = JSON.parse(data.toString()).traceEvents
341cb0ef41Sopenharmony_ci        .filter((trace) => trace.cat !== '__metadata');
351cb0ef41Sopenharmony_ci      traces.forEach((trace) => {
361cb0ef41Sopenharmony_ci        assert.strictEqual(trace.pid, proc.pid);
371cb0ef41Sopenharmony_ci        assert.strictEqual(trace.name, 'rejections');
381cb0ef41Sopenharmony_ci        assert(trace.args.unhandled <= 2);
391cb0ef41Sopenharmony_ci        assert(trace.args.handledAfter <= 1);
401cb0ef41Sopenharmony_ci      });
411cb0ef41Sopenharmony_ci    }));
421cb0ef41Sopenharmony_ci  }));
431cb0ef41Sopenharmony_ci}
44