1// Flags: --experimental-abortcontroller 2'use strict'; 3 4const { mustCall, mustNotCall } = require('../common'); 5const { strictEqual } = require('assert'); 6const fixtures = require('../common/fixtures'); 7const { fork } = require('child_process'); 8 9{ 10 // Test aborting a forked child_process after calling fork 11 const ac = new AbortController(); 12 const { signal } = ac; 13 const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { 14 signal 15 }); 16 cp.on('exit', mustCall((code, killSignal) => { 17 strictEqual(code, null); 18 strictEqual(killSignal, 'SIGTERM'); 19 })); 20 cp.on('error', mustCall((err) => { 21 strictEqual(err.name, 'AbortError'); 22 })); 23 process.nextTick(() => ac.abort()); 24} 25{ 26 // Test passing an already aborted signal to a forked child_process 27 const ac = new AbortController(); 28 const { signal } = ac; 29 ac.abort(); 30 const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { 31 signal 32 }); 33 cp.on('exit', mustCall((code, killSignal) => { 34 strictEqual(code, null); 35 strictEqual(killSignal, 'SIGTERM'); 36 })); 37 cp.on('error', mustCall((err) => { 38 strictEqual(err.name, 'AbortError'); 39 })); 40} 41 42{ 43 // Test passing a different kill signal 44 const ac = new AbortController(); 45 const { signal } = ac; 46 ac.abort(); 47 const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { 48 signal, 49 killSignal: 'SIGKILL', 50 }); 51 cp.on('exit', mustCall((code, killSignal) => { 52 strictEqual(code, null); 53 strictEqual(killSignal, 'SIGKILL'); 54 })); 55 cp.on('error', mustCall((err) => { 56 strictEqual(err.name, 'AbortError'); 57 })); 58} 59 60{ 61 // Test aborting a cp before close but after exit 62 const ac = new AbortController(); 63 const { signal } = ac; 64 const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { 65 signal 66 }); 67 cp.on('exit', mustCall(() => { 68 ac.abort(); 69 })); 70 cp.on('error', mustNotCall()); 71 72 setTimeout(() => cp.kill(), 1); 73} 74