Repository navigation
stream: keep async context of the pipeTo pump #67054
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
Open
mcollina
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
mcollina:webstreams-pipeto-async-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
test/parallel/test-whatwg-readablestream-pipeto-async-context.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| 'use strict'; | ||
|
|
||
| // The pipeTo() pump must keep running in the async context of the | ||
| // pipeTo() call when it resumes after backpressure or after a chunk | ||
| // arrives, instead of taking the context of the code that cleared the | ||
| // backpressure or enqueued the chunk. The source pulls and the writes the | ||
| // pump issues are checked. | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const { AsyncLocalStorage } = require('async_hooks'); | ||
| const { setImmediate: tick } = require('timers/promises'); | ||
|
|
||
| const als = new AsyncLocalStorage(); | ||
|
|
||
| function store() { | ||
| return als.getStore() ?? 'none'; | ||
| } | ||
|
|
||
| // Settles the pending callbacks from the given context until `promise` | ||
| // settles. | ||
| async function settleUntil(promise, ctx, pending) { | ||
| let finished = false; | ||
| promise.then(common.mustCall(() => { finished = true; })); | ||
| while (!finished) { | ||
| await tick(); | ||
| while (pending.length > 0) als.run(ctx, pending.shift()); | ||
| } | ||
| await promise; | ||
| } | ||
|
|
||
| const tests = []; | ||
| function test(name, expected, fn) { | ||
| tests.push({ name, expected, fn }); | ||
| } | ||
|
|
||
| test('sink writes resolved from another context', [ | ||
| // The first pull and write follow the source's start step. | ||
| 'pull:creator', 'write-1:creator', | ||
| 'pull:piper', 'write-2:piper', | ||
| 'pull:piper', 'write-3:piper', | ||
| 'pull:piper', | ||
| ], async (seen) => { | ||
| let n = 0; | ||
| const pending = []; | ||
| const rs = als.run('creator', () => new ReadableStream({ | ||
| pull(c) { | ||
| seen.push(`pull:${store()}`); | ||
| if (++n > 3) c.close(); else c.enqueue(n); | ||
| }, | ||
| }, { highWaterMark: 0 })); | ||
| const ws = als.run('creator', () => new WritableStream({ | ||
| write(chunk) { | ||
| seen.push(`write-${chunk}:${store()}`); | ||
| const { promise, resolve } = Promise.withResolvers(); | ||
| pending.push(resolve); | ||
| return promise; | ||
| }, | ||
| }, { highWaterMark: 1 })); | ||
| await settleUntil(als.run('piper', () => rs.pipeTo(ws)), 'releaser', pending); | ||
| }); | ||
|
|
||
| test('push source under backpressure', [ | ||
| 'write-a:pusher-a', 'write-b:piper', 'write-c:piper', 'write-d:piper', | ||
| ], async (seen) => { | ||
| let controller; | ||
| const pending = []; | ||
| const rs = als.run('creator', () => new ReadableStream({ | ||
| start(c) { controller = c; }, | ||
| }, { highWaterMark: 0 })); | ||
| const ws = als.run('creator', () => new WritableStream({ | ||
| write(chunk) { | ||
| seen.push(`write-${chunk}:${store()}`); | ||
| const { promise, resolve } = Promise.withResolvers(); | ||
| pending.push(resolve); | ||
| return promise; | ||
| }, | ||
| }, { highWaterMark: 1 })); | ||
| const piped = als.run('piper', () => rs.pipeTo(ws)); | ||
| await tick(); | ||
| for (const chunk of 'abcd') | ||
| als.run(`pusher-${chunk}`, () => controller.enqueue(chunk)); | ||
| als.run('closer', () => controller.close()); | ||
| await settleUntil(piped, 'releaser', pending); | ||
| }); | ||
|
|
||
| test('pipeThrough() with pulls resolved from another context', [ | ||
| 'pull:piper', 'transform-1:resolver', | ||
| 'pull:piper', 'transform-2:resolver', | ||
| 'pull:piper', 'transform-3:resolver', | ||
| 'pull:piper', | ||
| ], async (seen) => { | ||
| let n = 0; | ||
| const pending = []; | ||
| const readable = als.run('piper', () => new ReadableStream({ | ||
| pull(c) { | ||
| seen.push(`pull:${store()}`); | ||
| const { promise, resolve } = Promise.withResolvers(); | ||
| pending.push(() => { | ||
| if (++n > 3) c.close(); else c.enqueue(n); | ||
| resolve(); | ||
| }); | ||
| return promise; | ||
| }, | ||
| }, { highWaterMark: 0 }).pipeThrough(new TransformStream({ | ||
| transform(chunk, c) { | ||
| seen.push(`transform-${chunk}:${store()}`); | ||
| c.enqueue(chunk); | ||
| }, | ||
| }))); | ||
| const consumed = als.run('reader', async () => { | ||
| // eslint-disable-next-line no-unused-vars | ||
| for await (const _ of readable); | ||
| }); | ||
| await settleUntil(consumed, 'resolver', pending); | ||
| }); | ||
|
|
||
| test('pipeThrough() chain into a slow sink', [ | ||
| 'pull:creator', 't1-1:piper', 't2-1:piper', 'write-1:piper', | ||
| 'pull:piper', 't1-2:piper', 't2-2:piper', 'write-2:piper', | ||
| 'pull:piper', 't1-3:piper', 't2-3:piper', 'write-3:piper', | ||
| 'pull:piper', | ||
| ], async (seen) => { | ||
| let n = 0; | ||
| function transform(name) { | ||
| return new TransformStream({ | ||
| transform(chunk, c) { | ||
| seen.push(`${name}-${chunk}:${store()}`); | ||
| c.enqueue(chunk); | ||
| }, | ||
| }); | ||
| } | ||
| const rs = als.run('creator', () => new ReadableStream({ | ||
| pull(c) { | ||
| seen.push(`pull:${store()}`); | ||
| if (++n > 3) c.close(); else c.enqueue(n); | ||
| }, | ||
| }, { highWaterMark: 0 })); | ||
| const ws = als.run('creator', () => new WritableStream({ | ||
| async write(chunk) { | ||
| seen.push(`write-${chunk}:${store()}`); | ||
| await tick(); | ||
| }, | ||
| }, { highWaterMark: 1 })); | ||
| await als.run('piper', () => rs.pipeThrough(transform('t1')) | ||
| .pipeThrough(transform('t2')).pipeTo(ws)); | ||
| }); | ||
|
|
||
| (async () => { | ||
| for (const { name, expected, fn } of tests) { | ||
| const seen = []; | ||
| await fn(seen); | ||
| // Only the context of each callback is checked, not how the callbacks | ||
| // of different streams interleave. | ||
| assert.deepStrictEqual(seen.sort(), expected.toSorted(), name); | ||
| } | ||
| })().then(common.mustCall()); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or we should be using
.finally