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