Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: allow infinite concurrency #48588

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/internal/streams/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ function map(fn, options) {
concurrency = MathFloor(options.concurrency);
}

validateInteger(concurrency, 'concurrency', 1);
if (concurrency !== Infinity) {
validateInteger(concurrency, 'concurrency', 1);
}

return async function* map() {
const ac = new AbortController();
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-stream-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,31 @@ const { setTimeout } = require('timers/promises');
});
}

{
// Allow Infinite concurrency
let resolve;
const promise = new Promise((res) => {
resolve = res;
});
const stream = Readable.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).map(async (item, { signal }) => {
await setTimeout(10 - item, { signal });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this asserts concurrency works

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test meant to make sure that concurrency: Infinity does not throw an error, but you are right, it does not test that concurrency works, fixed it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a timer here? Can't this just be something like:

let count = 0; // or t.mock
Readable.from(Array(1000).fill()).map(() => {
  count++;
  return new Promise(() => {});
}, { concurrency: Infinity }).toArray().catch(common.mustNotCall());

await setImmediate(); // from timers/promises
strictEqual(count, 1000);

if (item === 10) {
resolve();
} else {
await promise;
}

return item;
}, { concurrency: Infinity });

(async () => {
const expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for await (const item of stream) {
assert.strictEqual(item, expected.shift());
}
})().then(common.mustCall());
}

{
// Concurrency result order
const stream = Readable.from([1, 2]).map(async (item, { signal }) => {
Expand Down