11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { spawn } = require('child_process');
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst aliveScript = fixtures.path('child-process-stay-alive-forever.js');
91cb0ef41Sopenharmony_ci{
101cb0ef41Sopenharmony_ci  // Verify that passing an AbortSignal works
111cb0ef41Sopenharmony_ci  const controller = new AbortController();
121cb0ef41Sopenharmony_ci  const { signal } = controller;
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  const cp = spawn(process.execPath, [aliveScript], {
151cb0ef41Sopenharmony_ci    signal,
161cb0ef41Sopenharmony_ci  });
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  cp.on('exit', common.mustCall((code, killSignal) => {
191cb0ef41Sopenharmony_ci    assert.strictEqual(code, null);
201cb0ef41Sopenharmony_ci    assert.strictEqual(killSignal, 'SIGTERM');
211cb0ef41Sopenharmony_ci  }));
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  cp.on('error', common.mustCall((e) => {
241cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
251cb0ef41Sopenharmony_ci  }));
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  controller.abort();
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci{
311cb0ef41Sopenharmony_ci  // Verify that passing an AbortSignal with custom abort error works
321cb0ef41Sopenharmony_ci  const controller = new AbortController();
331cb0ef41Sopenharmony_ci  const { signal } = controller;
341cb0ef41Sopenharmony_ci  const cp = spawn(process.execPath, [aliveScript], {
351cb0ef41Sopenharmony_ci    signal,
361cb0ef41Sopenharmony_ci  });
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  cp.on('exit', common.mustCall((code, killSignal) => {
391cb0ef41Sopenharmony_ci    assert.strictEqual(code, null);
401cb0ef41Sopenharmony_ci    assert.strictEqual(killSignal, 'SIGTERM');
411cb0ef41Sopenharmony_ci  }));
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  cp.on('error', common.mustCall((e) => {
441cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
451cb0ef41Sopenharmony_ci    assert.strictEqual(e.cause.name, 'Error');
461cb0ef41Sopenharmony_ci    assert.strictEqual(e.cause.message, 'boom');
471cb0ef41Sopenharmony_ci  }));
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  controller.abort(new Error('boom'));
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci{
531cb0ef41Sopenharmony_ci  const controller = new AbortController();
541cb0ef41Sopenharmony_ci  const { signal } = controller;
551cb0ef41Sopenharmony_ci  const cp = spawn(process.execPath, [aliveScript], {
561cb0ef41Sopenharmony_ci    signal,
571cb0ef41Sopenharmony_ci  });
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  cp.on('exit', common.mustCall((code, killSignal) => {
601cb0ef41Sopenharmony_ci    assert.strictEqual(code, null);
611cb0ef41Sopenharmony_ci    assert.strictEqual(killSignal, 'SIGTERM');
621cb0ef41Sopenharmony_ci  }));
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  cp.on('error', common.mustCall((e) => {
651cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
661cb0ef41Sopenharmony_ci    assert.strictEqual(e.cause, 'boom');
671cb0ef41Sopenharmony_ci  }));
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  controller.abort('boom');
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci{
731cb0ef41Sopenharmony_ci  // Verify that passing an already-aborted signal works.
741cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort();
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  const cp = spawn(process.execPath, [aliveScript], {
771cb0ef41Sopenharmony_ci    signal,
781cb0ef41Sopenharmony_ci  });
791cb0ef41Sopenharmony_ci  cp.on('exit', common.mustCall((code, killSignal) => {
801cb0ef41Sopenharmony_ci    assert.strictEqual(code, null);
811cb0ef41Sopenharmony_ci    assert.strictEqual(killSignal, 'SIGTERM');
821cb0ef41Sopenharmony_ci  }));
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  cp.on('error', common.mustCall((e) => {
851cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
861cb0ef41Sopenharmony_ci  }));
871cb0ef41Sopenharmony_ci}
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci{
901cb0ef41Sopenharmony_ci  // Verify that passing an already-aborted signal with custom abort error
911cb0ef41Sopenharmony_ci  // works.
921cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort(new Error('boom'));
931cb0ef41Sopenharmony_ci  const cp = spawn(process.execPath, [aliveScript], {
941cb0ef41Sopenharmony_ci    signal,
951cb0ef41Sopenharmony_ci  });
961cb0ef41Sopenharmony_ci  cp.on('exit', common.mustCall((code, killSignal) => {
971cb0ef41Sopenharmony_ci    assert.strictEqual(code, null);
981cb0ef41Sopenharmony_ci    assert.strictEqual(killSignal, 'SIGTERM');
991cb0ef41Sopenharmony_ci  }));
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  cp.on('error', common.mustCall((e) => {
1021cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
1031cb0ef41Sopenharmony_ci    assert.strictEqual(e.cause.name, 'Error');
1041cb0ef41Sopenharmony_ci    assert.strictEqual(e.cause.message, 'boom');
1051cb0ef41Sopenharmony_ci  }));
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci{
1091cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort('boom');
1101cb0ef41Sopenharmony_ci  const cp = spawn(process.execPath, [aliveScript], {
1111cb0ef41Sopenharmony_ci    signal,
1121cb0ef41Sopenharmony_ci  });
1131cb0ef41Sopenharmony_ci  cp.on('exit', common.mustCall((code, killSignal) => {
1141cb0ef41Sopenharmony_ci    assert.strictEqual(code, null);
1151cb0ef41Sopenharmony_ci    assert.strictEqual(killSignal, 'SIGTERM');
1161cb0ef41Sopenharmony_ci  }));
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  cp.on('error', common.mustCall((e) => {
1191cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
1201cb0ef41Sopenharmony_ci    assert.strictEqual(e.cause, 'boom');
1211cb0ef41Sopenharmony_ci  }));
1221cb0ef41Sopenharmony_ci}
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci{
1251cb0ef41Sopenharmony_ci  // Verify that waiting a bit and closing works
1261cb0ef41Sopenharmony_ci  const controller = new AbortController();
1271cb0ef41Sopenharmony_ci  const { signal } = controller;
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci  const cp = spawn(process.execPath, [aliveScript], {
1301cb0ef41Sopenharmony_ci    signal,
1311cb0ef41Sopenharmony_ci  });
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  cp.on('exit', common.mustCall((code, killSignal) => {
1341cb0ef41Sopenharmony_ci    assert.strictEqual(code, null);
1351cb0ef41Sopenharmony_ci    assert.strictEqual(killSignal, 'SIGTERM');
1361cb0ef41Sopenharmony_ci  }));
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  cp.on('error', common.mustCall((e) => {
1391cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
1401cb0ef41Sopenharmony_ci  }));
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  setTimeout(() => controller.abort(), 1);
1431cb0ef41Sopenharmony_ci}
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci{
1461cb0ef41Sopenharmony_ci  // Test passing a different killSignal
1471cb0ef41Sopenharmony_ci  const controller = new AbortController();
1481cb0ef41Sopenharmony_ci  const { signal } = controller;
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci  const cp = spawn(process.execPath, [aliveScript], {
1511cb0ef41Sopenharmony_ci    signal,
1521cb0ef41Sopenharmony_ci    killSignal: 'SIGKILL',
1531cb0ef41Sopenharmony_ci  });
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  cp.on('exit', common.mustCall((code, killSignal) => {
1561cb0ef41Sopenharmony_ci    assert.strictEqual(code, null);
1571cb0ef41Sopenharmony_ci    assert.strictEqual(killSignal, 'SIGKILL');
1581cb0ef41Sopenharmony_ci  }));
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci  cp.on('error', common.mustCall((e) => {
1611cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
1621cb0ef41Sopenharmony_ci  }));
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  setTimeout(() => controller.abort(), 1);
1651cb0ef41Sopenharmony_ci}
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci{
1681cb0ef41Sopenharmony_ci  // Test aborting a cp before close but after exit
1691cb0ef41Sopenharmony_ci  const controller = new AbortController();
1701cb0ef41Sopenharmony_ci  const { signal } = controller;
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  const cp = spawn(process.execPath, [aliveScript], {
1731cb0ef41Sopenharmony_ci    signal,
1741cb0ef41Sopenharmony_ci  });
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci  cp.on('exit', common.mustCall(() => {
1771cb0ef41Sopenharmony_ci    controller.abort();
1781cb0ef41Sopenharmony_ci  }));
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci  cp.on('error', common.mustNotCall());
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci  setTimeout(() => cp.kill(), 1);
1831cb0ef41Sopenharmony_ci}
184