• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --experimental-abortcontroller
2'use strict';
3
4const common = require('../common');
5const assert = require('assert');
6const execFile = require('child_process').execFile;
7const { getEventListeners } = require('events');
8const { getSystemErrorName } = require('util');
9const fixtures = require('../common/fixtures');
10
11const fixture = fixtures.path('exit.js');
12const echoFixture = fixtures.path('echo.js');
13const execOpts = { encoding: 'utf8', shell: true };
14
15{
16  execFile(
17    process.execPath,
18    [fixture, 42],
19    common.mustCall((e) => {
20      // Check that arguments are included in message
21      assert.strictEqual(e.message.trim(),
22                         `Command failed: ${process.execPath} ${fixture} 42`);
23      assert.strictEqual(e.code, 42);
24    })
25  );
26}
27
28{
29  // Verify that negative exit codes can be translated to UV error names.
30  const errorString = `Error: Command failed: ${process.execPath}`;
31  const code = -1;
32  const callback = common.mustCall((err, stdout, stderr) => {
33    assert.strictEqual(err.toString().trim(), errorString);
34    assert.strictEqual(err.code, getSystemErrorName(code));
35    assert.strictEqual(err.killed, true);
36    assert.strictEqual(err.signal, null);
37    assert.strictEqual(err.cmd, process.execPath);
38    assert.strictEqual(stdout.trim(), '');
39    assert.strictEqual(stderr.trim(), '');
40  });
41  const child = execFile(process.execPath, callback);
42
43  child.kill();
44  child.emit('close', code, null);
45}
46
47{
48  // Verify the shell option works properly
49  execFile(process.execPath, [fixture, 0], execOpts, common.mustSucceed());
50}
51
52{
53  // Verify that the signal option works properly
54  const ac = new AbortController();
55  const { signal } = ac;
56
57  const test = () => {
58    const check = common.mustCall((err) => {
59      assert.strictEqual(err.code, 'ABORT_ERR');
60      assert.strictEqual(err.name, 'AbortError');
61      assert.strictEqual(err.signal, undefined);
62    });
63    execFile(process.execPath, [echoFixture, 0], { signal }, check);
64  };
65
66  // Verify that it still works the same way now that the signal is aborted.
67  test();
68  ac.abort();
69}
70
71{
72  // Verify that does not spawn a child if already aborted
73  const ac = new AbortController();
74  const { signal } = ac;
75  ac.abort();
76
77  const check = common.mustCall((err) => {
78    assert.strictEqual(err.code, 'ABORT_ERR');
79    assert.strictEqual(err.name, 'AbortError');
80    assert.strictEqual(err.signal, undefined);
81  });
82  execFile(process.execPath, [echoFixture, 0], { signal }, check);
83}
84
85{
86  // Verify that if something different than Abortcontroller.signal
87  // is passed, ERR_INVALID_ARG_TYPE is thrown
88  assert.throws(() => {
89    const callback = common.mustNotCall(() => {});
90
91    execFile(process.execPath, [echoFixture, 0], { signal: 'hello' }, callback);
92  }, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });
93}
94{
95  // Verify that the process completing removes the abort listener
96  const ac = new AbortController();
97  const { signal } = ac;
98
99  const callback = common.mustCall((err) => {
100    assert.strictEqual(getEventListeners(ac.signal).length, 0);
101    assert.strictEqual(err, null);
102  });
103  execFile(process.execPath, [fixture, 0], { signal }, callback);
104}
105