-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
child_process: allow promisified exec to be cancel
Using new AbortController, add support for promisified exec to be cancelled. PR-URL: #34249 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
1cbb74d
commit da98c9f
Showing
4 changed files
with
160 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
test/parallel/test-child-process-exec-abortcontroller-promisified.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Flags: --experimental-abortcontroller | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const exec = require('child_process').exec; | ||
const { promisify } = require('util'); | ||
|
||
let pwdcommand, dir; | ||
const execPromisifed = promisify(exec); | ||
const invalidArgTypeError = { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
name: 'TypeError' | ||
}; | ||
|
||
|
||
if (common.isWindows) { | ||
pwdcommand = 'echo %cd%'; | ||
dir = 'c:\\windows'; | ||
} else { | ||
pwdcommand = 'pwd'; | ||
dir = '/dev'; | ||
} | ||
|
||
|
||
{ | ||
const ac = new AbortController(); | ||
const signal = ac.signal; | ||
const promise = execPromisifed(pwdcommand, { cwd: dir, signal }); | ||
assert.rejects(promise, /AbortError/).then(common.mustCall()); | ||
ac.abort(); | ||
} | ||
|
||
{ | ||
assert.throws(() => { | ||
execPromisifed(pwdcommand, { cwd: dir, signal: {} }); | ||
}, invalidArgTypeError); | ||
} | ||
|
||
{ | ||
function signal() {} | ||
assert.throws(() => { | ||
execPromisifed(pwdcommand, { cwd: dir, signal }); | ||
}, invalidArgTypeError); | ||
} | ||
|
||
{ | ||
const ac = new AbortController(); | ||
const signal = (ac.abort(), ac.signal); | ||
const promise = execPromisifed(pwdcommand, { cwd: dir, signal }); | ||
|
||
assert.rejects(promise, /AbortError/).then(common.mustCall()); | ||
} |
59 changes: 59 additions & 0 deletions
59
test/parallel/test-child-process-execFile-promisified-abortController.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Flags: --experimental-abortcontroller | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { promisify } = require('util'); | ||
const execFile = require('child_process').execFile; | ||
const fixtures = require('../common/fixtures'); | ||
|
||
const echoFixture = fixtures.path('echo.js'); | ||
const promisified = promisify(execFile); | ||
const invalidArgTypeError = { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
name: 'TypeError' | ||
}; | ||
|
||
{ | ||
// Verify that the signal option works properly | ||
const ac = new AbortController(); | ||
const signal = ac.signal; | ||
const promise = promisified(process.execPath, [echoFixture, 0], { signal }); | ||
|
||
ac.abort(); | ||
|
||
assert.rejects( | ||
promise, | ||
{ name: 'AbortError' } | ||
).then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Verify that the signal option works properly when already aborted | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
ac.abort(); | ||
|
||
assert.rejects( | ||
promisified(process.execPath, [echoFixture, 0], { signal }), | ||
{ name: 'AbortError' } | ||
).then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Verify that if something different than Abortcontroller.signal | ||
// is passed, ERR_INVALID_ARG_TYPE is thrown | ||
const signal = {}; | ||
assert.throws(() => { | ||
promisified(process.execPath, [echoFixture, 0], { signal }); | ||
}, invalidArgTypeError); | ||
} | ||
|
||
{ | ||
// Verify that if something different than Abortcontroller.signal | ||
// is passed, ERR_INVALID_ARG_TYPE is thrown | ||
const signal = 'world!'; | ||
assert.throws(() => { | ||
promisified(process.execPath, [echoFixture, 0], { signal }); | ||
}, invalidArgTypeError); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters