Skip to content

Commit

Permalink
fixup! add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
debadree25 committed Jan 29, 2023
1 parent ee41163 commit 4312987
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions test/parallel/test-webstreams-finished.js
Expand Up @@ -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());
}

0 comments on commit 4312987

Please sign in to comment.