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