Skip to content

Commit dbbc790

Browse 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

File tree

1 file changed

+64
-29
lines changed

1 file changed

+64
-29
lines changed

test/parallel/test-aborted-util.js

+64-29
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)