-
Notifications
You must be signed in to change notification settings - Fork 163
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
Port writable stream abort tests to wpt #540
Port writable stream abort tests to wpt #540
Conversation
5a52ea9
to
2c1618e
Compare
The pattern we've been using for that is to put
at the top of the file (plus maybe error2 if you have multiple errors and want to distinguish), then later doing return promise_rejects(t, error1, writer.write(), 'err message'); |
Ah I see. I'll adapt my code to use |
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.
In general very nice, and the porting work is super-appreciated. The main things are using promise_rejects and recordingWritableStream to clean up the tests.
}, 'Aborting a WritableStream should cause the writer\'s fulfilled ready promise to reset to a rejected one'); | ||
|
||
function promise_fulfills(expectedValue, promise, msg) { |
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 is not necessary. Inside your promise_test, just do
return promise.then(value => {
assert_equals(value, expectedValue, msg);
});
the test will fail if it returns a rejected promise.
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.
agree
promise_test(() => { | ||
const ws = new WritableStream({ | ||
write() { | ||
throw new Error('Unexpected write() call'); |
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.
It's a decent bit of work, but I think these sorts of tests would be improved by using the recordingWritableStream, and then checking the recorded events in a final .then
block. An example:
streams/reference-implementation/to-upstream-wpts/writable-streams/close.js
Lines 65 to 81 in ff1d3c9
promise_test(() => { | |
const ws = recordingWritableStream(); | |
const writer = ws.getWriter(); | |
return writer.ready.then(() => { | |
assert_equals(writer.desiredSize, 1, 'desiredSize should be 1'); | |
writer.close(); | |
assert_equals(writer.desiredSize, 1, 'desiredSize should be still 1'); | |
return writer.ready.then(v => { | |
assert_equals(v, undefined, 'ready promise should be fulfilled with undefined'); | |
assert_array_equals(ws.events, ['close'], 'write and abort should not be called'); | |
}); | |
}); | |
}, 'when close is called on a WritableStream in writable state, ready should return a fulfilled promise'); |
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.
Thanks for the tip. recordingWritableStream
is much cleaner 👍
promise_test(() => { | ||
let writeCount = 0; | ||
|
||
const ws = new WritableStream({ |
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.
Another good example of where recordingWritableStream would help you. You wouldn't need writeCount or any asserts, just check the events in a final .then
block.
]); | ||
}, 'Aborting a WritableStream puts it in an errored state, with stored error equal to the abort reason'); | ||
|
||
test(() => { |
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 will need to be a promise_test when it moves to use assert_promise_rejects
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.
I'm unsure where to import assert_promise_rejects
from. This function seems to be duplicated inside the web-platform-tests
repo. I tried including one of the existing functions, but get an URL error by testharness
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.
I meant promise_rejects, sorry.
|
||
writer.close(); | ||
|
||
setTimeout(() => { |
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.
Use promise_test and delay()
Sorry for the late response, I got ill for a few days. Thanks, for the review. I'll make these changes over the day 👍 |
It looks like you just accidentally created a duplicate of |
48d730e
to
edaab4b
Compare
Thanks for clearing that up. Yeah, I got confused by the existing tests in inside the |
@@ -1,14 +1,16 @@ | |||
'use strict'; | |||
/* global delay, recordingWritableStream */ |
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.
If you rebase this will not be necessary
|
||
if (self.importScripts) { | ||
self.importScripts('../resources/test-utils.js'); |
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.
Move this after testharness.js
writer.write(2); | ||
}) | ||
.then(() => { | ||
assert_equals(ws.events.length, 2, 'the stream should have received 2 events'); |
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.
You should use assert_array_equals here like the other recording stream tests. Same below.
The events[1] entry is the argument passed to abort, by the way, and is not related to writes.
|
||
const writer = ws.getWriter(); | ||
|
||
return promise_rejects(t, errorInSinkAbort, writer.abort(undefined), |
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.
Can you use the error1
pattern used elsewhere? With a single error1
at the top of the file? Here and below.
Also everywhere that creates a separate passedReason
can use that error1
.
const passedReason = new Error('Sorry, it just wasn\'t meant to be.'); | ||
writer.abort(passedReason); | ||
|
||
assert_equals(recordedReason, passedReason); |
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 can use recordingWritableStream instead, with assert_array_equals on the contents.
]); | ||
}, 'Aborting a WritableStream puts it in an errored state, with stored error equal to the abort reason'); | ||
|
||
// TODO return promise |
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.
Just store the promise with const writePromise = promise_rejects(...)
and then return it as the last line
|
||
const passedReason = new Error('Sorry, it just wasn\'t meant to be.'); | ||
writer.abort(passedReason); | ||
}, 'Aborting a WritableStream causes any outstanding write() promises to be rejected with the abort reason'); |
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.
Test description is again outdated; "with a TypeError"
const passedReason = new Error('Sorry, it just wasn\'t meant to be.'); | ||
writer.abort(passedReason); | ||
|
||
return promise_rejects(t, new TypeError('Aborted'), writer.closed, 'the stream should be errored with a TypeError'); |
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.
Don't include 'Aborted'
here; the test framework doesn't look at messages. (Yeah, it's kind of wonky.)
.then(() => writer.abort()) | ||
.then( | ||
v => assert_equals(v, undefined, 'abort promise should fulfill with undefined'), | ||
() => { throw new Error(); } |
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 () => { throw new Error(); }
is not necessary. In fact the second .then()
can be removed entirely since we already test the undefinedness earlier.
|
||
const writer = ws.getWriter(); | ||
|
||
writer.abort(); |
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.
Again best to use recordingWritableStream. As-is, this test will pass even if close is never called.
It might be good to add something to recordingWritableStream's close() that asserts that arguments.length === 0
.
edaab4b
to
995382f
Compare
@marvinhagemeister I'll take care of the rest of the issues I noted. Thanks so much for your help getting this file moved over :). |
Just ported another test file to wpt. There is one thing where I'm unsure if this is the correct way for assertion. In some tests the constructor of the error object is compared. I couldn't get it to work without casting it to a string first.
For example: