From 722516a9b1948e5068f51329b99eb107e718d35b Mon Sep 17 00:00:00 2001 From: Wassim Chegham Date: Thu, 18 Mar 2021 17:52:08 +0100 Subject: [PATCH] test: refactor code to use AbortSignal.abort() Refs: whatwg/dom#960 --- ...child-process-exec-abortcontroller-promisified.js | 4 +--- ...d-process-execFile-promisified-abortController.js | 4 +--- test/parallel/test-child-process-execfile.js | 4 +--- .../parallel/test-child-process-fork-abort-signal.js | 8 ++------ test/parallel/test-child-process-spawn-controller.js | 5 +---- test/parallel/test-dgram-close-signal.js | 4 +--- test/parallel/test-event-on-async-iterator.js | 10 ++++------ test/parallel/test-events-once.js | 10 ++++------ .../test-fs-promises-file-handle-readFile.js | 4 +--- test/parallel/test-fs-promises-readfile.js | 4 +--- test/parallel/test-fs-readfile.js | 4 +--- test/parallel/test-fs-watch-abort-signal.js | 4 +--- .../test-net-server-listen-options-signal.js | 5 ++--- test/parallel/test-stream-pipeline.js | 4 +--- test/parallel/test-stream-writable-destroy.js | 5 ++--- test/parallel/test-timers-promisified.js | 12 +++--------- 16 files changed, 27 insertions(+), 64 deletions(-) diff --git a/test/parallel/test-child-process-exec-abortcontroller-promisified.js b/test/parallel/test-child-process-exec-abortcontroller-promisified.js index ac77d39938d8b6..a19b797ba6671d 100644 --- a/test/parallel/test-child-process-exec-abortcontroller-promisified.js +++ b/test/parallel/test-child-process-exec-abortcontroller-promisified.js @@ -37,9 +37,7 @@ const waitCommand = common.isLinux ? } { - const ac = new AbortController(); - const { signal } = ac; - ac.abort(); + const signal = AbortSignal.abort(); // Abort in advance const promise = execPromisifed(waitCommand, { signal }); assert.rejects(promise, /AbortError/, 'pre aborted signal failed') diff --git a/test/parallel/test-child-process-execFile-promisified-abortController.js b/test/parallel/test-child-process-execFile-promisified-abortController.js index ccfcafc5d51e58..38c177eb33389c 100644 --- a/test/parallel/test-child-process-execFile-promisified-abortController.js +++ b/test/parallel/test-child-process-execFile-promisified-abortController.js @@ -29,9 +29,7 @@ const invalidArgTypeError = { { // Verify that the signal option works properly when already aborted - const ac = new AbortController(); - const { signal } = ac; - ac.abort(); + const signal = AbortSignal.abort(); assert.rejects( promisified(process.execPath, [echoFixture, 0], { signal }), diff --git a/test/parallel/test-child-process-execfile.js b/test/parallel/test-child-process-execfile.js index a8f0cabacc2860..40cb8cd3afab01 100644 --- a/test/parallel/test-child-process-execfile.js +++ b/test/parallel/test-child-process-execfile.js @@ -69,9 +69,7 @@ const execOpts = { encoding: 'utf8', shell: true }; { // Verify that does not spawn a child if already aborted - const ac = new AbortController(); - const { signal } = ac; - ac.abort(); + const signal = AbortSignal.abort(); const check = common.mustCall((err) => { assert.strictEqual(err.code, 'ABORT_ERR'); diff --git a/test/parallel/test-child-process-fork-abort-signal.js b/test/parallel/test-child-process-fork-abort-signal.js index 93a968366270cc..9982444c429552 100644 --- a/test/parallel/test-child-process-fork-abort-signal.js +++ b/test/parallel/test-child-process-fork-abort-signal.js @@ -23,9 +23,7 @@ const { fork } = require('child_process'); } { // Test passing an already aborted signal to a forked child_process - const ac = new AbortController(); - const { signal } = ac; - ac.abort(); + const signal = AbortSignal.abort(); const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { signal }); @@ -40,9 +38,7 @@ const { fork } = require('child_process'); { // Test passing a different kill signal - const ac = new AbortController(); - const { signal } = ac; - ac.abort(); + const signal = AbortSignal.abort(); const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { signal, killSignal: 'SIGKILL', diff --git a/test/parallel/test-child-process-spawn-controller.js b/test/parallel/test-child-process-spawn-controller.js index 36c4112cbe00da..5199c7a09557c9 100644 --- a/test/parallel/test-child-process-spawn-controller.js +++ b/test/parallel/test-child-process-spawn-controller.js @@ -29,10 +29,7 @@ const aliveScript = fixtures.path('child-process-stay-alive-forever.js'); { // Verify that passing an already-aborted signal works. - const controller = new AbortController(); - const { signal } = controller; - - controller.abort(); + const signal = AbortSignal.abort(); const cp = spawn(process.execPath, [aliveScript], { signal, diff --git a/test/parallel/test-dgram-close-signal.js b/test/parallel/test-dgram-close-signal.js index f1008b599dbd18..26de38b504dd4a 100644 --- a/test/parallel/test-dgram-close-signal.js +++ b/test/parallel/test-dgram-close-signal.js @@ -25,9 +25,7 @@ const dgram = require('dgram'); { // Test close with pre-aborted signal. - const controller = new AbortController(); - controller.abort(); - const { signal } = controller; + const signal = AbortSignal.abort(); const server = dgram.createSocket({ type: 'udp4', signal }); server.on('close', common.mustCall()); } diff --git a/test/parallel/test-event-on-async-iterator.js b/test/parallel/test-event-on-async-iterator.js index 276a6f62d37e33..9ad319136fa493 100644 --- a/test/parallel/test-event-on-async-iterator.js +++ b/test/parallel/test-event-on-async-iterator.js @@ -248,28 +248,26 @@ async function nodeEventTarget() { async function abortableOnBefore() { const ee = new EventEmitter(); - const ac = new AbortController(); - ac.abort(); + const abortedSignal = AbortSignal.abort(); [1, {}, null, false, 'hi'].forEach((signal) => { assert.throws(() => on(ee, 'foo', { signal }), { code: 'ERR_INVALID_ARG_TYPE' }); }); - assert.throws(() => on(ee, 'foo', { signal: ac.signal }), { + assert.throws(() => on(ee, 'foo', { signal: abortedSignal }), { name: 'AbortError' }); } async function eventTargetAbortableOnBefore() { const et = new EventTarget(); - const ac = new AbortController(); - ac.abort(); + const abortedSignal = AbortSignal.abort(); [1, {}, null, false, 'hi'].forEach((signal) => { assert.throws(() => on(et, 'foo', { signal }), { code: 'ERR_INVALID_ARG_TYPE' }); }); - assert.throws(() => on(et, 'foo', { signal: ac.signal }), { + assert.throws(() => on(et, 'foo', { signal: abortedSignal }), { name: 'AbortError' }); } diff --git a/test/parallel/test-events-once.js b/test/parallel/test-events-once.js index 14b8ea5815a61a..56042b1ecee4f9 100644 --- a/test/parallel/test-events-once.js +++ b/test/parallel/test-events-once.js @@ -133,9 +133,8 @@ async function prioritizesEventEmitter() { async function abortSignalBefore() { const ee = new EventEmitter(); - const ac = new AbortController(); ee.on('error', common.mustNotCall()); - ac.abort(); + const abortedSignal = AbortSignal.abort(); await Promise.all([1, {}, 'hi', null, false].map((signal) => { return rejects(once(ee, 'foo', { signal }), { @@ -143,7 +142,7 @@ async function abortSignalBefore() { }); })); - return rejects(once(ee, 'foo', { signal: ac.signal }), { + return rejects(once(ee, 'foo', { signal: abortedSignal }), { name: 'AbortError' }); } @@ -184,8 +183,7 @@ async function abortSignalRemoveListener() { async function eventTargetAbortSignalBefore() { const et = new EventTarget(); - const ac = new AbortController(); - ac.abort(); + const abortedSignal = AbortSignal.abort(); await Promise.all([1, {}, 'hi', null, false].map((signal) => { return rejects(once(et, 'foo', { signal }), { @@ -193,7 +191,7 @@ async function eventTargetAbortSignalBefore() { }); })); - return rejects(once(et, 'foo', { signal: ac.signal }), { + return rejects(once(et, 'foo', { signal: abortedSignal }), { name: 'AbortError' }); } diff --git a/test/parallel/test-fs-promises-file-handle-readFile.js b/test/parallel/test-fs-promises-file-handle-readFile.js index 66dc8f0fb0e0a1..fc28dd23328df7 100644 --- a/test/parallel/test-fs-promises-file-handle-readFile.js +++ b/test/parallel/test-fs-promises-file-handle-readFile.js @@ -58,9 +58,7 @@ async function doReadAndCancel() { const fileHandle = await open(filePathForHandle, 'w+'); const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8'); fs.writeFileSync(filePathForHandle, buffer); - const controller = new AbortController(); - const { signal } = controller; - controller.abort(); + const signal = AbortSignal.abort(); await assert.rejects(readFile(fileHandle, { signal }), { name: 'AbortError' }); diff --git a/test/parallel/test-fs-promises-readfile.js b/test/parallel/test-fs-promises-readfile.js index d1c79580b835b6..a7fc46e5a6544a 100644 --- a/test/parallel/test-fs-promises-readfile.js +++ b/test/parallel/test-fs-promises-readfile.js @@ -43,9 +43,7 @@ async function validateReadFileProc() { } function validateReadFileAbortLogicBefore() { - const controller = new AbortController(); - const signal = controller.signal; - controller.abort(); + const signal = AbortSignal.abort(); assert.rejects(readFile(fn, { signal }), { name: 'AbortError' }); diff --git a/test/parallel/test-fs-readfile.js b/test/parallel/test-fs-readfile.js index b7d798a5db8e9b..a081eec099890d 100644 --- a/test/parallel/test-fs-readfile.js +++ b/test/parallel/test-fs-readfile.js @@ -54,9 +54,7 @@ for (const e of fileInfo) { } { // Test cancellation, before - const controller = new AbortController(); - const signal = controller.signal; - controller.abort(); + const signal = AbortSignal.abort(); fs.readFile(fileInfo[0].name, { signal }, common.mustCall((err, buf) => { assert.strictEqual(err.name, 'AbortError'); })); diff --git a/test/parallel/test-fs-watch-abort-signal.js b/test/parallel/test-fs-watch-abort-signal.js index cbe9a1457dc9ae..33936d2d491f8b 100644 --- a/test/parallel/test-fs-watch-abort-signal.js +++ b/test/parallel/test-fs-watch-abort-signal.js @@ -24,9 +24,7 @@ const fixtures = require('../common/fixtures'); { // Signal aborted before creating the watcher const file = fixtures.path('empty.js'); - const ac = new AbortController(); - const { signal } = ac; - ac.abort(); + const signal = AbortSignal.abort(); const watcher = fs.watch(file, { signal }); watcher.once('close', common.mustCall()); } diff --git a/test/parallel/test-net-server-listen-options-signal.js b/test/parallel/test-net-server-listen-options-signal.js index 80efaf1700cd75..60e78fa00c5080 100644 --- a/test/parallel/test-net-server-listen-options-signal.js +++ b/test/parallel/test-net-server-listen-options-signal.js @@ -26,8 +26,7 @@ const net = require('net'); { // Test close with pre-aborted signal. const server = net.createServer(); - const controller = new AbortController(); - controller.abort(); + const signal = AbortSignal.abort(); server.on('close', common.mustCall()); - server.listen({ port: 0, signal: controller.signal }); + server.listen({ port: 0, signal }); } diff --git a/test/parallel/test-stream-pipeline.js b/test/parallel/test-stream-pipeline.js index 3ad8d82e785b95..a12fd20788eca9 100644 --- a/test/parallel/test-stream-pipeline.js +++ b/test/parallel/test-stream-pipeline.js @@ -487,9 +487,7 @@ const net = require('net'); // Check pre-aborted signal const pipelinePromise = promisify(pipeline); async function run() { - const ac = new AbortController(); - const { signal } = ac; - ac.abort(); + const signal = AbortSignal.abort(); async function* producer() { yield '5'; await Promise.resolve(); diff --git a/test/parallel/test-stream-writable-destroy.js b/test/parallel/test-stream-writable-destroy.js index 9859c59e446c95..7cd800b0938138 100644 --- a/test/parallel/test-stream-writable-destroy.js +++ b/test/parallel/test-stream-writable-destroy.js @@ -448,11 +448,10 @@ const assert = require('assert'); } { - const ac = new AbortController(); - ac.abort(); + const signal = AbortSignal.abort(); const write = new Writable({ - signal: ac.signal, + signal, write(chunk, enc, cb) { cb(); } }); diff --git a/test/parallel/test-timers-promisified.js b/test/parallel/test-timers-promisified.js index 04c3ff96149352..45da5036727061 100644 --- a/test/parallel/test-timers-promisified.js +++ b/test/parallel/test-timers-promisified.js @@ -98,9 +98,7 @@ process.on('multipleResolves', common.mustNotCall()); } { - const ac = new AbortController(); - const signal = ac.signal; - ac.abort(); // Abort in advance + const signal = AbortSignal.abort(); // Abort in advance assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/) .then(common.mustCall()); } @@ -114,17 +112,13 @@ process.on('multipleResolves', common.mustNotCall()); } { - const ac = new AbortController(); - const signal = ac.signal; - ac.abort(); // Abort in advance + const signal = AbortSignal.abort(); // Abort in advance assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/) .then(common.mustCall()); } { - const ac = new AbortController(); - const { signal } = ac; - ac.abort(); // Abort in advance + const signal = AbortSignal.abort(); // Abort in advance const iterable = setInterval(1, undefined, { signal }); const iterator = iterable[Symbol.asyncIterator]();