Skip to content

Commit

Permalink
test: refactor code to use AbortSignal.abort()
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko committed Mar 18, 2021
1 parent a2fdef0 commit 722516a
Show file tree
Hide file tree
Showing 16 changed files with 27 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-child-process-execfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 2 additions & 6 deletions test/parallel/test-child-process-fork-abort-signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand All @@ -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',
Expand Down
5 changes: 1 addition & 4 deletions test/parallel/test-child-process-spawn-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-dgram-close-signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
10 changes: 4 additions & 6 deletions test/parallel/test-event-on-async-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
}
Expand Down
10 changes: 4 additions & 6 deletions test/parallel/test-events-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,16 @@ 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 }), {
code: 'ERR_INVALID_ARG_TYPE'
});
}));

return rejects(once(ee, 'foo', { signal: ac.signal }), {
return rejects(once(ee, 'foo', { signal: abortedSignal }), {
name: 'AbortError'
});
}
Expand Down Expand Up @@ -184,16 +183,15 @@ 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 }), {
code: 'ERR_INVALID_ARG_TYPE'
});
}));

return rejects(once(et, 'foo', { signal: ac.signal }), {
return rejects(once(et, 'foo', { signal: abortedSignal }), {
name: 'AbortError'
});
}
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-fs-promises-file-handle-readFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-fs-promises-readfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-fs-readfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}));
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-fs-watch-abort-signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
5 changes: 2 additions & 3 deletions test/parallel/test-net-server-listen-options-signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
4 changes: 1 addition & 3 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-stream-writable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
});

Expand Down
12 changes: 3 additions & 9 deletions test/parallel/test-timers-promisified.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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]();
Expand Down

0 comments on commit 722516a

Please sign in to comment.