11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
51cb0ef41Sopenharmony_ciconst async_hooks = require('async_hooks');
61cb0ef41Sopenharmony_ciconst initHooks = require('./init-hooks');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst arg = process.argv[2];
91cb0ef41Sopenharmony_ciswitch (arg) {
101cb0ef41Sopenharmony_ci  case 'test_init_callback':
111cb0ef41Sopenharmony_ci    initHooks({
121cb0ef41Sopenharmony_ci      oninit: common.mustCall(() => { throw new Error(arg); }),
131cb0ef41Sopenharmony_ci    }).enable();
141cb0ef41Sopenharmony_ci    new async_hooks.AsyncResource(`${arg}_type`);
151cb0ef41Sopenharmony_ci    return;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  case 'test_callback': {
181cb0ef41Sopenharmony_ci    initHooks({
191cb0ef41Sopenharmony_ci      onbefore: common.mustCall(() => { throw new Error(arg); }),
201cb0ef41Sopenharmony_ci    }).enable();
211cb0ef41Sopenharmony_ci    const resource = new async_hooks.AsyncResource(`${arg}_type`);
221cb0ef41Sopenharmony_ci    resource.runInAsyncScope(() => {});
231cb0ef41Sopenharmony_ci    return;
241cb0ef41Sopenharmony_ci  }
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  case 'test_callback_abort':
271cb0ef41Sopenharmony_ci    initHooks({
281cb0ef41Sopenharmony_ci      oninit: common.mustCall(() => { throw new Error(arg); }),
291cb0ef41Sopenharmony_ci    }).enable();
301cb0ef41Sopenharmony_ci    new async_hooks.AsyncResource(`${arg}_type`);
311cb0ef41Sopenharmony_ci    return;
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci// This part should run only for the primary test
351cb0ef41Sopenharmony_ciassert.ok(!arg);
361cb0ef41Sopenharmony_ci{
371cb0ef41Sopenharmony_ci  // console.log should stay until this test's flakiness is solved
381cb0ef41Sopenharmony_ci  console.log('start case 1');
391cb0ef41Sopenharmony_ci  console.time('end case 1');
401cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [__filename, 'test_init_callback']);
411cb0ef41Sopenharmony_ci  assert.ifError(child.error);
421cb0ef41Sopenharmony_ci  const test_init_first_line = child.stderr.toString().split(/[\r\n]+/g)[0];
431cb0ef41Sopenharmony_ci  assert.strictEqual(test_init_first_line, 'Error: test_init_callback');
441cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 1);
451cb0ef41Sopenharmony_ci  console.timeEnd('end case 1');
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci{
491cb0ef41Sopenharmony_ci  console.log('start case 2');
501cb0ef41Sopenharmony_ci  console.time('end case 2');
511cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [__filename, 'test_callback']);
521cb0ef41Sopenharmony_ci  assert.ifError(child.error);
531cb0ef41Sopenharmony_ci  const test_callback_first_line = child.stderr.toString().split(/[\r\n]+/g)[0];
541cb0ef41Sopenharmony_ci  assert.strictEqual(test_callback_first_line, 'Error: test_callback');
551cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 1);
561cb0ef41Sopenharmony_ci  console.timeEnd('end case 2');
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci{
601cb0ef41Sopenharmony_ci  console.log('start case 3');
611cb0ef41Sopenharmony_ci  console.time('end case 3');
621cb0ef41Sopenharmony_ci  let program = process.execPath;
631cb0ef41Sopenharmony_ci  let args = [
641cb0ef41Sopenharmony_ci    '--abort-on-uncaught-exception', __filename, 'test_callback_abort' ];
651cb0ef41Sopenharmony_ci  const options = { encoding: 'utf8' };
661cb0ef41Sopenharmony_ci  if (!common.isWindows) {
671cb0ef41Sopenharmony_ci    program = `ulimit -c 0 && exec ${program} ${args.join(' ')}`;
681cb0ef41Sopenharmony_ci    args = [];
691cb0ef41Sopenharmony_ci    options.shell = true;
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci  const child = spawnSync(program, args, options);
721cb0ef41Sopenharmony_ci  if (common.isWindows) {
731cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 134);
741cb0ef41Sopenharmony_ci    assert.strictEqual(child.signal, null);
751cb0ef41Sopenharmony_ci  } else {
761cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, null);
771cb0ef41Sopenharmony_ci    // Most posix systems will show 'SIGABRT', but alpine34 does not
781cb0ef41Sopenharmony_ci    if (child.signal !== 'SIGABRT') {
791cb0ef41Sopenharmony_ci      console.log(`primary received signal ${child.signal}\nchild's stderr:`);
801cb0ef41Sopenharmony_ci      console.log(child.stderr);
811cb0ef41Sopenharmony_ci      process.exit(1);
821cb0ef41Sopenharmony_ci    }
831cb0ef41Sopenharmony_ci    assert.strictEqual(child.signal, 'SIGABRT');
841cb0ef41Sopenharmony_ci  }
851cb0ef41Sopenharmony_ci  assert.strictEqual(child.stdout, '');
861cb0ef41Sopenharmony_ci  const firstLineStderr = child.stderr.split(/[\r\n]+/g)[0].trim();
871cb0ef41Sopenharmony_ci  assert.strictEqual(firstLineStderr, 'Error: test_callback_abort');
881cb0ef41Sopenharmony_ci  console.timeEnd('end case 3');
891cb0ef41Sopenharmony_ci}
90