• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const exec = require('child_process').exec;
5const { promisify } = require('util');
6
7const execPromisifed = promisify(exec);
8const invalidArgTypeError = {
9  code: 'ERR_INVALID_ARG_TYPE',
10  name: 'TypeError'
11};
12
13const waitCommand = common.isWindows ?
14  `${process.execPath} -e "setInterval(()=>{}, 99)"` :
15  'sleep 2m';
16
17{
18  const ac = new AbortController();
19  const signal = ac.signal;
20  const promise = execPromisifed(waitCommand, { signal });
21  assert.rejects(promise, {
22    name: 'AbortError',
23    cause: new DOMException('This operation was aborted', 'AbortError'),
24  }).then(common.mustCall());
25  ac.abort();
26}
27
28{
29  const err = new Error('boom');
30  const ac = new AbortController();
31  const signal = ac.signal;
32  const promise = execPromisifed(waitCommand, { signal });
33  assert.rejects(promise, {
34    name: 'AbortError',
35    cause: err
36  }).then(common.mustCall());
37  ac.abort(err);
38}
39
40{
41  const ac = new AbortController();
42  const signal = ac.signal;
43  const promise = execPromisifed(waitCommand, { signal });
44  assert.rejects(promise, {
45    name: 'AbortError',
46    cause: 'boom'
47  }).then(common.mustCall());
48  ac.abort('boom');
49}
50
51{
52  assert.throws(() => {
53    execPromisifed(waitCommand, { signal: {} });
54  }, invalidArgTypeError);
55}
56
57{
58  function signal() {}
59  assert.throws(() => {
60    execPromisifed(waitCommand, { signal });
61  }, invalidArgTypeError);
62}
63
64{
65  const signal = AbortSignal.abort(); // Abort in advance
66  const promise = execPromisifed(waitCommand, { signal });
67
68  assert.rejects(promise, { name: 'AbortError' })
69        .then(common.mustCall());
70}
71
72{
73  const err = new Error('boom');
74  const signal = AbortSignal.abort(err); // Abort in advance
75  const promise = execPromisifed(waitCommand, { signal });
76
77  assert.rejects(promise, { name: 'AbortError', cause: err })
78        .then(common.mustCall());
79}
80
81{
82  const signal = AbortSignal.abort('boom'); // Abort in advance
83  const promise = execPromisifed(waitCommand, { signal });
84
85  assert.rejects(promise, { name: 'AbortError', cause: 'boom' })
86        .then(common.mustCall());
87}
88