|
| 1 | +// META: global=window,worker,shadowrealm |
| 2 | +// META: script=../resources/test-utils.js |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const thrownError = new Error('bad things are happening!'); |
| 6 | +thrownError.name = 'error1'; |
| 7 | + |
| 8 | +const originalReason = new Error('original reason'); |
| 9 | +originalReason.name = 'error2'; |
| 10 | + |
| 11 | +promise_test(async t => { |
| 12 | + let cancelled = undefined; |
| 13 | + const ts = new TransformStream({ |
| 14 | + cancel(reason) { |
| 15 | + cancelled = reason; |
| 16 | + } |
| 17 | + }); |
| 18 | + const res = await ts.readable.cancel(thrownError); |
| 19 | + assert_equals(res, undefined, 'readable.cancel() should return undefined'); |
| 20 | + assert_equals(cancelled, thrownError, 'transformer.cancel() should be called with the passed reason'); |
| 21 | +}, 'cancelling the readable side should call transformer.cancel()'); |
| 22 | + |
| 23 | +promise_test(async t => { |
| 24 | + const ts = new TransformStream({ |
| 25 | + cancel(reason) { |
| 26 | + assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason'); |
| 27 | + throw thrownError; |
| 28 | + } |
| 29 | + }); |
| 30 | + const writer = ts.writable.getWriter(); |
| 31 | + const cancelPromise = ts.readable.cancel(originalReason); |
| 32 | + await promise_rejects_exactly(t, thrownError, cancelPromise, 'readable.cancel() should reject with thrownError'); |
| 33 | + await promise_rejects_exactly(t, thrownError, writer.closed, 'writer.closed should reject with thrownError'); |
| 34 | +}, 'cancelling the readable side should reject if transformer.cancel() throws'); |
| 35 | + |
| 36 | +promise_test(async t => { |
| 37 | + let aborted = undefined; |
| 38 | + const ts = new TransformStream({ |
| 39 | + cancel(reason) { |
| 40 | + aborted = reason; |
| 41 | + }, |
| 42 | + flush: t.unreached_func('flush should not be called') |
| 43 | + }); |
| 44 | + const res = await ts.writable.abort(thrownError); |
| 45 | + assert_equals(res, undefined, 'writable.abort() should return undefined'); |
| 46 | + assert_equals(aborted, thrownError, 'transformer.abort() should be called with the passed reason'); |
| 47 | +}, 'aborting the writable side should call transformer.abort()'); |
| 48 | + |
| 49 | +promise_test(async t => { |
| 50 | + const ts = new TransformStream({ |
| 51 | + cancel(reason) { |
| 52 | + assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason'); |
| 53 | + throw thrownError; |
| 54 | + }, |
| 55 | + flush: t.unreached_func('flush should not be called') |
| 56 | + }); |
| 57 | + const reader = ts.readable.getReader(); |
| 58 | + const abortPromise = ts.writable.abort(originalReason); |
| 59 | + await promise_rejects_exactly(t, thrownError, abortPromise, 'writable.abort() should reject with thrownError'); |
| 60 | + await promise_rejects_exactly(t, thrownError, reader.closed, 'reader.closed should reject with thrownError'); |
| 61 | +}, 'aborting the writable side should reject if transformer.cancel() throws'); |
| 62 | + |
| 63 | +promise_test(async t => { |
| 64 | + const ts = new TransformStream({ |
| 65 | + async cancel(reason) { |
| 66 | + assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason'); |
| 67 | + throw thrownError; |
| 68 | + }, |
| 69 | + flush: t.unreached_func('flush should not be called') |
| 70 | + }); |
| 71 | + const cancelPromise = ts.readable.cancel(originalReason); |
| 72 | + const closePromise = ts.writable.close(); |
| 73 | + await Promise.all([ |
| 74 | + promise_rejects_exactly(t, thrownError, cancelPromise, 'cancelPromise should reject with thrownError'), |
| 75 | + promise_rejects_exactly(t, thrownError, closePromise, 'closePromise should reject with thrownError'), |
| 76 | + ]); |
| 77 | +}, 'closing the writable side should reject if a parallel transformer.cancel() throws'); |
| 78 | + |
| 79 | +promise_test(async t => { |
| 80 | + let controller; |
| 81 | + const ts = new TransformStream({ |
| 82 | + start(c) { |
| 83 | + controller = c; |
| 84 | + }, |
| 85 | + async cancel(reason) { |
| 86 | + assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason'); |
| 87 | + controller.error(thrownError); |
| 88 | + }, |
| 89 | + flush: t.unreached_func('flush should not be called') |
| 90 | + }); |
| 91 | + const cancelPromise = ts.readable.cancel(originalReason); |
| 92 | + const closePromise = ts.writable.close(); |
| 93 | + await Promise.all([ |
| 94 | + promise_rejects_exactly(t, thrownError, cancelPromise, 'cancelPromise should reject with thrownError'), |
| 95 | + promise_rejects_exactly(t, thrownError, closePromise, 'closePromise should reject with thrownError'), |
| 96 | + ]); |
| 97 | +}, 'readable.cancel() and a parallel writable.close() should reject if a transformer.cancel() calls controller.error()'); |
| 98 | + |
| 99 | +promise_test(async t => { |
| 100 | + let controller; |
| 101 | + const ts = new TransformStream({ |
| 102 | + start(c) { |
| 103 | + controller = c; |
| 104 | + }, |
| 105 | + async cancel(reason) { |
| 106 | + assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason'); |
| 107 | + controller.error(thrownError); |
| 108 | + }, |
| 109 | + flush: t.unreached_func('flush should not be called') |
| 110 | + }); |
| 111 | + const cancelPromise = ts.writable.abort(originalReason); |
| 112 | + await promise_rejects_exactly(t, thrownError, cancelPromise, 'cancelPromise should reject with thrownError'); |
| 113 | + const closePromise = ts.readable.cancel(1); |
| 114 | + await promise_rejects_exactly(t, thrownError, closePromise, 'closePromise should reject with thrownError'); |
| 115 | +}, 'writable.abort() and readable.cancel() should reject if a transformer.cancel() calls controller.error()'); |
0 commit comments