Bug 1536170 - Add unit tests for Async.yieldingForEach() r=tcsc
☠☠ backed out by fd8c9a28b4d4 ☠ ☠
authorBarret Rennie <barret@brennie.ca>
Thu, 11 Apr 2019 18:39:54 +0000
changeset 469067 ccea2e827d9dff136b8f6ac9b357e3fabb5e3ec7
parent 469066 51a67bffd7d2670d12d87058973bc01e64f10096
child 469068 57c26f8e0bf7ab4e1d23bdbf68ed91c0bd925204
push id35856
push usercsabou@mozilla.com
push dateFri, 12 Apr 2019 03:19:48 +0000
treeherdermozilla-central@940684cd1065 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstcsc
bugs1536170
milestone68.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 1536170 - Add unit tests for Async.yieldingForEach() r=tcsc Differential Revision: https://phabricator.services.mozilla.com/D26592
services/common/tests/unit/test_async_foreach.js
services/common/tests/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/services/common/tests/unit/test_async_foreach.js
@@ -0,0 +1,75 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+const {Async} = ChromeUtils.import("resource://services-common/async.js");
+const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
+const {sinon} = ChromeUtils.import("resource://testing-common/Sinon.jsm");
+
+function makeArray(length) {
+  // Start at 1 so that we can just divide by yieldEvery to get the expected
+  // call count. (we exp)
+  return Array.from({ length }, (v, i) => i + 1);
+}
+
+// Adjust if we ever change the default.
+const DEFAULT_YIELD_EVERY = 50;
+
+add_task(async function testYields() {
+  let spy = sinon.spy(Async, "promiseYield");
+  try {
+    await Async.yieldingForEach(makeArray(DEFAULT_YIELD_EVERY * 2), element => {
+      // The yield will happen *after* this function is ran.
+      Assert.equal(spy.callCount, Math.floor((element - 1) / DEFAULT_YIELD_EVERY));
+    });
+  } finally {
+    spy.restore();
+  }
+});
+
+add_task(async function testExistingYieldState() {
+  const yieldState = Async.yieldState(DEFAULT_YIELD_EVERY);
+
+  for (let i = 0; i < 15; i++) {
+    Assert.equal(yieldState.shouldYield(), false);
+  }
+
+  let spy = sinon.spy(Async, "promiseYield");
+
+  try {
+    await Async.yieldingForEach(makeArray(DEFAULT_YIELD_EVERY * 2), element => {
+      Assert.equal(spy.callCount, Math.floor((element + 15 - 1) / DEFAULT_YIELD_EVERY));
+    }, yieldState);
+  } finally {
+    spy.restore();
+  }
+});
+
+add_task(async function testEarlyReturn() {
+  let lastElement = 0;
+  await Async.yieldingForEach(makeArray(DEFAULT_YIELD_EVERY), element => {
+    lastElement = element;
+    return element === 10;
+  });
+
+  Assert.equal(lastElement, 10);
+});
+
+add_task(async function testEaryReturnAsync() {
+  let lastElement = 0;
+  await Async.yieldingForEach(makeArray(DEFAULT_YIELD_EVERY), async (element) => {
+    lastElement = element;
+    return element === 10;
+  });
+
+  Assert.equal(lastElement, 10);
+});
+
+add_task(async function testEarlyReturnPromise() {
+  let lastElement = 0;
+  await Async.yieldingForEach(makeArray(DEFAULT_YIELD_EVERY), element => {
+    lastElement = element;
+    return new Promise(resolve => resolve(element === 10));
+  });
+
+  Assert.equal(lastElement, 10);
+});
--- a/services/common/tests/unit/xpcshell.ini
+++ b/services/common/tests/unit/xpcshell.ini
@@ -37,16 +37,17 @@ tags = remote-settings blocklist
 [test_utils_json.js]
 [test_utils_makeURI.js]
 [test_utils_namedTimer.js]
 [test_utils_sets.js]
 [test_utils_utf8.js]
 [test_utils_uuid.js]
 
 [test_async_chain.js]
+[test_async_foreach.js]
 [test_async_iterator.js]
 
 [test_hawkclient.js]
 skip-if = os == "android"
 [test_hawkrequest.js]
 skip-if = os == "android"
 
 [test_logmanager.js]