11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (common.isWindows) {
41cb0ef41Sopenharmony_ci  // No way to send CTRL_C_EVENT to processes from JS right now.
51cb0ef41Sopenharmony_ci  common.skip('platform not supported');
61cb0ef41Sopenharmony_ci}
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst vm = require('vm');
101cb0ef41Sopenharmony_ciconst spawn = require('child_process').spawn;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
131cb0ef41Sopenharmony_ci  const method = process.argv[3];
141cb0ef41Sopenharmony_ci  const listeners = +process.argv[4];
151cb0ef41Sopenharmony_ci  assert.ok(method);
161cb0ef41Sopenharmony_ci  assert.ok(Number.isInteger(listeners));
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  const script = `process.send('${method}'); while(true) {}`;
191cb0ef41Sopenharmony_ci  const args = method === 'runInContext' ?
201cb0ef41Sopenharmony_ci    [vm.createContext({ process })] :
211cb0ef41Sopenharmony_ci    [];
221cb0ef41Sopenharmony_ci  const options = { breakOnSigint: true };
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  for (let i = 0; i < listeners; i++)
251cb0ef41Sopenharmony_ci    process.on('SIGINT', common.mustNotCall());
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  assert.throws(
281cb0ef41Sopenharmony_ci    () => { vm[method](script, ...args, options); },
291cb0ef41Sopenharmony_ci    {
301cb0ef41Sopenharmony_ci      code: 'ERR_SCRIPT_EXECUTION_INTERRUPTED',
311cb0ef41Sopenharmony_ci      message: 'Script execution was interrupted by `SIGINT`'
321cb0ef41Sopenharmony_ci    });
331cb0ef41Sopenharmony_ci  return;
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cifor (const method of ['runInThisContext', 'runInContext']) {
371cb0ef41Sopenharmony_ci  for (const listeners of [0, 1, 2]) {
381cb0ef41Sopenharmony_ci    const args = [__filename, 'child', method, listeners];
391cb0ef41Sopenharmony_ci    const child = spawn(process.execPath, args, {
401cb0ef41Sopenharmony_ci      stdio: [null, 'pipe', 'inherit', 'ipc']
411cb0ef41Sopenharmony_ci    });
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    child.on('message', common.mustCall(() => {
441cb0ef41Sopenharmony_ci      process.kill(child.pid, 'SIGINT');
451cb0ef41Sopenharmony_ci    }));
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci    child.on('close', common.mustCall((code, signal) => {
481cb0ef41Sopenharmony_ci      assert.strictEqual(signal, null);
491cb0ef41Sopenharmony_ci      assert.strictEqual(code, 0);
501cb0ef41Sopenharmony_ci    }));
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci}
53