Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Created November 6, 2018 18:25
Show Gist options
  • Save jorendorff/f8fa7416fcb885d225f53be4eeaa0577 to your computer and use it in GitHub Desktop.
Save jorendorff/f8fa7416fcb885d225f53be4eeaa0577 to your computer and use it in GitHub Desktop.
changeset: 509196:cd2bd76439e0
user: Jason Orendorff <[email protected]>
date: Wed Oct 24 14:20:29 2018 -0500
files: js/src/tests/non262/ReadableStream/basic-pull.js js/src/tests/non262/ReadableStream/basic-push.js js/src/tests/non262/ReadableStream/shell.js
description:
Bug 1503012 - Part 1: Some basic ReadableStream tests that run in the shell. r?tcampbell
diff --git a/js/src/tests/non262/ReadableStream/basic-pull.js b/js/src/tests/non262/ReadableStream/basic-pull.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);
+
+ var 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);
diff --git a/js/src/tests/non262/ReadableStream/basic-push.js b/js/src/tests/non262/ReadableStream/basic-push.js
new file mode 100644
--- /dev/null
+++ b/js/src/tests/non262/ReadableStream/basic-push.js
@@ -0,0 +1,48 @@
+// |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 sleep(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);
diff --git a/js/src/tests/non262/ReadableStream/shell.js b/js/src/tests/non262/ReadableStream/shell.js
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 `sleep(n)` will become settled and fire its handlers
+// before a promise created with `sleep(n+1)`.
+//
+function sleep(ticks) {
+ let p = Promise.resolve();
+ if (ticks > 0) {
+ return p.then(() => sleep(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);
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment