Skip to content

Commit

Permalink
Streams: resolve BYOB reads immediately on cancel
Browse files Browse the repository at this point in the history
See whatwg/streams#1129 for the accompanying spec change.
  • Loading branch information
MattiasBuelens authored Jun 1, 2021
1 parent 7d240f8 commit b869e60
Showing 1 changed file with 45 additions and 14 deletions.
59 changes: 45 additions & 14 deletions streams/readable-byte-streams/general.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,6 @@ promise_test(t => {
cancel(r) {
if (cancelCount === 0) {
reason = r;
controller.byobRequest.respond(0);
}

++cancelCount;
Expand All @@ -1091,12 +1090,13 @@ promise_test(t => {
const reader = stream.getReader({ mode: 'byob' });

const readPromise = reader.read(new Uint8Array(1)).then(result => {
assert_true(result.done);
assert_true(result.done, 'result.done');
assert_equals(result.value, undefined, 'result.value');
});

const cancelPromise = reader.cancel(passedReason).then(result => {
assert_equals(result, undefined);
assert_equals(cancelCount, 1);
assert_equals(result, undefined, 'cancel() return value should be fulfilled with undefined');
assert_equals(cancelCount, 1, 'cancel() should be called only once');
assert_equals(reason, passedReason, 'reason should equal the passed reason');
});

Expand Down Expand Up @@ -1133,20 +1133,16 @@ promise_test(() => {

const promise = reader.read(new Uint16Array(1)).then(result => {
assert_true(result.done, 'result.done');
assert_equals(result.value.constructor, Uint16Array, 'result.value');
assert_equals(result.value, undefined, 'result.value');
});

assert_equals(pullCount, 1, '1 pull() should have been made in response to partial fill by enqueue()');
assert_not_equals(byobRequest, null, 'byobRequest should not be null');
assert_equals(viewInfos[0].byteLength, 2, 'byteLength before enqueue() shouild be 2');
assert_equals(viewInfos[1].byteLength, 1, 'byteLength after enqueue() should be 1');


reader.cancel();

// Tell that the buffer given via pull() is returned.
controller.byobRequest.respond(0);

assert_equals(pullCount, 1, 'pull() should only be called once');
return promise;
});
Expand Down Expand Up @@ -2045,15 +2041,19 @@ promise_test(() => {
}, 'calling respondWithNewView() twice on the same byobRequest should throw');

promise_test(() => {
let controller;
let byobRequest;
let resolvePullCalledPromise;
const pullCalledPromise = new Promise(resolve => {
resolvePullCalledPromise = resolve;
});
let resolvePull;
const rs = new ReadableStream({
pull(controller) {
byobRequest = controller.byobRequest;
start(c) {
controller = c;
},
pull(c) {
byobRequest = c.byobRequest;
resolvePullCalledPromise();
return new Promise(resolve => {
resolvePull = resolve;
Expand All @@ -2064,15 +2064,46 @@ promise_test(() => {
const reader = rs.getReader({ mode: 'byob' });
const readPromise = reader.read(new Uint8Array(16));
return pullCalledPromise.then(() => {
const cancelPromise = reader.cancel('meh');
resolvePull();
controller.close();
byobRequest.respond(0);
return Promise.all([readPromise, cancelPromise]).then(() => {
resolvePull();
return readPromise.then(() => {
assert_throws_js(TypeError, () => byobRequest.respond(0), 'respond() should throw');
});
});
}, 'calling respond(0) twice on the same byobRequest should throw even when closed');

promise_test(() => {
let controller;
let byobRequest;
let resolvePullCalledPromise;
const pullCalledPromise = new Promise(resolve => {
resolvePullCalledPromise = resolve;
});
let resolvePull;
const rs = new ReadableStream({
start(c) {
controller = c;
},
pull(c) {
byobRequest = c.byobRequest;
resolvePullCalledPromise();
return new Promise(resolve => {
resolvePull = resolve;
});
},
type: 'bytes'
});
const reader = rs.getReader({ mode: 'byob' });
const readPromise = reader.read(new Uint8Array(16));
return pullCalledPromise.then(() => {
const cancelPromise = reader.cancel('meh');
assert_throws_js(TypeError, () => byobRequest.respond(0), 'respond() should throw');
resolvePull();
return Promise.all([readPromise, cancelPromise]);
});
}, 'calling respond() should throw when canceled');

promise_test(() => {
let resolvePullCalledPromise;
const pullCalledPromise = new Promise(resolve => {
Expand Down

0 comments on commit b869e60

Please sign in to comment.