Skip to content

Commit e85f311

Browse files
manekinekkoruyadorno
authored andcommitted
test: refactor code to use AbortSignal.abort()
PR-URL: #37798 Refs: whatwg/dom#960 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Zijian Liu <[email protected]> Reviewed-By: Zeyu Yang <[email protected]>
1 parent 3376051 commit e85f311

16 files changed

+27
-64
lines changed

test/parallel/test-child-process-exec-abortcontroller-promisified.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ const waitCommand = common.isLinux ?
3737
}
3838

3939
{
40-
const ac = new AbortController();
41-
const { signal } = ac;
42-
ac.abort();
40+
const signal = AbortSignal.abort(); // Abort in advance
4341
const promise = execPromisifed(waitCommand, { signal });
4442

4543
assert.rejects(promise, /AbortError/, 'pre aborted signal failed')

test/parallel/test-child-process-execFile-promisified-abortController.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ const invalidArgTypeError = {
2929

3030
{
3131
// Verify that the signal option works properly when already aborted
32-
const ac = new AbortController();
33-
const { signal } = ac;
34-
ac.abort();
32+
const signal = AbortSignal.abort();
3533

3634
assert.rejects(
3735
promisified(process.execPath, [echoFixture, 0], { signal }),

test/parallel/test-child-process-execfile.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ const execOpts = { encoding: 'utf8', shell: true };
6969

7070
{
7171
// Verify that does not spawn a child if already aborted
72-
const ac = new AbortController();
73-
const { signal } = ac;
74-
ac.abort();
72+
const signal = AbortSignal.abort();
7573

7674
const check = common.mustCall((err) => {
7775
assert.strictEqual(err.code, 'ABORT_ERR');

test/parallel/test-child-process-fork-abort-signal.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ const { fork } = require('child_process');
2323
}
2424
{
2525
// Test passing an already aborted signal to a forked child_process
26-
const ac = new AbortController();
27-
const { signal } = ac;
28-
ac.abort();
26+
const signal = AbortSignal.abort();
2927
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
3028
signal
3129
});
@@ -40,9 +38,7 @@ const { fork } = require('child_process');
4038

4139
{
4240
// Test passing a different kill signal
43-
const ac = new AbortController();
44-
const { signal } = ac;
45-
ac.abort();
41+
const signal = AbortSignal.abort();
4642
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
4743
signal,
4844
killSignal: 'SIGKILL',

test/parallel/test-child-process-spawn-controller.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ const aliveScript = fixtures.path('child-process-stay-alive-forever.js');
2929

3030
{
3131
// Verify that passing an already-aborted signal works.
32-
const controller = new AbortController();
33-
const { signal } = controller;
34-
35-
controller.abort();
32+
const signal = AbortSignal.abort();
3633

3734
const cp = spawn(process.execPath, [aliveScript], {
3835
signal,

test/parallel/test-dgram-close-signal.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ const dgram = require('dgram');
2525

2626
{
2727
// Test close with pre-aborted signal.
28-
const controller = new AbortController();
29-
controller.abort();
30-
const { signal } = controller;
28+
const signal = AbortSignal.abort();
3129
const server = dgram.createSocket({ type: 'udp4', signal });
3230
server.on('close', common.mustCall());
3331
}

test/parallel/test-event-on-async-iterator.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,28 +248,26 @@ async function nodeEventTarget() {
248248

249249
async function abortableOnBefore() {
250250
const ee = new EventEmitter();
251-
const ac = new AbortController();
252-
ac.abort();
251+
const abortedSignal = AbortSignal.abort();
253252
[1, {}, null, false, 'hi'].forEach((signal) => {
254253
assert.throws(() => on(ee, 'foo', { signal }), {
255254
code: 'ERR_INVALID_ARG_TYPE'
256255
});
257256
});
258-
assert.throws(() => on(ee, 'foo', { signal: ac.signal }), {
257+
assert.throws(() => on(ee, 'foo', { signal: abortedSignal }), {
259258
name: 'AbortError'
260259
});
261260
}
262261

263262
async function eventTargetAbortableOnBefore() {
264263
const et = new EventTarget();
265-
const ac = new AbortController();
266-
ac.abort();
264+
const abortedSignal = AbortSignal.abort();
267265
[1, {}, null, false, 'hi'].forEach((signal) => {
268266
assert.throws(() => on(et, 'foo', { signal }), {
269267
code: 'ERR_INVALID_ARG_TYPE'
270268
});
271269
});
272-
assert.throws(() => on(et, 'foo', { signal: ac.signal }), {
270+
assert.throws(() => on(et, 'foo', { signal: abortedSignal }), {
273271
name: 'AbortError'
274272
});
275273
}

test/parallel/test-events-once.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,16 @@ async function prioritizesEventEmitter() {
133133

134134
async function abortSignalBefore() {
135135
const ee = new EventEmitter();
136-
const ac = new AbortController();
137136
ee.on('error', common.mustNotCall());
138-
ac.abort();
137+
const abortedSignal = AbortSignal.abort();
139138

140139
await Promise.all([1, {}, 'hi', null, false].map((signal) => {
141140
return rejects(once(ee, 'foo', { signal }), {
142141
code: 'ERR_INVALID_ARG_TYPE'
143142
});
144143
}));
145144

146-
return rejects(once(ee, 'foo', { signal: ac.signal }), {
145+
return rejects(once(ee, 'foo', { signal: abortedSignal }), {
147146
name: 'AbortError'
148147
});
149148
}
@@ -184,16 +183,15 @@ async function abortSignalRemoveListener() {
184183

185184
async function eventTargetAbortSignalBefore() {
186185
const et = new EventTarget();
187-
const ac = new AbortController();
188-
ac.abort();
186+
const abortedSignal = AbortSignal.abort();
189187

190188
await Promise.all([1, {}, 'hi', null, false].map((signal) => {
191189
return rejects(once(et, 'foo', { signal }), {
192190
code: 'ERR_INVALID_ARG_TYPE'
193191
});
194192
}));
195193

196-
return rejects(once(et, 'foo', { signal: ac.signal }), {
194+
return rejects(once(et, 'foo', { signal: abortedSignal }), {
197195
name: 'AbortError'
198196
});
199197
}

test/parallel/test-fs-promises-file-handle-readFile.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ async function doReadAndCancel() {
5858
const fileHandle = await open(filePathForHandle, 'w+');
5959
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
6060
fs.writeFileSync(filePathForHandle, buffer);
61-
const controller = new AbortController();
62-
const { signal } = controller;
63-
controller.abort();
61+
const signal = AbortSignal.abort();
6462
await assert.rejects(readFile(fileHandle, { signal }), {
6563
name: 'AbortError'
6664
});

test/parallel/test-fs-promises-readfile.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ async function validateReadFileProc() {
4343
}
4444

4545
function validateReadFileAbortLogicBefore() {
46-
const controller = new AbortController();
47-
const signal = controller.signal;
48-
controller.abort();
46+
const signal = AbortSignal.abort();
4947
assert.rejects(readFile(fn, { signal }), {
5048
name: 'AbortError'
5149
});

0 commit comments

Comments
 (0)