11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst { mustCall, mustNotCall } = require('../common');
41cb0ef41Sopenharmony_ciconst { strictEqual } = require('assert');
51cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
61cb0ef41Sopenharmony_ciconst { fork } = require('child_process');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci{
91cb0ef41Sopenharmony_ci  // Test aborting a forked child_process after calling fork
101cb0ef41Sopenharmony_ci  const ac = new AbortController();
111cb0ef41Sopenharmony_ci  const { signal } = ac;
121cb0ef41Sopenharmony_ci  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
131cb0ef41Sopenharmony_ci    signal
141cb0ef41Sopenharmony_ci  });
151cb0ef41Sopenharmony_ci  cp.on('exit', mustCall((code, killSignal) => {
161cb0ef41Sopenharmony_ci    strictEqual(code, null);
171cb0ef41Sopenharmony_ci    strictEqual(killSignal, 'SIGTERM');
181cb0ef41Sopenharmony_ci  }));
191cb0ef41Sopenharmony_ci  cp.on('error', mustCall((err) => {
201cb0ef41Sopenharmony_ci    strictEqual(err.name, 'AbortError');
211cb0ef41Sopenharmony_ci  }));
221cb0ef41Sopenharmony_ci  process.nextTick(() => ac.abort());
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci{
261cb0ef41Sopenharmony_ci  // Test aborting with custom error
271cb0ef41Sopenharmony_ci  const ac = new AbortController();
281cb0ef41Sopenharmony_ci  const { signal } = ac;
291cb0ef41Sopenharmony_ci  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
301cb0ef41Sopenharmony_ci    signal
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci  cp.on('exit', mustCall((code, killSignal) => {
331cb0ef41Sopenharmony_ci    strictEqual(code, null);
341cb0ef41Sopenharmony_ci    strictEqual(killSignal, 'SIGTERM');
351cb0ef41Sopenharmony_ci  }));
361cb0ef41Sopenharmony_ci  cp.on('error', mustCall((err) => {
371cb0ef41Sopenharmony_ci    strictEqual(err.name, 'AbortError');
381cb0ef41Sopenharmony_ci    strictEqual(err.cause.name, 'Error');
391cb0ef41Sopenharmony_ci    strictEqual(err.cause.message, 'boom');
401cb0ef41Sopenharmony_ci  }));
411cb0ef41Sopenharmony_ci  process.nextTick(() => ac.abort(new Error('boom')));
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci  // Test passing an already aborted signal to a forked child_process
461cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort();
471cb0ef41Sopenharmony_ci  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
481cb0ef41Sopenharmony_ci    signal
491cb0ef41Sopenharmony_ci  });
501cb0ef41Sopenharmony_ci  cp.on('exit', mustCall((code, killSignal) => {
511cb0ef41Sopenharmony_ci    strictEqual(code, null);
521cb0ef41Sopenharmony_ci    strictEqual(killSignal, 'SIGTERM');
531cb0ef41Sopenharmony_ci  }));
541cb0ef41Sopenharmony_ci  cp.on('error', mustCall((err) => {
551cb0ef41Sopenharmony_ci    strictEqual(err.name, 'AbortError');
561cb0ef41Sopenharmony_ci  }));
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci{
601cb0ef41Sopenharmony_ci  // Test passing an aborted signal with custom error to a forked child_process
611cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort(new Error('boom'));
621cb0ef41Sopenharmony_ci  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
631cb0ef41Sopenharmony_ci    signal
641cb0ef41Sopenharmony_ci  });
651cb0ef41Sopenharmony_ci  cp.on('exit', mustCall((code, killSignal) => {
661cb0ef41Sopenharmony_ci    strictEqual(code, null);
671cb0ef41Sopenharmony_ci    strictEqual(killSignal, 'SIGTERM');
681cb0ef41Sopenharmony_ci  }));
691cb0ef41Sopenharmony_ci  cp.on('error', mustCall((err) => {
701cb0ef41Sopenharmony_ci    strictEqual(err.name, 'AbortError');
711cb0ef41Sopenharmony_ci    strictEqual(err.cause.name, 'Error');
721cb0ef41Sopenharmony_ci    strictEqual(err.cause.message, 'boom');
731cb0ef41Sopenharmony_ci  }));
741cb0ef41Sopenharmony_ci}
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci{
771cb0ef41Sopenharmony_ci  // Test passing a different kill signal
781cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort();
791cb0ef41Sopenharmony_ci  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
801cb0ef41Sopenharmony_ci    signal,
811cb0ef41Sopenharmony_ci    killSignal: 'SIGKILL',
821cb0ef41Sopenharmony_ci  });
831cb0ef41Sopenharmony_ci  cp.on('exit', mustCall((code, killSignal) => {
841cb0ef41Sopenharmony_ci    strictEqual(code, null);
851cb0ef41Sopenharmony_ci    strictEqual(killSignal, 'SIGKILL');
861cb0ef41Sopenharmony_ci  }));
871cb0ef41Sopenharmony_ci  cp.on('error', mustCall((err) => {
881cb0ef41Sopenharmony_ci    strictEqual(err.name, 'AbortError');
891cb0ef41Sopenharmony_ci  }));
901cb0ef41Sopenharmony_ci}
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci{
931cb0ef41Sopenharmony_ci  // Test aborting a cp before close but after exit
941cb0ef41Sopenharmony_ci  const ac = new AbortController();
951cb0ef41Sopenharmony_ci  const { signal } = ac;
961cb0ef41Sopenharmony_ci  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
971cb0ef41Sopenharmony_ci    signal
981cb0ef41Sopenharmony_ci  });
991cb0ef41Sopenharmony_ci  cp.on('exit', mustCall(() => {
1001cb0ef41Sopenharmony_ci    ac.abort();
1011cb0ef41Sopenharmony_ci  }));
1021cb0ef41Sopenharmony_ci  cp.on('error', mustNotCall());
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  setTimeout(() => cp.kill(), 1);
1051cb0ef41Sopenharmony_ci}
106