mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-30 00:01:50 +00:00
Bug 1503012 - Part 1: Some basic ReadableStream tests that run in the shell. r=arai
Differential Revision: https://phabricator.services.mozilla.com/D11532 --HG-- extra : moz-landing-system : lando
This commit is contained in:
parent
c4c5775807
commit
7bd284ed9d
33
js/src/tests/non262/ReadableStream/basic-pull.js
Normal file
33
js/src/tests/non262/ReadableStream/basic-pull.js
Normal file
@ -0,0 +1,33 @@
|
||||
// |reftest| skip-if(!this.ReadableStream||!this.drainJobQueue)
|
||||
|
||||
// Example of a stream that produces data on demand, the "pull" model.
|
||||
let fibStream = new ReadableStream({
|
||||
start(controller) {
|
||||
this.a = 0;
|
||||
this.b = 1;
|
||||
controller.enqueue(0);
|
||||
controller.enqueue(1);
|
||||
},
|
||||
|
||||
pull(controller) {
|
||||
[this.a, this.b] = [this.b, this.a + this.b];
|
||||
controller.enqueue(this.b);
|
||||
}
|
||||
});
|
||||
|
||||
async function test() {
|
||||
assertEq(fibStream.locked, false);
|
||||
let reader = fibStream.getReader();
|
||||
assertEq(fibStream.locked, true);
|
||||
|
||||
let results = [];
|
||||
while (results.length < 10) {
|
||||
results.push((await reader.read()).value);
|
||||
}
|
||||
|
||||
assertEq(results.join(), "0,1,1,2,3,5,8,13,21,34");
|
||||
reader.releaseLock();
|
||||
assertEq(fibStream.locked, false);
|
||||
}
|
||||
|
||||
runAsyncTest(test);
|
49
js/src/tests/non262/ReadableStream/basic-push.js
Normal file
49
js/src/tests/non262/ReadableStream/basic-push.js
Normal file
@ -0,0 +1,49 @@
|
||||
// |reftest| skip-if(!this.ReadableStream||!this.drainJobQueue)
|
||||
|
||||
// Example of a stream that enqueues data asynchronously, whether the reader
|
||||
// wants it or not, the "push" model.
|
||||
let fbStream = new ReadableStream({
|
||||
start(controller) {
|
||||
simulatePacketsDriftingIn(controller);
|
||||
},
|
||||
});
|
||||
|
||||
async function simulatePacketsDriftingIn(controller) {
|
||||
for (let i = 1; i <= 30; i++) {
|
||||
let importantData =
|
||||
(i % 15 == 0 ? "FizzBuzz" :
|
||||
i % 5 == 0 ? "Buzz":
|
||||
i % 3 == 0 ? "Fizz" :
|
||||
String(i));
|
||||
controller.enqueue(importantData);
|
||||
await asyncSleep(1 + i % 7);
|
||||
}
|
||||
controller.close();
|
||||
}
|
||||
|
||||
const expected = [
|
||||
"1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz",
|
||||
"11", "Fizz", "13", "14", "FizzBuzz", "16", "17", "Fizz", "19", "Buzz",
|
||||
"Fizz", "22", "23", "Fizz", "Buzz", "26", "Fizz", "28", "29", "FizzBuzz"
|
||||
];
|
||||
|
||||
async function test() {
|
||||
assertEq(fbStream.locked, false);
|
||||
let reader = fbStream.getReader();
|
||||
assertEq(fbStream.locked, true);
|
||||
|
||||
let results = [];
|
||||
while (true) {
|
||||
let r = await reader.read();
|
||||
if (r.done) {
|
||||
break;
|
||||
}
|
||||
results.push(r.value);
|
||||
}
|
||||
|
||||
assertEq(results.join("-"), expected.join("-"));
|
||||
reader.releaseLock();
|
||||
assertEq(fbStream.locked, false);
|
||||
}
|
||||
|
||||
runAsyncTest(test);
|
30
js/src/tests/non262/ReadableStream/shell.js
Normal file
30
js/src/tests/non262/ReadableStream/shell.js
Normal file
@ -0,0 +1,30 @@
|
||||
// Return a promise that will resolve to `undefined` the next time jobs are
|
||||
// processed.
|
||||
//
|
||||
// `ticks` indicates how long the promise should "wait" before resolving: a
|
||||
// promise created with `asyncSleep(n)` will become settled and fire its handlers
|
||||
// before a promise created with `asyncSleep(n+1)`.
|
||||
//
|
||||
function asyncSleep(ticks) {
|
||||
let p = Promise.resolve();
|
||||
if (ticks > 0) {
|
||||
return p.then(() => asyncSleep(ticks - 1));
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
// Run the async function `test`. Wait for it to finish running. Throw if it
|
||||
// throws or if it fails to finish (awaiting a value forever).
|
||||
function runAsyncTest(test) {
|
||||
let passed = false;
|
||||
let problem = "test did not finish";
|
||||
test()
|
||||
.then(_ => { passed = true; })
|
||||
.catch(exc => { problem = exc; });
|
||||
drainJobQueue();
|
||||
if (!passed) {
|
||||
throw problem;
|
||||
}
|
||||
|
||||
reportCompare(0, 0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user