From 43129876236c764c6ebd5e709ec44d324bfe4cf3 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Sun, 29 Jan 2023 10:52:07 +0530 Subject: [PATCH] fixup! add tests --- test/parallel/test-webstreams-finished.js | 90 +++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/test/parallel/test-webstreams-finished.js b/test/parallel/test-webstreams-finished.js index 65a14d863eb922..2a19c1ebae36d0 100644 --- a/test/parallel/test-webstreams-finished.js +++ b/test/parallel/test-webstreams-finished.js @@ -230,3 +230,93 @@ const { finished: finishedPromise } = require('stream/promises'); assert.strictEqual(err?.message, 'asd'); }); } + +{ + // Check pre-cancelled + const signal = new EventTarget(); + signal.aborted = true; + + const rs = new ReadableStream({ + start() {} + }); + finished(rs, { signal }, common.mustCall((err) => { + assert.strictEqual(err.name, 'AbortError'); + })); +} + +{ + // Check cancelled before the stream ends sync. + const ac = new AbortController(); + const { signal } = ac; + + const rs = new ReadableStream({ + start() {} + }); + finished(rs, { signal }, common.mustCall((err) => { + assert.strictEqual(err.name, 'AbortError'); + })); + + ac.abort(); +} + +{ + // Check cancelled before the stream ends async. + const ac = new AbortController(); + const { signal } = ac; + + const rs = new ReadableStream({ + start() {} + }); + setTimeout(() => ac.abort(), 1); + finished(rs, { signal }, common.mustCall((err) => { + assert.strictEqual(err.name, 'AbortError'); + })); +} + +{ + // Check cancelled after doesn't throw. + const ac = new AbortController(); + const { signal } = ac; + + const rs = new ReadableStream({ + start(controller) { + controller.enqueue('asd'); + controller.close(); + } + }); + finished(rs, { signal }, common.mustSucceed()); + + rs.getReader().read().then(common.mustCall((chunk) => { + assert.strictEqual(chunk.value, 'asd'); + setImmediate(() => ac.abort()); + })); +} + +{ + // Promisified abort works + async function run() { + const ac = new AbortController(); + const { signal } = ac; + const rs = new ReadableStream({ + start() {} + }); + setImmediate(() => ac.abort()); + await finishedPromise(rs, { signal }); + } + + assert.rejects(run, { name: 'AbortError' }).then(common.mustCall()); +} + +{ + // Promisified pre-aborted works + async function run() { + const signal = new EventTarget(); + signal.aborted = true; + const rs = new ReadableStream({ + start() {} + }); + await finishedPromise(rs, { signal }); + } + + assert.rejects(run, { name: 'AbortError' }).then(common.mustCall()); +}