summaryrefslogtreecommitdiff
path: root/test/parallel/test-aborted-util.js
blob: 7991d72cb6c62fe918157d84e8d963caa97f15b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Flags: --expose-gc
'use strict';

const common = require('../common');
const { aborted } = require('util');
const assert = require('assert');
const { getEventListeners } = require('events');
const { spawn } = require('child_process');

{
  // Test aborted works when provided a resource
  const ac = new AbortController();
  aborted(ac.signal, {}).then(common.mustCall());
  ac.abort();
  assert.strictEqual(ac.signal.aborted, true);
  assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
}

{
  // Test aborted with gc cleanup
  const ac = new AbortController();
  aborted(ac.signal, {}).then(common.mustNotCall());
  setImmediate(() => {
    global.gc();
    ac.abort();
    assert.strictEqual(ac.signal.aborted, true);
    assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
  });
}

{
  // Fails with error if not provided abort signal
  Promise.all([{}, null, undefined, Symbol(), [], 1, 0, 1n, true, false, 'a', () => {}].map((sig) =>
    assert.rejects(aborted(sig, {}), {
      code: 'ERR_INVALID_ARG_TYPE',
    })
  )).then(common.mustCall());
}

{
  // Fails if not provided a resource
  const ac = new AbortController();
  Promise.all([null, undefined, 0, 1, 0n, 1n, Symbol(), '', 'a'].map((resource) =>
    assert.rejects(aborted(ac.signal, resource), {
      code: 'ERR_INVALID_ARG_TYPE',
    })
  )).then(common.mustCall());
}

{
  const childProcess = spawn(process.execPath, ['--input-type=module']);
  childProcess.on('exit', common.mustCall((code) => {
    assert.strictEqual(code, 13);
  }));
  childProcess.stdin.end(`
    import { aborted } from 'node:util';
    await aborted(new AbortController().signal, {});
  `);
}