Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dbbc790

Browse filesBrowse files
jasnellRafaelGSS
authored andcommitted
test: update test-aborted-util to use node:test
PR-URL: #54578 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 64442fc commit dbbc790
Copy full SHA for dbbc790

File tree

Expand file treeCollapse file tree

1 file changed

+64
-29
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+64
-29
lines changed
Open diff view settings
Collapse file

‎test/parallel/test-aborted-util.js‎

Copy file name to clipboard
+64-29Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,94 @@
11
// Flags: --expose-gc
22
'use strict';
33

4-
const common = require('../common');
4+
require('../common');
55
const { aborted } = require('util');
6-
const assert = require('assert');
6+
const {
7+
match,
8+
rejects,
9+
strictEqual,
10+
} = require('assert');
711
const { getEventListeners } = require('events');
8-
const { spawn } = require('child_process');
12+
const { inspect } = require('util');
913

10-
{
11-
// Test aborted works when provided a resource
14+
const {
15+
test,
16+
} = require('node:test');
17+
18+
test('Aborted works when provided a resource', async () => {
1219
const ac = new AbortController();
13-
aborted(ac.signal, {}).then(common.mustCall());
20+
const promise = aborted(ac.signal, {});
1421
ac.abort();
15-
assert.strictEqual(ac.signal.aborted, true);
16-
assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
17-
}
22+
await promise;
23+
strictEqual(ac.signal.aborted, true);
24+
strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
25+
});
1826

19-
{
27+
test('Aborted with gc cleanup', async () => {
2028
// Test aborted with gc cleanup
2129
const ac = new AbortController();
22-
aborted(ac.signal, {}).then(common.mustNotCall());
30+
31+
const abortedPromise = aborted(ac.signal, {});
32+
const { promise, resolve } = Promise.withResolvers();
33+
2334
setImmediate(() => {
2435
global.gc();
2536
ac.abort();
26-
assert.strictEqual(ac.signal.aborted, true);
27-
assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
37+
strictEqual(ac.signal.aborted, true);
38+
strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
39+
resolve();
2840
});
29-
}
3041

31-
{
32-
// Fails with error if not provided abort signal
33-
Promise.all([{}, null, undefined, Symbol(), [], 1, 0, 1n, true, false, 'a', () => {}].map((sig) =>
34-
assert.rejects(aborted(sig, {}), {
42+
await promise;
43+
44+
// Ensure that the promise is still pending
45+
match(inspect(abortedPromise), /<pending>/);
46+
});
47+
48+
test('Fails with error if not provided AbortSignal', async () => {
49+
await Promise.all([{}, null, undefined, Symbol(), [], 1, 0, 1n, true, false, 'a', () => {}].map((sig) =>
50+
rejects(aborted(sig, {}), {
3551
code: 'ERR_INVALID_ARG_TYPE',
3652
})
37-
)).then(common.mustCall());
38-
}
53+
));
54+
});
3955

40-
{
56+
test('Fails if not provided a resource', async () => {
4157
// Fails if not provided a resource
4258
const ac = new AbortController();
43-
Promise.all([null, undefined, 0, 1, 0n, 1n, Symbol(), '', 'a'].map((resource) =>
44-
assert.rejects(aborted(ac.signal, resource), {
59+
await Promise.all([null, undefined, 0, 1, 0n, 1n, Symbol(), '', 'a'].map((resource) =>
60+
rejects(aborted(ac.signal, resource), {
4561
code: 'ERR_INVALID_ARG_TYPE',
4662
})
47-
)).then(common.mustCall());
63+
));
64+
});
65+
66+
// To allow this case to be more flexibly tested on runtimes that do not have
67+
// child_process.spawn, we lazily require it and skip the test if it is not
68+
// present.
69+
70+
let spawn;
71+
function lazySpawn() {
72+
if (spawn === undefined) {
73+
try {
74+
spawn = require('child_process').spawn;
75+
} catch {
76+
// Ignore if spawn does not exist.
77+
}
78+
}
79+
return spawn;
4880
}
4981

50-
{
82+
test('Does not hang forever', { skip: !lazySpawn() }, async () => {
83+
const { promise, resolve } = Promise.withResolvers();
5184
const childProcess = spawn(process.execPath, ['--input-type=module']);
52-
childProcess.on('exit', common.mustCall((code) => {
53-
assert.strictEqual(code, 13);
54-
}));
85+
childProcess.on('exit', (code) => {
86+
strictEqual(code, 13);
87+
resolve();
88+
});
5589
childProcess.stdin.end(`
5690
import { aborted } from 'node:util';
5791
await aborted(new AbortController().signal, {});
5892
`);
59-
}
93+
await promise;
94+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.