• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --no-warnings --expose-internals --experimental-abortcontroller
2'use strict';
3const common = require('../common');
4const assert = require('assert');
5const timers = require('timers');
6const { promisify } = require('util');
7
8// TODO(benjamingr) - refactor to use getEventListeners when #35991 lands
9const { NodeEventTarget } = require('internal/event_target');
10
11/* eslint-disable no-restricted-syntax */
12
13const setTimeout = promisify(timers.setTimeout);
14const setImmediate = promisify(timers.setImmediate);
15
16process.on('multipleResolves', common.mustNotCall());
17
18{
19  const promise = setTimeout(1);
20  promise.then(common.mustCall((value) => {
21    assert.strictEqual(value, undefined);
22  }));
23}
24
25{
26  const promise = setTimeout(1, 'foobar');
27  promise.then(common.mustCall((value) => {
28    assert.strictEqual(value, 'foobar');
29  }));
30}
31
32{
33  const promise = setImmediate();
34  promise.then(common.mustCall((value) => {
35    assert.strictEqual(value, undefined);
36  }));
37}
38
39{
40  const promise = setImmediate('foobar');
41  promise.then(common.mustCall((value) => {
42    assert.strictEqual(value, 'foobar');
43  }));
44}
45
46{
47  const ac = new AbortController();
48  const signal = ac.signal;
49  assert.rejects(setTimeout(10, undefined, { signal }), /AbortError/);
50  ac.abort();
51}
52
53{
54  const ac = new AbortController();
55  const signal = ac.signal;
56  ac.abort(); // Abort in advance
57  assert.rejects(setTimeout(10, undefined, { signal }), /AbortError/);
58}
59
60{
61  const ac = new AbortController();
62  const signal = ac.signal;
63  assert.rejects(setImmediate(10, { signal }), /AbortError/);
64  ac.abort();
65}
66
67{
68  const ac = new AbortController();
69  const signal = ac.signal;
70  ac.abort(); // Abort in advance
71  assert.rejects(setImmediate(10, { signal }), /AbortError/);
72}
73
74{
75  // Check that aborting after resolve will not reject.
76  const ac = new AbortController();
77  const signal = ac.signal;
78  setTimeout(10, undefined, { signal }).then(() => {
79    ac.abort();
80  });
81}
82{
83  // Check that aborting after resolve will not reject.
84  const ac = new AbortController();
85  const signal = ac.signal;
86  setImmediate(10, { signal }).then(() => {
87    ac.abort();
88  });
89}
90
91{
92  // Check that timer adding signals does not leak handlers
93  const signal = new NodeEventTarget();
94  signal.aborted = false;
95  setTimeout(0, null, { signal }).finally(common.mustCall(() => {
96    assert.strictEqual(signal.listenerCount('abort'), 0);
97  }));
98}
99
100{
101  // Check that timer adding signals does not leak handlers
102  const signal = new NodeEventTarget();
103  signal.aborted = false;
104  setImmediate(0, { signal }).finally(common.mustCall(() => {
105    assert.strictEqual(signal.listenerCount('abort'), 0);
106  }));
107}
108
109{
110  Promise.all(
111    [1, '', false, Infinity].map((i) => assert.rejects(setImmediate(10, i)), {
112      code: 'ERR_INVALID_ARG_TYPE'
113    })).then(common.mustCall());
114
115  Promise.all(
116    [1, '', false, Infinity, null, {}].map(
117      (signal) => assert.rejects(setImmediate(10, { signal })), {
118        code: 'ERR_INVALID_ARG_TYPE'
119      })).then(common.mustCall());
120
121  Promise.all(
122    [1, '', false, Infinity].map(
123      (i) => assert.rejects(setTimeout(10, null, i)), {
124        code: 'ERR_INVALID_ARG_TYPE'
125      })).then(common.mustCall());
126
127  Promise.all(
128    [1, '', false, Infinity, null, {}].map(
129      (signal) => assert.rejects(setTimeout(10, null, { signal })), {
130        code: 'ERR_INVALID_ARG_TYPE'
131      })).then(common.mustCall());
132
133  Promise.all(
134    [1, '', Infinity, null, {}].map(
135      (ref) => assert.rejects(setTimeout(10, null, { ref })), {
136        code: 'ERR_INVALID_ARG_TYPE'
137      })).then(common.mustCall());
138}
139