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_ciif (!common.isMainThread)
81cb0ef41Sopenharmony_ci  common.skip('No signal handling available in Workers');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst spawn = require('child_process').spawn;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciprocess.env.REPL_TEST_PPID = process.pid;
141cb0ef41Sopenharmony_ciconst child = spawn(process.execPath, [ '-i' ], {
151cb0ef41Sopenharmony_ci  stdio: [null, null, 2]
161cb0ef41Sopenharmony_ci});
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cilet stdout = '';
191cb0ef41Sopenharmony_cichild.stdout.setEncoding('utf8');
201cb0ef41Sopenharmony_cichild.stdout.on('data', function(c) {
211cb0ef41Sopenharmony_ci  stdout += c;
221cb0ef41Sopenharmony_ci});
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cichild.stdout.once('data', common.mustCall(() => {
251cb0ef41Sopenharmony_ci  process.on('SIGUSR2', common.mustCall(() => {
261cb0ef41Sopenharmony_ci    process.kill(child.pid, 'SIGINT');
271cb0ef41Sopenharmony_ci    child.stdout.once('data', common.mustCall(() => {
281cb0ef41Sopenharmony_ci      // Make sure state from before the interruption is still available.
291cb0ef41Sopenharmony_ci      child.stdin.end('a*2*3*7\n');
301cb0ef41Sopenharmony_ci    }));
311cb0ef41Sopenharmony_ci  }));
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  child.stdin.write('a = 1001;' +
341cb0ef41Sopenharmony_ci                    'process.kill(+process.env.REPL_TEST_PPID, "SIGUSR2");' +
351cb0ef41Sopenharmony_ci                    'while(true){}\n');
361cb0ef41Sopenharmony_ci}));
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cichild.on('close', function(code) {
391cb0ef41Sopenharmony_ci  assert.strictEqual(code, 0);
401cb0ef41Sopenharmony_ci  const expected = 'Script execution was interrupted by `SIGINT`';
411cb0ef41Sopenharmony_ci  assert.ok(
421cb0ef41Sopenharmony_ci    stdout.includes(expected),
431cb0ef41Sopenharmony_ci    `Expected stdout to contain "${expected}", got ${stdout}`
441cb0ef41Sopenharmony_ci  );
451cb0ef41Sopenharmony_ci  assert.ok(
461cb0ef41Sopenharmony_ci    stdout.includes('42042\n'),
471cb0ef41Sopenharmony_ci    `Expected stdout to contain "42042", got ${stdout}`
481cb0ef41Sopenharmony_ci  );
491cb0ef41Sopenharmony_ci});
50