node/test/parallel/test-stream-readable-strategy-option.js
Antoine du Hamel e50cbc1abd
test: enforce better never-settling-promise detection
Tests should be explicit regarding whether a promise is expected to
settle, and the test should fail when the behavior does not meet
expectations.

PR-URL: https://github.com/nodejs/node/pull/60976
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Erick Wendel <erick.workspace@gmail.com>
2025-12-10 23:55:36 +00:00

75 lines
2.0 KiB
JavaScript

'use strict';
const common = require('../common');
const { Readable } = require('stream');
const assert = require('assert');
{
// Strategy 2
const streamData = ['a', 'b', 'c', null];
// Fulfill a Readable object
const readable = new Readable({
read: common.mustCall(() => {
process.nextTick(() => {
readable.push(streamData.shift());
});
}, streamData.length),
});
// Use helper to convert it to a Web ReadableStream using ByteLength strategy
const readableStream = Readable.toWeb(readable, {
strategy: new ByteLengthQueuingStrategy({ highWaterMark: 1 }),
});
assert(!readableStream.locked);
readableStream.getReader().read().then(common.mustCall());
}
{
// Strategy 2
const streamData = ['a', 'b', 'c', null];
// Fulfill a Readable object
const readable = new Readable({
read: common.mustCall(() => {
process.nextTick(() => {
readable.push(streamData.shift());
});
}, streamData.length),
});
// Use helper to convert it to a Web ReadableStream using Count strategy
const readableStream = Readable.toWeb(readable, {
strategy: new CountQueuingStrategy({ highWaterMark: 1 }),
});
assert(!readableStream.locked);
readableStream.getReader().read().then(common.mustCall());
}
{
const desireSizeExpected = 2;
const stringStream = new ReadableStream(
{
start: common.mustCall((controller) => {
// Check if the strategy is being assigned on the init of the ReadableStream
assert.strictEqual(controller.desiredSize, desireSizeExpected);
controller.enqueue('a');
controller.enqueue('b');
controller.close();
}),
},
new CountQueuingStrategy({ highWaterMark: desireSizeExpected })
);
const reader = stringStream.getReader();
reader.read().then(common.mustCall());
reader.read().then(common.mustCall());
reader.read().then(({ value, done }) => {
assert.strictEqual(value, undefined);
assert.strictEqual(done, true);
}).then(common.mustCall());
}