1// META: global=window,worker 2// META: script=../resources/recording-streams.js 3// META: script=../resources/test-utils.js 4'use strict'; 5 6promise_test(t => { 7 const ts = recordingTransformStream({}, undefined, { highWaterMark: 0 }); 8 const rs = new ReadableStream({ 9 start(controller) { 10 controller.enqueue(0); 11 } 12 }); 13 let pipeToRejected = false; 14 const pipeToPromise = promise_rejects_js(t, TypeError, rs.pipeTo(ts.writable), 'pipeTo should reject').then(() => { 15 pipeToRejected = true; 16 }); 17 return delay(0).then(() => { 18 assert_array_equals(ts.events, [], 'transform() should have seen no chunks'); 19 assert_false(pipeToRejected, 'pipeTo() should not have rejected yet'); 20 ts.controller.terminate(); 21 return pipeToPromise; 22 }).then(() => { 23 assert_array_equals(ts.events, [], 'transform() should still have seen no chunks'); 24 assert_true(pipeToRejected, 'pipeToRejected must be true'); 25 }); 26}, 'controller.terminate() should error pipeTo()'); 27 28promise_test(t => { 29 const ts = recordingTransformStream({}, undefined, { highWaterMark: 1 }); 30 const rs = new ReadableStream({ 31 start(controller) { 32 controller.enqueue(0); 33 controller.enqueue(1); 34 } 35 }); 36 const pipeToPromise = rs.pipeTo(ts.writable); 37 return delay(0).then(() => { 38 assert_array_equals(ts.events, ['transform', 0], 'transform() should have seen one chunk'); 39 ts.controller.terminate(); 40 return promise_rejects_js(t, TypeError, pipeToPromise, 'pipeTo() should reject'); 41 }).then(() => { 42 assert_array_equals(ts.events, ['transform', 0], 'transform() should still have seen only one chunk'); 43 }); 44}, 'controller.terminate() should prevent remaining chunks from being processed'); 45 46test(() => { 47 new TransformStream({ 48 start(controller) { 49 controller.enqueue(0); 50 controller.terminate(); 51 assert_throws_js(TypeError, () => controller.enqueue(1), 'enqueue should throw'); 52 } 53 }); 54}, 'controller.enqueue() should throw after controller.terminate()'); 55 56const error1 = new Error('error1'); 57error1.name = 'error1'; 58 59promise_test(t => { 60 const ts = new TransformStream({ 61 start(controller) { 62 controller.enqueue(0); 63 controller.terminate(); 64 controller.error(error1); 65 } 66 }); 67 return Promise.all([ 68 promise_rejects_js(t, TypeError, ts.writable.abort(), 'abort() should reject with a TypeError'), 69 promise_rejects_exactly(t, error1, ts.readable.cancel(), 'cancel() should reject with error1'), 70 promise_rejects_exactly(t, error1, ts.readable.getReader().closed, 'closed should reject with error1') 71 ]); 72}, 'controller.error() after controller.terminate() with queued chunk should error the readable'); 73 74promise_test(t => { 75 const ts = new TransformStream({ 76 start(controller) { 77 controller.terminate(); 78 controller.error(error1); 79 } 80 }); 81 return Promise.all([ 82 promise_rejects_js(t, TypeError, ts.writable.abort(), 'abort() should reject with a TypeError'), 83 ts.readable.cancel(), 84 ts.readable.getReader().closed 85 ]); 86}, 'controller.error() after controller.terminate() without queued chunk should do nothing'); 87 88promise_test(() => { 89 const ts = new TransformStream({ 90 flush(controller) { 91 controller.terminate(); 92 } 93 }); 94 const writer = ts.writable.getWriter(); 95 return Promise.all([ 96 writer.close(), 97 writer.closed, 98 ts.readable.getReader().closed 99 ]); 100}, 'controller.terminate() inside flush() should not prevent writer.close() from succeeding'); 101