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