Skip to content

Commit

Permalink
Version 2: run cancel before cancelling
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed Jun 12, 2023
1 parent ff3c698 commit 4156716
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
12 changes: 6 additions & 6 deletions streams/transform-streams/cancel.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
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({
Expand All @@ -18,7 +21,6 @@ promise_test(async t => {
}, 'cancelling the readable side should call transformer.cancel()');

promise_test(async t => {
const originalReason = new Error('original reason');
const ts = new TransformStream({
cancel(reason) {
assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason');
Expand All @@ -28,7 +30,7 @@ promise_test(async t => {
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, originalReason, writer.closed, 'writer.closed should reject with original reason');
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 => {
Expand All @@ -45,7 +47,6 @@ promise_test(async t => {
}, 'aborting the writable side should call transformer.abort()');

promise_test(async t => {
const originalReason = new Error('original reason');
const ts = new TransformStream({
cancel(reason) {
assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason');
Expand All @@ -56,11 +57,10 @@ promise_test(async t => {
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, originalReason, reader.closed, 'reader.closed should reject with original reason');
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 originalReason = new Error('original reason');
const ts = new TransformStream({
async cancel(reason) {
assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason');
Expand All @@ -72,7 +72,7 @@ promise_test(async t => {
const closePromise = ts.writable.close();
await Promise.all([
promise_rejects_exactly(t, thrownError, cancelPromise, 'cancelPromise should reject with thrownError'),
promise_rejects_exactly(t, originalReason, closePromise, 'closePromise 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');

Expand Down
28 changes: 21 additions & 7 deletions streams/transform-streams/errors.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,14 @@ promise_test(t => {
// The microtask following transformer.start() hasn't completed yet, so the abort is queued and not notified to the
// TransformStream yet.
const abortPromise = writer.abort(thrownError);
const cancelPromise = ts.readable.cancel(new Error('cancel reason'));
const cancelPromise = ts.readable.cancel(new Error('cancel reason'))
return Promise.all([
abortPromise,
cancelPromise,
promise_rejects_exactly(t, thrownError, writer.closed, 'writer.closed should reject with thrownError')]);
}, 'abort should set the close reason for the writable when it happens before cancel during start, but cancel should ' +
'still succeed');
promise_rejects_exactly(t, thrownError, writer.closed, 'writer.closed should reject'),
]);
}, 'abort should set the close reason for the writable when it happens before cancel during start, and cancel should ' +
'reject');

promise_test(t => {
let resolveTransform;
Expand Down Expand Up @@ -256,13 +257,26 @@ promise_test(t => {
controller = c;
}
});
const cancelPromise = ts.readable.cancel(thrownError);
controller.error(ignoredError);
const cancelPromise = ts.readable.cancel(ignoredError);
controller.error(thrownError);
return Promise.all([
cancelPromise,
promise_rejects_exactly(t, thrownError, ts.writable.getWriter().closed, 'closed should reject with thrownError')
]);
}, 'controller.error() should do nothing after readable.cancel()');
}, 'controller.error() should close writable immediately after readable.cancel()');

promise_test(t => {
let controller;
const ts = new TransformStream({
start(c) {
controller = c;
}
});
return ts.readable.cancel(thrownError).then(() => {
controller.error(ignoredError);
return promise_rejects_exactly(t, thrownError, ts.writable.getWriter().closed, 'closed should reject with thrownError');
});
}, 'controller.error() should do nothing after readable.cancel() resolves');

promise_test(t => {
let controller;
Expand Down
4 changes: 2 additions & 2 deletions streams/transform-streams/flush.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,5 @@ promise_test(t => {
controller.error(error1);
}
});
return promise_rejects_exactly(t, error1, ts.writable.getWriter().close(), 'close() should reject');
}, 'error() during flush should cause writer.close() to reject');
return ts.writable.getWriter().close();
}, 'error() during flush should not cause writer.close() to reject');
19 changes: 17 additions & 2 deletions streams/transform-streams/general.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,24 @@ promise_test(t => {
controller.terminate();
return Promise.all([
cancelPromise,
promise_rejects_exactly(t, cancelReason, ts.writable.getWriter().closed, 'closed should reject with cancelReason')
promise_rejects_js(t, TypeError, ts.writable.getWriter().closed, 'closed should reject with TypeError')
]);
}, 'terminate() should do nothing after readable.cancel()');
}, 'terminate() should abort writable immediately after readable.cancel()');

promise_test(t => {
let controller;
const ts = new TransformStream({
start(c) {
controller = c;
}
});
const cancelReason = { name: 'cancelReason' };
return ts.readable.cancel(cancelReason).then(() => {
controller.terminate();
return promise_rejects_exactly(t, cancelReason, ts.writable.getWriter().closed, 'closed should reject with TypeError');
})
}, 'terminate() should do nothing after readable.cancel() resolves');


promise_test(() => {
let calls = 0;
Expand Down
4 changes: 4 additions & 0 deletions streams/transform-streams/reentrant-strategies.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ promise_test(t => {
// call to TransformStreamDefaultSink.
return delay(0).then(() => {
controller.enqueue('a');
return reader.read();
}).then(({ value, done }) => {
assert_false(done, 'done should be false');
assert_equals(value, 'a', 'value should be correct');
return Promise.all([promise_rejects_exactly(t, error1, reader.read(), 'read() should reject'), abortPromise]);
});
}, 'writer.abort() inside size() should work');

0 comments on commit 4156716

Please sign in to comment.