OLD | NEW |
(Empty) | |
| 1 'use strict'; |
| 2 |
| 3 if (self.importScripts) { |
| 4 self.importScripts('../resources/rs-utils.js'); |
| 5 self.importScripts('/resources/testharness.js'); |
| 6 } |
| 7 |
| 8 test(function() { |
| 9 new ReadableStream(); // ReadableStream constructed with no parameters. |
| 10 new ReadableStream({ }); // ReadableStream constructed with an empty object
as parameter. |
| 11 new ReadableStream(undefined); // ReadableStream constructed with undefined
as parameter. |
| 12 var x; |
| 13 new ReadableStream(x) // ReadableStream constructed with an undefined variab
le as parameter. |
| 14 }, 'ReadableStream can be constructed with no errors'); |
| 15 |
| 16 test(function() { |
| 17 assert_throws(new TypeError(), function() { new ReadableStream(null); }, 'co
nstructor should throw when the source is null'); |
| 18 }, 'ReadableStream can\'t be constructed with garbage'); |
| 19 |
| 20 test(function() { |
| 21 var methods = ['cancel', 'constructor', 'getReader', 'tee']; |
| 22 var properties = methods.concat(['locked']).sort(); |
| 23 |
| 24 var rs = new ReadableStream(); |
| 25 var proto = Object.getPrototypeOf(rs); |
| 26 |
| 27 assert_array_equals(Object.getOwnPropertyNames(proto).sort(), properties, 's
hould have all the correct methods'); |
| 28 |
| 29 for (var m of methods) { |
| 30 var propDesc = Object.getOwnPropertyDescriptor(proto, m); |
| 31 assert_false(propDesc.enumerable, 'method should be non-enumerable'); |
| 32 assert_true(propDesc.configurable, 'method should be configurable'); |
| 33 assert_true(propDesc.writable, 'method should be writable'); |
| 34 assert_equals(typeof rs[m], 'function', 'should have be a method'); |
| 35 } |
| 36 |
| 37 var lockedPropDesc = Object.getOwnPropertyDescriptor(proto, 'locked'); |
| 38 assert_false(lockedPropDesc.enumerable, 'locked should be non-enumerable'); |
| 39 assert_equals(lockedPropDesc.writable, undefined, 'locked should not be a da
ta property'); |
| 40 assert_equals(typeof lockedPropDesc.get, 'function', 'locked should have a g
etter'); |
| 41 assert_equals(lockedPropDesc.set, undefined, 'locked should not have a sette
r'); |
| 42 assert_true(lockedPropDesc.configurable, 'locked should be configurable'); |
| 43 |
| 44 assert_equals(rs.cancel.length, 1, 'cancel should have 1 parameter'); |
| 45 assert_equals(rs.constructor.length, 0, 'constructor should have no paramete
rs'); |
| 46 assert_equals(rs.getReader.length, 0, 'getReader should have no parameters')
; |
| 47 assert_equals(rs.tee.length, 0, 'tee should have no parameters'); |
| 48 }, 'ReadableStream instances should have the correct list of properties'); |
| 49 |
| 50 test(function() { |
| 51 assert_throws(new TypeError(), function() { |
| 52 new ReadableStream({ start: 'potato'}); |
| 53 }, 'constructor should throw when start is not a function'); |
| 54 }, 'ReadableStream constructor should throw for non-function start arguments'); |
| 55 |
| 56 test(function() { |
| 57 new ReadableStream({ cancel: '2'}); // Constructor should not throw when can
cel is not a function. |
| 58 }, 'ReadableStream constructor can get initial garbage as cancel argument'); |
| 59 |
| 60 test(function() { |
| 61 new ReadableStream({ pull: { } }); // Constructor should not throw when pull
is not a function. |
| 62 }, 'ReadableStream constructor can get initial garbage as pull argument'); |
| 63 |
| 64 test(function() { |
| 65 var startCalled = false; |
| 66 var source = { |
| 67 start: function(controller) { |
| 68 assert_equals(this, source, 'source is this during start'); |
| 69 |
| 70 var methods = ['close', 'enqueue', 'error', 'constructor']; |
| 71 var properties = ['desiredSize'].concat(methods).sort(); |
| 72 var proto = Object.getPrototypeOf(controller); |
| 73 |
| 74 assert_array_equals(Object.getOwnPropertyNames(proto).sort(), proper
ties, |
| 75 'the controller should have the right properties
'); |
| 76 |
| 77 for (var m of methods) { |
| 78 var propDesc = Object.getOwnPropertyDescriptor(proto, m); |
| 79 assert_equals(typeof controller[m], 'function', `should have a $
{m} method`); |
| 80 assert_false(propDesc.enumerable, m + " should be non-enumerable
"); |
| 81 assert_true(propDesc.configurable, m + " should be configurable"
); |
| 82 assert_true(propDesc.writable, m + " should be writable"); |
| 83 } |
| 84 |
| 85 var desiredSizePropDesc = Object.getOwnPropertyDescriptor(proto, 'de
siredSize'); |
| 86 assert_false(desiredSizePropDesc.enumerable, 'desiredSize should be
non-enumerable'); |
| 87 assert_equals(desiredSizePropDesc.writable, undefined, 'desiredSize
should not be a data property'); |
| 88 assert_equals(typeof desiredSizePropDesc.get, 'function', 'desiredSi
ze should have a getter'); |
| 89 assert_equals(desiredSizePropDesc.set, undefined, 'desiredSize shoul
d not have a setter'); |
| 90 assert_true(desiredSizePropDesc.configurable, 'desiredSize should be
configurable'); |
| 91 |
| 92 assert_equals(controller.close.length, 0, 'close should have no para
meters'); |
| 93 assert_equals(controller.constructor.length, 1, 'constructor should
have 1 parameter'); |
| 94 assert_equals(controller.enqueue.length, 1, 'enqueue should have 1 p
arameter'); |
| 95 assert_equals(controller.error.length, 1, 'error should have 1 param
eter'); |
| 96 |
| 97 startCalled = true; |
| 98 } |
| 99 }; |
| 100 |
| 101 new ReadableStream(source); |
| 102 assert_true(startCalled); |
| 103 }, 'ReadableStream start should be called with the proper parameters'); |
| 104 |
| 105 test(function() { |
| 106 var startCalled = false; |
| 107 var source = { |
| 108 start: function(controller) { |
| 109 var properties = ['close', 'constructor', 'desiredSize', 'enqueue',
'error']; |
| 110 assert_array_equals(Object.getOwnPropertyNames(Object.getPrototypeOf
(controller)).sort(), properties, |
| 111 'prototype should have the right properties'); |
| 112 controller.test = ''; |
| 113 assert_array_equals(Object.getOwnPropertyNames(Object.getPrototypeOf
(controller)).sort(), properties, |
| 114 'prototype should still have the right propertie
s'); |
| 115 assert_not_equals(Object.getOwnPropertyNames(controller).indexOf('te
st'), -1, |
| 116 '"test" should be a property of the controller'); |
| 117 |
| 118 startCalled = true; |
| 119 } |
| 120 }; |
| 121 |
| 122 var rs = new ReadableStream(source); |
| 123 assert_true(startCalled); |
| 124 }, 'ReadableStream start controller parameter should be extensible'); |
| 125 |
| 126 var test1 = async_test('ReadableStream should be able to call start method withi
n prototype chain of its source'); |
| 127 test1.step(function() |
| 128 { |
| 129 function SimpleStreamSource() { |
| 130 }; |
| 131 SimpleStreamSource.prototype = { |
| 132 start: function() { |
| 133 test1.done('start should be called'); |
| 134 }, |
| 135 } |
| 136 |
| 137 new ReadableStream(new SimpleStreamSource()); |
| 138 }); |
| 139 |
| 140 var test2 = async_test('ReadableStream start should be able to return a promise'
); |
| 141 test2.step(function() |
| 142 { |
| 143 var readCalled = false; |
| 144 var rs = new ReadableStream({ |
| 145 start: function(c) { |
| 146 return new Promise(test2.step_func(function(resolve, reject) { |
| 147 setTimeout(test2.step_func(function() { |
| 148 c.enqueue('a'); |
| 149 c.close(); |
| 150 resolve(); |
| 151 }), 500); |
| 152 })); |
| 153 }, |
| 154 }); |
| 155 |
| 156 var reader = rs.getReader(); |
| 157 |
| 158 reader.read().then(test2.step_func(function(r) { |
| 159 readCalled = true; |
| 160 assert_object_equals(r, { value: 'a', done: false }, 'value read should
be the one enqueued'); |
| 161 })); |
| 162 |
| 163 reader.closed.then(test2.step_func(function() { |
| 164 assert_true(readCalled); |
| 165 test2.done('stream should close successfully'); |
| 166 })); |
| 167 }); |
| 168 |
| 169 var test3 = async_test('ReadableStream start should be able to return a promise
and reject it'); |
| 170 test3.step(function() |
| 171 { |
| 172 var theError = new Error('rejected!'); |
| 173 var rs = new ReadableStream({ |
| 174 start: function() { |
| 175 return new Promise(test3.step_func(function(resolve, reject) { |
| 176 setTimeout(test3.step_func(function() { |
| 177 reject(theError); |
| 178 }), 500); |
| 179 })); |
| 180 }, |
| 181 }); |
| 182 |
| 183 rs.getReader().closed.catch(test3.step_func(function(e) { |
| 184 assert_equals(e, theError, 'promise should be rejected with the same err
or'); |
| 185 test3.done(); |
| 186 })); |
| 187 }); |
| 188 |
| 189 var test4 = async_test('ReadableStream should be able to enqueue different objec
ts.'); |
| 190 test4.step(function() { |
| 191 var readCalls = 0; |
| 192 var objects = [ |
| 193 { potato: 'Give me more!'}, |
| 194 'test', |
| 195 1 |
| 196 ]; |
| 197 |
| 198 var rs = new ReadableStream({ |
| 199 start: function(c) { |
| 200 for (var o of objects) { |
| 201 c.enqueue(o); |
| 202 } |
| 203 c.close(); |
| 204 } |
| 205 }); |
| 206 |
| 207 var reader = rs.getReader(); |
| 208 |
| 209 reader.read().then(test4.step_func(function(r) { |
| 210 assert_object_equals(r, { value: objects[readCalls++], done: false }, 'v
alue read should be the one enqueued'); |
| 211 })); |
| 212 |
| 213 reader.read().then(test4.step_func(function(r) { |
| 214 assert_object_equals(r, { value: objects[readCalls++], done: false }, 'v
alue read should be the one enqueued'); |
| 215 })); |
| 216 |
| 217 reader.read().then(test4.step_func(function(r) { |
| 218 assert_object_equals(r, { value: objects[readCalls++], done: false }, 'v
alue read should be the one enqueued'); |
| 219 })); |
| 220 |
| 221 reader.closed.then(test4.step_func(function() { |
| 222 assert_equals(readCalls, 3); |
| 223 test4.done('stream should close correctly correctly'); |
| 224 })); |
| 225 }); |
| 226 |
| 227 var test5 = async_test('ReadableStream: if pull rejects, it should error the str
eam'); |
| 228 test5.step(function() { |
| 229 var error = new Error('pull failure'); |
| 230 var rs = new ReadableStream({ |
| 231 pull: function() { |
| 232 return Promise.reject(error); |
| 233 } |
| 234 }); |
| 235 |
| 236 var reader = rs.getReader(); |
| 237 |
| 238 var closed = false; |
| 239 var read = false; |
| 240 |
| 241 reader.closed.catch(test5.step_func(function(e) { |
| 242 closed = true; |
| 243 assert_true(read); |
| 244 assert_equals(e, error, 'closed should reject with the thrown error'); |
| 245 test5.done(); |
| 246 })); |
| 247 |
| 248 reader.read().catch(test5.step_func(function(e) { |
| 249 read = true; |
| 250 assert_false(closed); |
| 251 assert_equals(e, error, 'read() should reject with the thrown error'); |
| 252 })); |
| 253 }); |
| 254 |
| 255 var test6 = async_test('ReadableStream: should only call pull once upon starting
the stream'); |
| 256 test6.step(function() { |
| 257 var pullCount = 0; |
| 258 var startPromise = Promise.resolve(); |
| 259 var rs = new ReadableStream({ |
| 260 start: function() { |
| 261 return startPromise; |
| 262 }, |
| 263 pull: function() { |
| 264 pullCount++; |
| 265 } |
| 266 }); |
| 267 |
| 268 startPromise.then(test6.step_func(function() { |
| 269 assert_equals(pullCount, 1, 'pull should be called once start finishes')
; |
| 270 |
| 271 setTimeout(test6.step_func(function() { |
| 272 assert_equals(pullCount, 1, 'pull should be called exactly once'); |
| 273 test6.done(); |
| 274 }), 1000); |
| 275 })); |
| 276 |
| 277 }); |
| 278 |
| 279 var test7 = async_test('ReadableStream: should call pull when trying to read fro
m a started, empty stream'); |
| 280 test7.step(function() { |
| 281 var pullCount = 0; |
| 282 var startPromise = Promise.resolve(); |
| 283 var rs = new ReadableStream({ |
| 284 start: function() { |
| 285 return startPromise; |
| 286 }, |
| 287 pull: function(c) { |
| 288 // Don't enqueue immediately after start. We want the stream to be e
mpty when we call .read() on it. |
| 289 if (pullCount > 0) { |
| 290 c.enqueue(pullCount); |
| 291 } |
| 292 |
| 293 ++pullCount; |
| 294 } |
| 295 }); |
| 296 |
| 297 startPromise.then(test7.step_func(function() { |
| 298 assert_equals(pullCount, 1, 'pull should be called once start finishes')
; |
| 299 |
| 300 var reader = rs.getReader(); |
| 301 return reader.read().then(test7.step_func(function(result) { |
| 302 assert_equals(pullCount, 2, 'pull should be called again in reaction
to calling read'); |
| 303 assert_object_equals(result, { value: 1, done: false }, 'the result
read should be the one enqueued'); |
| 304 test7.done(); |
| 305 })); |
| 306 })).catch(test7.step_func(function(e) { assert_unreached(e); })); |
| 307 }); |
| 308 |
| 309 var test8 = async_test('ReadableStream: should only call pull once on a non-empt
y stream read from before start fulfills'); |
| 310 test8.step(function() { |
| 311 var pullCount = 0; |
| 312 var startPromise = Promise.resolve(); |
| 313 var rs = new ReadableStream({ |
| 314 start: function(c) { |
| 315 c.enqueue('a'); |
| 316 return startPromise; |
| 317 }, |
| 318 pull: function() { |
| 319 pullCount++; |
| 320 } |
| 321 }); |
| 322 |
| 323 startPromise.then(test8.step_func(function() { |
| 324 assert_equals(pullCount, 1, 'pull should be called once start finishes')
; |
| 325 })); |
| 326 |
| 327 rs.getReader().read().then(test8.step_func(function(r) { |
| 328 assert_object_equals(r, { value: 'a', done: false }, 'first read() shoul
d return first chunk'); |
| 329 assert_equals(pullCount, 1, 'pull should not have been called again'); |
| 330 |
| 331 setTimeout(test8.step_func(function() { |
| 332 assert_equals(pullCount, 1, 'pull should be called exactly once'); |
| 333 test8.done(); |
| 334 }), 1000); |
| 335 })); |
| 336 |
| 337 assert_equals(pullCount, 0, 'calling read() should not cause pull to be call
ed yet'); |
| 338 }); |
| 339 |
| 340 var test9 = async_test('ReadableStream: should only call pull once on a non-empt
y stream read from after start fulfills'); |
| 341 test9.step(function() { |
| 342 var pullCount = 0; |
| 343 var startPromise = Promise.resolve(); |
| 344 var rs = new ReadableStream({ |
| 345 start: function(c) { |
| 346 c.enqueue('a'); |
| 347 return startPromise; |
| 348 }, |
| 349 pull: function() { |
| 350 pullCount++; |
| 351 } |
| 352 }); |
| 353 |
| 354 startPromise.then(test9.step_func(function() { |
| 355 assert_equals(pullCount, 0, 'pull should not be called once start finish
es, since the queue is full'); |
| 356 |
| 357 rs.getReader().read().then(test9.step_func(function(r) { |
| 358 assert_object_equals(r, { value: 'a', done: false }, 'first read() s
hould return first chunk'); |
| 359 |
| 360 setTimeout(test9.step_func(function() { |
| 361 assert_equals(pullCount, 1, 'pull should be called exactly once'
); |
| 362 test9.done(); |
| 363 }), 1000); |
| 364 |
| 365 })); |
| 366 |
| 367 assert_equals(pullCount, 1, 'calling read() should not cause pull to be
called immediately'); |
| 368 })); |
| 369 }); |
| 370 |
| 371 var test10 = async_test('ReadableStream: should call pull in reaction to read()i
ng the last chunk, if not draining'); |
| 372 test10.step(function() { |
| 373 var pullCount = 0; |
| 374 var controller; |
| 375 var startPromise = Promise.resolve(); |
| 376 var rs = new ReadableStream({ |
| 377 start: function(c) { |
| 378 controller = c; |
| 379 return startPromise; |
| 380 }, |
| 381 pull: function() { |
| 382 ++pullCount; |
| 383 } |
| 384 }); |
| 385 |
| 386 var reader = rs.getReader(); |
| 387 |
| 388 startPromise.then(test10.step_func(function() { |
| 389 assert_equals(pullCount, 1, 'pull should have been called once by the ti
me the stream starts'); |
| 390 |
| 391 controller.enqueue('a'); |
| 392 assert_equals(pullCount, 1, 'pull should not have been called again afte
r enqueue'); |
| 393 |
| 394 return reader.read().then(test10.step_func(function() { |
| 395 assert_equals(pullCount, 2, 'pull should have been called again afte
r read'); |
| 396 |
| 397 setTimeout(test10.step_func(function() { |
| 398 assert_equals(pullCount, 2, 'pull should be called exactly twice
'); |
| 399 test10.done(); |
| 400 }), 500); |
| 401 })); |
| 402 })).catch(test10.step_func(function(e) { assert_unreached(e); })); |
| 403 }); |
| 404 |
| 405 var test11 = async_test('ReadableStream: should not call pull() in reaction to r
ead()ing the last chunk, if draining'); |
| 406 test11.step(function() { |
| 407 var pullCount = 0; |
| 408 var controller; |
| 409 var startPromise = Promise.resolve(); |
| 410 var pullPromise = Promise.resolve(); |
| 411 var rs = new ReadableStream({ |
| 412 start: function(c) { |
| 413 controller = c; |
| 414 return startPromise; |
| 415 }, |
| 416 pull: function() { |
| 417 ++pullCount; |
| 418 return pullPromise; |
| 419 } |
| 420 }); |
| 421 |
| 422 var reader = rs.getReader(); |
| 423 |
| 424 startPromise.then(test11.step_func(function() { |
| 425 assert_equals(pullCount, 1, 'pull should have been called once by the ti
me the stream starts'); |
| 426 |
| 427 controller.enqueue('a'); |
| 428 assert_equals(pullCount, 1, 'pull should not have been called again afte
r enqueue'); |
| 429 |
| 430 controller.close(); |
| 431 |
| 432 return reader.read().then(test11.step_func(function() { |
| 433 assert_equals(pullCount, 1, 'pull should not have been called a seco
nd time after read'); |
| 434 |
| 435 setTimeout(test11.step_func(function() { |
| 436 assert_equals(pullCount, 1, 'pull should be called exactly once'
); |
| 437 test11.done(); |
| 438 }), 1000); |
| 439 })); |
| 440 })).catch(test11.step_func(function(e) { assert_unreached(e); })); |
| 441 }); |
| 442 |
| 443 var test12 = async_test('ReadableStream: should not call pull until the previous
pull call\'s promise fulfills'); |
| 444 test12.step(function() { |
| 445 var resolve; |
| 446 var returnedPromise; |
| 447 var timesCalled = 0; |
| 448 var startPromise = Promise.resolve(); |
| 449 var rs = new ReadableStream({ |
| 450 start: function() { |
| 451 return startPromise; |
| 452 }, |
| 453 pull: function(c) { |
| 454 c.enqueue(++timesCalled); |
| 455 returnedPromise = new Promise(test12.step_func(function(r) { resolve
= r; })); |
| 456 return returnedPromise; |
| 457 } |
| 458 }); |
| 459 var reader = rs.getReader(); |
| 460 |
| 461 startPromise.then(test12.step_func(function() { |
| 462 return reader.read().then(test12.step_func(function(result1) { |
| 463 assert_equals(timesCalled, 1, |
| 464 'pull should have been called once after start, but no
t yet have been called a second time'); |
| 465 assert_object_equals(result1, { value: 1, done: false }, 'read() sho
uld fulfill with the enqueued value'); |
| 466 |
| 467 setTimeout(test12.step_func(function() { |
| 468 assert_equals(timesCalled, 1, 'after 30 ms, pull should still on
ly have been called once'); |
| 469 |
| 470 resolve(); |
| 471 |
| 472 returnedPromise.then(test12.step_func(function() { |
| 473 assert_equals(timesCalled, 2, |
| 474 'after the promise returned by pull is fulfill
ed, pull should be called a second time'); |
| 475 test12.done(); |
| 476 })); |
| 477 }), 500); |
| 478 })); |
| 479 })).catch(test12.step_func(function(e) { assert_unreached(e); })); |
| 480 }); |
| 481 |
| 482 var test13 = async_test('ReadableStream: should pull after start, and after ever
y read'); |
| 483 test13.step(function() { |
| 484 var timesCalled = 0; |
| 485 var startPromise = Promise.resolve(); |
| 486 var rs = new ReadableStream({ |
| 487 start: function(c) { |
| 488 c.enqueue('a'); |
| 489 c.enqueue('b'); |
| 490 c.enqueue('c'); |
| 491 return startPromise; |
| 492 }, |
| 493 pull() { |
| 494 ++timesCalled; |
| 495 } |
| 496 }, |
| 497 { |
| 498 size: function() { |
| 499 return 1; |
| 500 }, |
| 501 highWaterMark: Infinity |
| 502 }); |
| 503 var reader = rs.getReader(); |
| 504 |
| 505 startPromise.then(test13.step_func(function() { |
| 506 return reader.read().then(test13.step_func(function(result1) { |
| 507 assert_object_equals(result1, { value: 'a', done: false }, 'first ch
unk should be as expected'); |
| 508 |
| 509 return reader.read().then(test13.step_func(function(result2) { |
| 510 assert_object_equals(result2, { value: 'b', done: false }, 'seco
nd chunk should be as expected'); |
| 511 |
| 512 return reader.read().then(test13.step_func(function(result3) { |
| 513 assert_object_equals(result3, { value: 'c', done: false }, '
third chunk should be as expected'); |
| 514 |
| 515 setTimeout(test13.step_func(function() { |
| 516 // Once for after start, and once for every read. |
| 517 assert_equals(timesCalled, 4, 'pull() should be called e
xactly four times'); |
| 518 test13.done(); |
| 519 }), 1000); |
| 520 })); |
| 521 })); |
| 522 })); |
| 523 })).catch(test13.step_func(function(e) { assert_unreached(e); })); |
| 524 }); |
| 525 |
| 526 var test14 = async_test('ReadableStream: should not call pull after start if the
stream is now closed'); |
| 527 test14.step(function() { |
| 528 var timesCalled = 0; |
| 529 var startPromise = Promise.resolve(); |
| 530 var rs = new ReadableStream({ |
| 531 start: function(c) { |
| 532 c.enqueue('a'); |
| 533 c.close(); |
| 534 return startPromise; |
| 535 }, |
| 536 pull: function() { |
| 537 ++timesCalled; |
| 538 } |
| 539 }); |
| 540 |
| 541 startPromise.then(test14.step_func(function() { |
| 542 assert_equals(timesCalled, 0, 'after start finishes, pull should not hav
e been called'); |
| 543 |
| 544 var reader = rs.getReader(); |
| 545 return reader.read().then(test14.step_func(function() { |
| 546 assert_equals(timesCalled, 0, 'reading should not have triggered a p
ull call'); |
| 547 |
| 548 return reader.closed.then(test14.step_func(function() { |
| 549 assert_equals(timesCalled, 0, 'stream should have closed with st
ill no calls to pull'); |
| 550 test14.done(); |
| 551 })); |
| 552 })); |
| 553 })).catch(test14.step_func(function(e) { assert_unreached(e); })); |
| 554 }); |
| 555 |
| 556 var test15 = async_test('ReadableStream: should call pull after enqueueing from
inside pull (with no read requests), if strategy allows'); |
| 557 test15.step(function() { |
| 558 var timesCalled = 0; |
| 559 var startPromise = Promise.resolve(); |
| 560 var rs = new ReadableStream({ |
| 561 start: function() { |
| 562 return startPromise; |
| 563 }, |
| 564 pull: function(c) { |
| 565 c.enqueue(++timesCalled); |
| 566 |
| 567 if (timesCalled == 4) { |
| 568 setTimeout(test15.step_func(function() { |
| 569 // after start: size = 0, pull() |
| 570 // after enqueue(1): size = 1, pull() |
| 571 // after enqueue(2): size = 2, pull() |
| 572 // after enqueue(3): size = 3, pull() |
| 573 // after enqueue(4): size = 4, do not pull |
| 574 assert_equals(timesCalled, 4, 'pull() should have been calle
d four times'); |
| 575 test15.done(); |
| 576 }), 1000); |
| 577 } |
| 578 } |
| 579 }, |
| 580 { |
| 581 size: function() { |
| 582 return 1; |
| 583 }, |
| 584 highWaterMark: 4 |
| 585 }); |
| 586 }); |
| 587 |
| 588 var test16 = async_test('ReadableStream pull should be able to close a stream.')
; |
| 589 test16.step(function() { |
| 590 var pullCalled = false; |
| 591 var rs = new ReadableStream({ |
| 592 pull: function(c) { |
| 593 pullCalled = true; |
| 594 c.close(); |
| 595 } |
| 596 }); |
| 597 |
| 598 var reader = rs.getReader(); |
| 599 reader.closed.then(test16.step_func(function() { |
| 600 assert_true(pullCalled); |
| 601 test16.done('stream was closed successfully'); |
| 602 })); |
| 603 }); |
| 604 |
| 605 test(function() { |
| 606 var startCalled = false; |
| 607 var rs = new ReadableStream({ |
| 608 start: function(c) { |
| 609 assert_equals(c.enqueue('a'), undefined, 'the first enqueue should r
eturn undefined'); |
| 610 c.close(); |
| 611 |
| 612 assert_throws(new TypeError(''), function() { c.enqueue('b'); }, 'en
queue after close should throw a TypeError'); |
| 613 startCalled = true; |
| 614 } |
| 615 }); |
| 616 assert_true(startCalled); |
| 617 }, 'ReadableStream: enqueue should throw when the stream is readable but drainin
g'); |
| 618 |
| 619 test(function() { |
| 620 var startCalled = false; |
| 621 var rs = new ReadableStream({ |
| 622 start: function(c) { |
| 623 c.close(); |
| 624 |
| 625 assert_throws(new TypeError(), function() { c.enqueue('a'); }, 'enqu
eue after close should throw a TypeError'); |
| 626 startCalled = true; |
| 627 } |
| 628 }); |
| 629 assert_true(startCalled); |
| 630 }, 'ReadableStream: enqueue should throw when the stream is closed'); |
| 631 |
| 632 test(function() { |
| 633 var startCalled = false; |
| 634 var expectedError = new Error('i am sad'); |
| 635 var rs = new ReadableStream({ |
| 636 start: function(c) { |
| 637 c.error(expectedError); |
| 638 |
| 639 assert_throws(expectedError, function() { c.enqueue('a'); }, 'enqueu
e after error should throw that error'); |
| 640 startCalled = true; |
| 641 } |
| 642 }); |
| 643 assert_true(startCalled); |
| 644 }, 'ReadableStream: enqueue should throw the stored error when the stream is err
ored'); |
| 645 |
| 646 var test17 = async_test('ReadableStream: should call underlying source methods a
s methods'); |
| 647 test17.step(function() { |
| 648 var startCalled = 0; |
| 649 var pullCalled = 0; |
| 650 var cancelCalled = 0; |
| 651 |
| 652 function Source() { |
| 653 } |
| 654 |
| 655 Source.prototype = { |
| 656 start: function(c) { |
| 657 startCalled++; |
| 658 assert_equals(this, theSource, 'start() should be called with the co
rrect this'); |
| 659 c.enqueue('a'); |
| 660 }, |
| 661 |
| 662 pull: function() { |
| 663 pullCalled++; |
| 664 assert_equals(this, theSource, 'pull() should be called with the cor
rect this'); |
| 665 }, |
| 666 |
| 667 cancel: function() { |
| 668 cancelCalled++; |
| 669 assert_equals(this, theSource, 'cancel() should be called with the c
orrect this'); |
| 670 }, |
| 671 }; |
| 672 |
| 673 var theSource = new Source(); |
| 674 theSource.debugName = 'the source object passed to the constructor'; // make
s test failures easier to diagnose |
| 675 var rs = new ReadableStream(theSource); |
| 676 |
| 677 var reader = rs.getReader(); |
| 678 reader.read().then(test17.step_func(function() { |
| 679 reader.releaseLock(); |
| 680 rs.cancel(); |
| 681 assert_equals(startCalled, 1); |
| 682 assert_equals(pullCalled, 1); |
| 683 assert_equals(cancelCalled, 1); |
| 684 test17.done(); |
| 685 })).catch(test17.step_func(function(e) { assert_unreached(e); } )); |
| 686 }); |
| 687 |
| 688 test(function() { |
| 689 var startCalled = false; |
| 690 new ReadableStream({ |
| 691 start: function(c) { |
| 692 assert_equals(c.desiredSize, 1); |
| 693 c.enqueue('a'); |
| 694 assert_equals(c.desiredSize, 0); |
| 695 c.enqueue('b'); |
| 696 assert_equals(c.desiredSize, -1); |
| 697 c.enqueue('c'); |
| 698 assert_equals(c.desiredSize, -2); |
| 699 c.enqueue('d'); |
| 700 assert_equals(c.desiredSize, -3); |
| 701 c.enqueue('e'); |
| 702 startCalled = true; |
| 703 } |
| 704 }); |
| 705 assert_true(startCalled); |
| 706 }, 'ReadableStream strategies: the default strategy should give desiredSize of 1
to start, decreasing by 1 per enqueue'); |
| 707 |
| 708 var test18 = async_test('ReadableStream strategies: the default strategy should
continue giving desiredSize of 1 if the chunks are read immediately'); |
| 709 test18.step(function() { |
| 710 var controller; |
| 711 var rs = new ReadableStream({ |
| 712 start: function(c) { |
| 713 controller = c; |
| 714 } |
| 715 }); |
| 716 var reader = rs.getReader(); |
| 717 |
| 718 assert_equals(controller.desiredSize, 1, 'desiredSize should start at 1'); |
| 719 controller.enqueue('a'); |
| 720 assert_equals(controller.desiredSize, 0, 'desiredSize should decrease to 0 a
fter first enqueue'); |
| 721 |
| 722 reader.read().then(test18.step_func(function(result1) { |
| 723 assert_object_equals(result1, { value: 'a', done: false }, 'first chunk
read should be correct'); |
| 724 |
| 725 assert_equals(controller.desiredSize, 1, 'desiredSize should go up to 1
after the first read'); |
| 726 controller.enqueue('b'); |
| 727 assert_equals(controller.desiredSize, 0, 'desiredSize should go down to
0 after the second enqueue'); |
| 728 |
| 729 return reader.read(); |
| 730 })).then(test18.step_func(function(result2) { |
| 731 assert_object_equals(result2, { value: 'b', done: false }, 'second c
hunk read should be correct'); |
| 732 |
| 733 assert_equals(controller.desiredSize, 1, 'desiredSize should go up t
o 1 after the second read'); |
| 734 controller.enqueue('c'); |
| 735 assert_equals(controller.desiredSize, 0, 'desiredSize should go down
to 0 after the third enqueue'); |
| 736 |
| 737 return reader.read(); |
| 738 })).then(test18.step_func(function(result3) { |
| 739 assert_object_equals(result3, { value: 'c', done: false }, 'third ch
unk read should be correct'); |
| 740 |
| 741 assert_equals(controller.desiredSize, 1, 'desiredSize should go up t
o 1 after the third read'); |
| 742 controller.enqueue('d'); |
| 743 assert_equals(controller.desiredSize, 0, 'desiredSize should go down
to 0 after the fourth enqueue'); |
| 744 |
| 745 test18.done(); |
| 746 })).catch(test18.step_func(function(e) { assert_unreached(e); })); |
| 747 }); |
| 748 |
| 749 var test19 = async_test('ReadableStream integration test: adapting a random push
source'); |
| 750 test19.step(function() { |
| 751 var pullChecked = false; |
| 752 var randomSource = new RandomPushSource(8); |
| 753 |
| 754 var rs = new ReadableStream({ |
| 755 start: function(c) { |
| 756 assert_equals(typeof c, 'object', 'c should be an object in start'); |
| 757 assert_equals(typeof c.enqueue, 'function', 'enqueue should be a fun
ction in start'); |
| 758 assert_equals(typeof c.close, 'function', 'close should be a functio
n in start'); |
| 759 assert_equals(typeof c.error, 'function', 'error should be a functio
n in start'); |
| 760 |
| 761 randomSource.ondata = test19.step_func(function(chunk) { |
| 762 if (!c.enqueue(chunk) <= 0) { |
| 763 randomSource.readStop(); |
| 764 } |
| 765 }); |
| 766 |
| 767 randomSource.onend = c.close.bind(c); |
| 768 randomSource.onerror = c.error.bind(c); |
| 769 }, |
| 770 |
| 771 pull: function(c) { |
| 772 if (!pullChecked) { |
| 773 pullChecked = true; |
| 774 assert_equals(typeof c, 'object', 'c should be an object in pull
'); |
| 775 assert_equals(typeof c.enqueue, 'function', 'enqueue should be a
function in pull'); |
| 776 assert_equals(typeof c.close, 'function', 'close should be a fun
ction in pull'); |
| 777 } |
| 778 |
| 779 randomSource.readStart(); |
| 780 } |
| 781 }); |
| 782 |
| 783 readableStreamToArray(rs).then(test19.step_func(function(chunks) { |
| 784 assert_equals(chunks.length, 8, '8 chunks should be read'); |
| 785 for (var i = 0; i < chunks.length; i++) { |
| 786 assert_equals(chunks[i].length, 128, 'chunk should have 128 bytes'); |
| 787 } |
| 788 |
| 789 test19.done(); |
| 790 }), test19.step_func(function(e) { assert_unreached(e); })); |
| 791 }); |
| 792 |
| 793 var test20 = async_test('ReadableStream integration test: adapting a sync pull s
ource'); |
| 794 test20.step(function() { |
| 795 var rs = sequentialReadableStream(10); |
| 796 |
| 797 readableStreamToArray(rs).then(test20.step_func(function(chunks) { |
| 798 assert_true(rs.source.closed, 'source should be closed after all chunks
are read'); |
| 799 assert_array_equals(chunks, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'the expect
ed 10 chunks should be read'); |
| 800 |
| 801 test20.done(); |
| 802 })); |
| 803 }); |
| 804 |
| 805 var test21 = async_test('ReadableStream integration test: adapting an async pull
source'); |
| 806 test21.step(function() { |
| 807 var rs = sequentialReadableStream(10, { async: true }); |
| 808 |
| 809 readableStreamToArray(rs).then(test21.step_func(function(chunks) { |
| 810 assert_true(rs.source.closed, 'source should be closed after all chunks
are read'); |
| 811 assert_array_equals(chunks, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'the expect
ed 10 chunks should be read'); |
| 812 |
| 813 test21.done(); |
| 814 })); |
| 815 }); |
| 816 |
| 817 done(); |
OLD | NEW |