Bug 1503012 - Part 1: Some basic ReadableStream tests that run in the shell. r=arai
authorJason Orendorff <jorendorff@mozilla.com>
Fri, 16 Nov 2018 17:13:19 +0000
changeset 503227 96d08f7859d163479054a2835df1bf7d23a06916
parent 503226 0e6465e2cdf40c3b1f18e18c52e3285d49872c25
child 503228 efa160d5e7d6a69c69815903c137039a50a96fd0
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersarai
bugs1503012
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1503012 - Part 1: Some basic ReadableStream tests that run in the shell. r=arai Differential Revision: https://phabricator.services.mozilla.com/D11532
js/src/tests/non262/ReadableStream/basic-pull.js
js/src/tests/non262/ReadableStream/basic-push.js
js/src/tests/non262/ReadableStream/shell.js
new file mode 100644
--- /dev/null
+++ b/js/src/tests/non262/ReadableStream/basic-pull.js
@@ -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);
new file mode 100644
--- /dev/null
+++ b/js/src/tests/non262/ReadableStream/basic-push.js
@@ -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);
new file mode 100644
--- /dev/null
+++ b/js/src/tests/non262/ReadableStream/shell.js
@@ -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);
+}