-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Streams: tests for Transformer.cancel
#40453
Merged
lucacasonato
merged 7 commits into
web-platform-tests:master
from
lucacasonato:TransformStream_cancel
Sep 29, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff3c698
Streams: tests for `Transformer.cancel`
lucacasonato 4156716
Version 2: run cancel before cancelling
lucacasonato 1f2f6c8
Fixes
lucacasonato 1fbae6a
Add semicolons
MattiasBuelens 9fa9a0c
Add shadowrealm
domenic ab3f840
lint
lucacasonato a169eb4
Merge remote-tracking branch 'origin/master' into TransformStream_cancel
lucacasonato 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 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,115 @@ | ||
// META: global=window,worker,shadowrealm | ||
// META: script=../resources/test-utils.js | ||
'use strict'; | ||
|
||
const thrownError = new Error('bad things are happening!'); | ||
thrownError.name = 'error1'; | ||
|
||
const originalReason = new Error('original reason'); | ||
originalReason.name = 'error2'; | ||
|
||
promise_test(async t => { | ||
let cancelled = undefined; | ||
const ts = new TransformStream({ | ||
cancel(reason) { | ||
cancelled = reason; | ||
} | ||
}); | ||
const res = await ts.readable.cancel(thrownError); | ||
assert_equals(res, undefined, 'readable.cancel() should return undefined'); | ||
assert_equals(cancelled, thrownError, 'transformer.cancel() should be called with the passed reason'); | ||
}, 'cancelling the readable side should call transformer.cancel()'); | ||
|
||
promise_test(async t => { | ||
const ts = new TransformStream({ | ||
cancel(reason) { | ||
assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason'); | ||
throw thrownError; | ||
} | ||
}); | ||
const writer = ts.writable.getWriter(); | ||
const cancelPromise = ts.readable.cancel(originalReason); | ||
await promise_rejects_exactly(t, thrownError, cancelPromise, 'readable.cancel() should reject with thrownError'); | ||
await promise_rejects_exactly(t, thrownError, writer.closed, 'writer.closed should reject with thrownError'); | ||
}, 'cancelling the readable side should reject if transformer.cancel() throws'); | ||
|
||
promise_test(async t => { | ||
let aborted = undefined; | ||
const ts = new TransformStream({ | ||
cancel(reason) { | ||
aborted = reason; | ||
}, | ||
flush: t.unreached_func('flush should not be called') | ||
}); | ||
const res = await ts.writable.abort(thrownError); | ||
assert_equals(res, undefined, 'writable.abort() should return undefined'); | ||
assert_equals(aborted, thrownError, 'transformer.abort() should be called with the passed reason'); | ||
}, 'aborting the writable side should call transformer.abort()'); | ||
|
||
promise_test(async t => { | ||
const ts = new TransformStream({ | ||
cancel(reason) { | ||
assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason'); | ||
throw thrownError; | ||
}, | ||
flush: t.unreached_func('flush should not be called') | ||
}); | ||
const reader = ts.readable.getReader(); | ||
const abortPromise = ts.writable.abort(originalReason); | ||
await promise_rejects_exactly(t, thrownError, abortPromise, 'writable.abort() should reject with thrownError'); | ||
await promise_rejects_exactly(t, thrownError, reader.closed, 'reader.closed should reject with thrownError'); | ||
}, 'aborting the writable side should reject if transformer.cancel() throws'); | ||
|
||
promise_test(async t => { | ||
const ts = new TransformStream({ | ||
async cancel(reason) { | ||
assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason'); | ||
throw thrownError; | ||
}, | ||
flush: t.unreached_func('flush should not be called') | ||
}); | ||
const cancelPromise = ts.readable.cancel(originalReason); | ||
const closePromise = ts.writable.close(); | ||
await Promise.all([ | ||
promise_rejects_exactly(t, thrownError, cancelPromise, 'cancelPromise should reject with thrownError'), | ||
promise_rejects_exactly(t, thrownError, closePromise, 'closePromise should reject with thrownError'), | ||
]); | ||
}, 'closing the writable side should reject if a parallel transformer.cancel() throws'); | ||
|
||
promise_test(async t => { | ||
let controller; | ||
const ts = new TransformStream({ | ||
start(c) { | ||
controller = c; | ||
}, | ||
async cancel(reason) { | ||
assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason'); | ||
controller.error(thrownError); | ||
}, | ||
flush: t.unreached_func('flush should not be called') | ||
}); | ||
const cancelPromise = ts.readable.cancel(originalReason); | ||
const closePromise = ts.writable.close(); | ||
await Promise.all([ | ||
promise_rejects_exactly(t, thrownError, cancelPromise, 'cancelPromise should reject with thrownError'), | ||
promise_rejects_exactly(t, thrownError, closePromise, 'closePromise should reject with thrownError'), | ||
]); | ||
}, 'readable.cancel() and a parallel writable.close() should reject if a transformer.cancel() calls controller.error()'); | ||
|
||
promise_test(async t => { | ||
let controller; | ||
const ts = new TransformStream({ | ||
start(c) { | ||
controller = c; | ||
}, | ||
async cancel(reason) { | ||
assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason'); | ||
controller.error(thrownError); | ||
}, | ||
flush: t.unreached_func('flush should not be called') | ||
}); | ||
const cancelPromise = ts.writable.abort(originalReason); | ||
await promise_rejects_exactly(t, thrownError, cancelPromise, 'cancelPromise should reject with thrownError'); | ||
const closePromise = ts.readable.cancel(1); | ||
await promise_rejects_exactly(t, thrownError, closePromise, 'closePromise should reject with thrownError'); | ||
}, 'writable.abort() and readable.cancel() should reject if a transformer.cancel() calls controller.error()'); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
Hmm, I see. Since
TransformStreamDefaultSourceCancelAlgorithm
takes at least one microtask to runtransformer.cancel()
(even if it does not exist), theTypeError
fromterminate()
now wins the race. 🤔I suppose that's fine.
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.
hello @MattiasBuelens i am trying to implement this for node this test presently fails my knowledge of WPTs is little limited hence asking here so is
cancelPromise
also expected to throw in this test? I am seeing that running this test standalone throws TypeError but unable to understand why the WPT says this uncaughtThere 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.
This test will fail if you have not implanted the changes from the streams spec PR (yet).
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.
This test passes against the reference implementation, so if it fails it may be a bug in your implementation.
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.
Could be a bug no doubt i just wnated to know this test is basically equalt to the following script right:
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.
@debadree25 Yes, that looks script right. Ideally, you want to assert that the
TypeError
originates from.closed
and not fromcancelPromise
, so in Node you could use assert.rejects():No,
cancelPromise
should always resolve.The only change is that
writer.closed
now becomes errored bycontroller.terminate()
, whereas previously it would be errored byts.readable.cancel(cancelReason)
. This is becausereadable.cancel()
now needs to go through the (possibly asynchronous)transformer.cancel()
method first before it can error the writable, whereascontroller.terminate()
always errors the writable synchronously (without going through a transformer method).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.
Thank you so much @MattiasBuelens for the detail response i guess i have found the issue with my implementation!