OLD | NEW |
(Empty) | |
| 1 'use strict'; |
| 2 |
| 3 if (self.importScripts) { |
| 4 self.importScripts('/resources/testharness.js'); |
| 5 } |
| 6 |
| 7 |
| 8 test(() => { |
| 9 const theError = new Error('a unique string'); |
| 10 |
| 11 assert_throws(theError, () => { |
| 12 new ReadableStream({ |
| 13 get start() { |
| 14 throw theError; |
| 15 } |
| 16 }); |
| 17 }, 'constructing the stream should re-throw the error'); |
| 18 |
| 19 }, 'Underlying source start: throwing getter'); |
| 20 |
| 21 |
| 22 test(() => { |
| 23 const theError = new Error('a unique string'); |
| 24 |
| 25 assert_throws(theError, () => { |
| 26 new ReadableStream({ |
| 27 start() { |
| 28 throw theError; |
| 29 } |
| 30 }); |
| 31 }, 'constructing the stream should re-throw the error'); |
| 32 |
| 33 }, 'Underlying source start: throwing method'); |
| 34 |
| 35 |
| 36 promise_test(t => { |
| 37 |
| 38 const theError = new Error('a unique string'); |
| 39 const rs = new ReadableStream({ |
| 40 get pull() { |
| 41 throw theError; |
| 42 } |
| 43 }); |
| 44 |
| 45 return promise_rejects(t, theError, rs.getReader().closed); |
| 46 |
| 47 }, 'Underlying source: throwing pull getter (initial pull)'); |
| 48 |
| 49 |
| 50 promise_test(t => { |
| 51 |
| 52 const theError = new Error('a unique string'); |
| 53 const rs = new ReadableStream({ |
| 54 pull() { |
| 55 throw theError; |
| 56 } |
| 57 }); |
| 58 |
| 59 return promise_rejects(t, theError, rs.getReader().closed); |
| 60 |
| 61 }, 'Underlying source: throwing pull method (initial pull)'); |
| 62 |
| 63 |
| 64 promise_test(t => { |
| 65 |
| 66 const theError = new Error('a unique string'); |
| 67 |
| 68 let counter = 0; |
| 69 const rs = new ReadableStream({ |
| 70 get pull() { |
| 71 ++counter; |
| 72 if (counter === 1) { |
| 73 return c => c.enqueue('a'); |
| 74 } |
| 75 |
| 76 throw theError; |
| 77 } |
| 78 }); |
| 79 const reader = rs.getReader(); |
| 80 |
| 81 return Promise.all([ |
| 82 reader.read().then(r => { |
| 83 assert_object_equals(r, { value: 'a', done: false }, 'the chunk read shoul
d be correct'); |
| 84 }), |
| 85 promise_rejects(t, theError, reader.closed) |
| 86 ]); |
| 87 |
| 88 }, 'Underlying source pull: throwing getter (second pull)'); |
| 89 |
| 90 |
| 91 promise_test(t => { |
| 92 |
| 93 const theError = new Error('a unique string'); |
| 94 |
| 95 let counter = 0; |
| 96 const rs = new ReadableStream({ |
| 97 pull(c) { |
| 98 ++counter; |
| 99 if (counter === 1) { |
| 100 c.enqueue('a'); |
| 101 } |
| 102 |
| 103 throw theError; |
| 104 } |
| 105 }); |
| 106 const reader = rs.getReader(); |
| 107 |
| 108 return Promise.all([ |
| 109 reader.read().then(r => { |
| 110 assert_object_equals(r, { value: 'a', done: false }, 'the chunk read shoul
d be correct'); |
| 111 }), |
| 112 promise_rejects(t, theError, reader.closed) |
| 113 ]); |
| 114 |
| 115 }, 'Underlying source pull: throwing method (second pull)'); |
| 116 |
| 117 promise_test(t => { |
| 118 |
| 119 const theError = new Error('a unique string'); |
| 120 const rs = new ReadableStream({ |
| 121 get cancel() { |
| 122 throw theError; |
| 123 } |
| 124 }); |
| 125 |
| 126 return promise_rejects(t, theError, rs.cancel()); |
| 127 |
| 128 }, 'Underlying source cancel: throwing getter'); |
| 129 |
| 130 promise_test(t => { |
| 131 |
| 132 const theError = new Error('a unique string'); |
| 133 const rs = new ReadableStream({ |
| 134 cancel() { |
| 135 throw theError; |
| 136 } |
| 137 }); |
| 138 |
| 139 return promise_rejects(t, theError, rs.cancel()); |
| 140 |
| 141 }, 'Underlying source cancel: throwing method'); |
| 142 |
| 143 promise_test(t => { |
| 144 |
| 145 let controller; |
| 146 const rs = new ReadableStream({ |
| 147 start(c) { |
| 148 controller = c; |
| 149 } |
| 150 }); |
| 151 |
| 152 rs.cancel(); |
| 153 controller.enqueue('a'); // Calling enqueue after canceling should not throw a
nything. |
| 154 |
| 155 return rs.getReader().closed; |
| 156 |
| 157 }, 'Underlying source: calling enqueue on an empty canceled stream should not th
row'); |
| 158 |
| 159 promise_test(t => { |
| 160 |
| 161 let controller; |
| 162 const rs = new ReadableStream({ |
| 163 start(c) { |
| 164 c.enqueue('a'); |
| 165 c.enqueue('b'); |
| 166 controller = c; |
| 167 } |
| 168 }); |
| 169 |
| 170 rs.cancel(); |
| 171 controller.enqueue('c'); // Calling enqueue after canceling should not throw a
nything. |
| 172 |
| 173 return rs.getReader().closed; |
| 174 |
| 175 }, 'Underlying source: calling enqueue on a non-empty canceled stream should not
throw'); |
| 176 |
| 177 promise_test(t => { |
| 178 |
| 179 return new ReadableStream({ |
| 180 start(c) { |
| 181 c.close(); |
| 182 assert_throws(new TypeError(), () => c.enqueue('a'), 'call to enqueue shou
ld throw a TypeError'); |
| 183 } |
| 184 }).getReader().closed; |
| 185 |
| 186 }, 'Underlying source: calling enqueue on a closed stream should throw'); |
| 187 |
| 188 promise_test(t => { |
| 189 |
| 190 const theError = new Error('boo'); |
| 191 const closed = new ReadableStream({ |
| 192 start(c) { |
| 193 c.error(theError); |
| 194 assert_throws(theError, () => c.enqueue('a'), 'call to enqueue should thro
w the error'); |
| 195 } |
| 196 }).getReader().closed; |
| 197 |
| 198 return promise_rejects(t, theError, closed); |
| 199 |
| 200 }, 'Underlying source: calling enqueue on an errored stream should throw'); |
| 201 |
| 202 promise_test(t => { |
| 203 |
| 204 return new ReadableStream({ |
| 205 start(c) { |
| 206 c.close(); |
| 207 assert_throws(new TypeError(), () => c.close(), 'second call to close shou
ld throw a TypeError'); |
| 208 } |
| 209 }).getReader().closed; |
| 210 |
| 211 }, 'Underlying source: calling close twice on an empty stream should throw the s
econd time'); |
| 212 |
| 213 promise_test(t => { |
| 214 |
| 215 let startCalled = false; |
| 216 let readCalled = false; |
| 217 const reader = new ReadableStream({ |
| 218 start(c) { |
| 219 c.enqueue('a'); |
| 220 c.close(); |
| 221 assert_throws(new TypeError(), () => c.close(), 'second call to close shou
ld throw a TypeError'); |
| 222 startCalled = true; |
| 223 } |
| 224 }).getReader(); |
| 225 |
| 226 return Promise.all([ |
| 227 reader.read().then(r => { |
| 228 assert_object_equals(r, { value: 'a', done: false }, 'read() should read t
he enqueued chunk'); |
| 229 readCalled = true; |
| 230 }), |
| 231 reader.closed.then(() => { |
| 232 assert_true(startCalled); |
| 233 assert_true(readCalled); |
| 234 }) |
| 235 ]); |
| 236 |
| 237 }, 'Underlying source: calling close twice on a non-empty stream should throw th
e second time'); |
| 238 |
| 239 promise_test(t => { |
| 240 let controller; |
| 241 let startCalled = false; |
| 242 const rs = new ReadableStream({ |
| 243 start(c) { |
| 244 controller = c; |
| 245 startCalled = true; |
| 246 } |
| 247 }); |
| 248 |
| 249 rs.cancel(); |
| 250 controller.close(); // Calling close after canceling should not throw anything
. |
| 251 |
| 252 return rs.getReader().closed.then(() => { |
| 253 assert_true(startCalled); |
| 254 }); |
| 255 |
| 256 }, 'Underlying source: calling close on an empty canceled stream should not th
row'); |
| 257 |
| 258 promise_test(t => { |
| 259 |
| 260 let controller; |
| 261 let startCalled = false; |
| 262 const rs = new ReadableStream({ |
| 263 start(c) { |
| 264 controller = c; |
| 265 c.enqueue('a'); |
| 266 startCalled = true; |
| 267 } |
| 268 }); |
| 269 |
| 270 rs.cancel(); |
| 271 controller.close(); // Calling close after canceling should not throw anything
. |
| 272 |
| 273 return rs.getReader().closed.then(() => { |
| 274 assert_true(startCalled); |
| 275 }); |
| 276 |
| 277 }, 'Underlying source: calling close on a non-empty canceled stream should not t
hrow'); |
| 278 |
| 279 promise_test(t => { |
| 280 |
| 281 const theError = new Error('boo'); |
| 282 let startCalled = false; |
| 283 |
| 284 const closed = new ReadableStream({ |
| 285 start(c) { |
| 286 c.error(theError); |
| 287 assert_throws(new TypeError(), () => c.close(), 'call to close should thro
w a TypeError'); |
| 288 startCalled = true; |
| 289 } |
| 290 }).getReader().closed; |
| 291 |
| 292 return closed.catch(e => { |
| 293 assert_true(startCalled); |
| 294 assert_equals(e, theError, 'closed should reject with the error'); |
| 295 }); |
| 296 |
| 297 }, 'Underlying source: calling close after error should throw'); |
| 298 |
| 299 promise_test(t => { |
| 300 |
| 301 const theError = new Error('boo'); |
| 302 let startCalled = false; |
| 303 |
| 304 const closed = new ReadableStream({ |
| 305 start(c) { |
| 306 c.error(theError); |
| 307 assert_throws(new TypeError(), () => c.error(), 'second call to error shou
ld throw a TypeError'); |
| 308 startCalled = true; |
| 309 } |
| 310 }).getReader().closed; |
| 311 |
| 312 return closed.catch(e => { |
| 313 assert_true(startCalled); |
| 314 assert_equals(e, theError, 'closed should reject with the error'); |
| 315 }); |
| 316 |
| 317 }, 'Underlying source: calling error twice should throw the second time'); |
| 318 |
| 319 promise_test(t => { |
| 320 |
| 321 let startCalled = false; |
| 322 |
| 323 const closed = new ReadableStream({ |
| 324 start(c) { |
| 325 c.close(); |
| 326 assert_throws(new TypeError(), () => c.error(), 'second call to error shou
ld throw a TypeError'); |
| 327 startCalled = true; |
| 328 } |
| 329 }).getReader().closed; |
| 330 |
| 331 return closed.then(() => assert_true(startCalled)); |
| 332 |
| 333 }, 'Underlying source: calling error after close should throw'); |
| 334 |
| 335 promise_test(t => { |
| 336 |
| 337 let startCalled = false; |
| 338 const firstError = new Error('1'); |
| 339 const secondError = new Error('2'); |
| 340 |
| 341 const closed = new ReadableStream({ |
| 342 start(c) { |
| 343 c.error(firstError); |
| 344 startCalled = true; |
| 345 return Promise.reject(secondError); |
| 346 } |
| 347 }).getReader().closed; |
| 348 |
| 349 return closed.catch(e => { |
| 350 assert_true(startCalled); |
| 351 assert_equals(e, firstError, 'closed should reject with the first error'); |
| 352 }); |
| 353 |
| 354 }, 'Underlying source: calling error and returning a rejected promise from start
should cause the stream to error ' + |
| 355 'with the first error'); |
| 356 |
| 357 promise_test(t => { |
| 358 |
| 359 let startCalled = false; |
| 360 const firstError = new Error('1'); |
| 361 const secondError = new Error('2'); |
| 362 |
| 363 const closed = new ReadableStream({ |
| 364 pull(c) { |
| 365 c.error(firstError); |
| 366 startCalled = true; |
| 367 return Promise.reject(secondError); |
| 368 } |
| 369 }).getReader().closed; |
| 370 |
| 371 return closed.catch(e => { |
| 372 assert_true(startCalled); |
| 373 assert_equals(e, firstError, 'closed should reject with the first error'); |
| 374 }); |
| 375 |
| 376 }, 'Underlying source: calling error and returning a rejected promise from pull
should cause the stream to error ' + |
| 377 'with the first error'); |
| 378 |
| 379 done(); |
OLD | NEW |