1// META: global=window,worker 2// META: script=../resources/test-utils.js 3// META: script=../resources/recording-streams.js 4'use strict'; 5 6const error1 = new Error('error1!'); 7error1.name = 'error1'; 8 9const error2 = new Error('error2!'); 10error2.name = 'error2'; 11 12function createErroredWritableStream(t) { 13 return Promise.resolve().then(() => { 14 const ws = recordingWritableStream({ 15 start(c) { 16 c.error(error2); 17 } 18 }); 19 20 const writer = ws.getWriter(); 21 return promise_rejects_exactly(t, error2, writer.closed, 'the writable stream must be errored with error2') 22 .then(() => { 23 writer.releaseLock(); 24 assert_array_equals(ws.events, []); 25 return ws; 26 }); 27 }); 28} 29 30promise_test(t => { 31 const rs = recordingReadableStream({ 32 start(c) { 33 c.error(error1); 34 } 35 }); 36 const ws = recordingWritableStream({ 37 start(c) { 38 c.error(error2); 39 } 40 }); 41 42 // Trying to abort a stream that is erroring will give the writable's error 43 return promise_rejects_exactly(t, error2, rs.pipeTo(ws), 'pipeTo must reject with the writable stream\'s error').then(() => { 44 assert_array_equals(rs.events, []); 45 assert_array_equals(ws.events, []); 46 47 return Promise.all([ 48 promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1'), 49 promise_rejects_exactly(t, error2, ws.getWriter().closed, 'the writable stream must be errored with error2') 50 ]); 51 }); 52 53}, 'Piping from an errored readable stream to an erroring writable stream'); 54 55promise_test(t => { 56 const rs = recordingReadableStream({ 57 start(c) { 58 c.error(error1); 59 } 60 }); 61 62 return createErroredWritableStream(t) 63 .then(ws => promise_rejects_exactly(t, error1, rs.pipeTo(ws), 'pipeTo must reject with the readable stream\'s error')) 64 .then(() => { 65 assert_array_equals(rs.events, []); 66 67 return promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1'); 68 }); 69}, 'Piping from an errored readable stream to an errored writable stream'); 70 71promise_test(t => { 72 const rs = recordingReadableStream({ 73 start(c) { 74 c.error(error1); 75 } 76 }); 77 const ws = recordingWritableStream({ 78 start(c) { 79 c.error(error2); 80 } 81 }); 82 83 return promise_rejects_exactly(t, error1, rs.pipeTo(ws, { preventAbort: true }), 84 'pipeTo must reject with the readable stream\'s error') 85 .then(() => { 86 assert_array_equals(rs.events, []); 87 assert_array_equals(ws.events, []); 88 89 return Promise.all([ 90 promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1'), 91 promise_rejects_exactly(t, error2, ws.getWriter().closed, 'the writable stream must be errored with error2') 92 ]); 93 }); 94 95}, 'Piping from an errored readable stream to an erroring writable stream; preventAbort = true'); 96 97promise_test(t => { 98 const rs = recordingReadableStream({ 99 start(c) { 100 c.error(error1); 101 } 102 }); 103 return createErroredWritableStream(t) 104 .then(ws => promise_rejects_exactly(t, error1, rs.pipeTo(ws, { preventAbort: true }), 105 'pipeTo must reject with the readable stream\'s error')) 106 .then(() => { 107 assert_array_equals(rs.events, []); 108 109 return promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1'); 110 }); 111 112}, 'Piping from an errored readable stream to an errored writable stream; preventAbort = true'); 113 114promise_test(t => { 115 const rs = recordingReadableStream({ 116 start(c) { 117 c.error(error1); 118 } 119 }); 120 const ws = recordingWritableStream(); 121 const writer = ws.getWriter(); 122 const closePromise = writer.close(); 123 writer.releaseLock(); 124 125 return promise_rejects_exactly(t, error1, rs.pipeTo(ws), 'pipeTo must reject with the readable stream\'s error').then(() => { 126 assert_array_equals(rs.events, []); 127 assert_array_equals(ws.events, ['abort', error1]); 128 129 return Promise.all([ 130 promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1'), 131 promise_rejects_exactly(t, error1, ws.getWriter().closed, 132 'closed must reject with error1'), 133 promise_rejects_exactly(t, error1, closePromise, 134 'close() must reject with error1') 135 ]); 136 }); 137 138}, 'Piping from an errored readable stream to a closing writable stream'); 139 140promise_test(t => { 141 const rs = recordingReadableStream({ 142 start(c) { 143 c.error(error1); 144 } 145 }); 146 const ws = recordingWritableStream(); 147 const writer = ws.getWriter(); 148 const closePromise = writer.close(); 149 writer.releaseLock(); 150 151 return flushAsyncEvents().then(() => { 152 return promise_rejects_exactly(t, error1, rs.pipeTo(ws), 'pipeTo must reject with the readable stream\'s error').then(() => { 153 assert_array_equals(rs.events, []); 154 assert_array_equals(ws.events, ['close']); 155 156 return Promise.all([ 157 promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1'), 158 ws.getWriter().closed, 159 closePromise 160 ]); 161 }); 162 }); 163 164}, 'Piping from an errored readable stream to a closed writable stream'); 165 166promise_test(t => { 167 const rs = recordingReadableStream({ 168 start(c) { 169 c.close(); 170 } 171 }); 172 const ws = recordingWritableStream({ 173 start(c) { 174 c.error(error1); 175 } 176 }); 177 178 return promise_rejects_exactly(t, error1, rs.pipeTo(ws), 'pipeTo must reject with the writable stream\'s error').then(() => { 179 assert_array_equals(rs.events, []); 180 assert_array_equals(ws.events, []); 181 182 return Promise.all([ 183 rs.getReader().closed, 184 promise_rejects_exactly(t, error1, ws.getWriter().closed, 'the writable stream must be errored with error1') 185 ]); 186 }); 187 188}, 'Piping from a closed readable stream to an erroring writable stream'); 189 190promise_test(t => { 191 const rs = recordingReadableStream({ 192 start(c) { 193 c.close(); 194 } 195 }); 196 return createErroredWritableStream(t) 197 .then(ws => promise_rejects_exactly(t, error2, rs.pipeTo(ws), 'pipeTo must reject with the writable stream\'s error')) 198 .then(() => { 199 assert_array_equals(rs.events, []); 200 201 return rs.getReader().closed; 202 }); 203 204}, 'Piping from a closed readable stream to an errored writable stream'); 205 206promise_test(() => { 207 const rs = recordingReadableStream({ 208 start(c) { 209 c.close(); 210 } 211 }); 212 const ws = recordingWritableStream(); 213 const writer = ws.getWriter(); 214 writer.close(); 215 writer.releaseLock(); 216 217 return rs.pipeTo(ws).then(() => { 218 assert_array_equals(rs.events, []); 219 assert_array_equals(ws.events, ['close']); 220 221 return Promise.all([ 222 rs.getReader().closed, 223 ws.getWriter().closed 224 ]); 225 }); 226 227}, 'Piping from a closed readable stream to a closed writable stream'); 228