tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_async_foreach.js (2267B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { Async } = ChromeUtils.importESModule(
      5  "resource://services-common/async.sys.mjs"
      6 );
      7 const { sinon } = ChromeUtils.importESModule(
      8  "resource://testing-common/Sinon.sys.mjs"
      9 );
     10 
     11 function makeArray(length) {
     12  // Start at 1 so that we can just divide by yieldEvery to get the expected
     13  // call count. (we exp)
     14  return Array.from({ length }, (v, i) => i + 1);
     15 }
     16 
     17 // Adjust if we ever change the default.
     18 const DEFAULT_YIELD_EVERY = 50;
     19 
     20 add_task(async function testYields() {
     21  let spy = sinon.spy(Async, "promiseYield");
     22  try {
     23    await Async.yieldingForEach(makeArray(DEFAULT_YIELD_EVERY * 2), element => {
     24      // The yield will happen *after* this function is ran.
     25      Assert.equal(
     26        spy.callCount,
     27        Math.floor((element - 1) / DEFAULT_YIELD_EVERY)
     28      );
     29    });
     30  } finally {
     31    spy.restore();
     32  }
     33 });
     34 
     35 add_task(async function testExistingYieldState() {
     36  const yieldState = Async.yieldState(DEFAULT_YIELD_EVERY);
     37 
     38  for (let i = 0; i < 15; i++) {
     39    Assert.equal(yieldState.shouldYield(), false);
     40  }
     41 
     42  let spy = sinon.spy(Async, "promiseYield");
     43 
     44  try {
     45    await Async.yieldingForEach(
     46      makeArray(DEFAULT_YIELD_EVERY * 2),
     47      element => {
     48        Assert.equal(
     49          spy.callCount,
     50          Math.floor((element + 15 - 1) / DEFAULT_YIELD_EVERY)
     51        );
     52      },
     53      yieldState
     54    );
     55  } finally {
     56    spy.restore();
     57  }
     58 });
     59 
     60 add_task(async function testEarlyReturn() {
     61  let lastElement = 0;
     62  await Async.yieldingForEach(makeArray(DEFAULT_YIELD_EVERY), element => {
     63    lastElement = element;
     64    return element === 10;
     65  });
     66 
     67  Assert.equal(lastElement, 10);
     68 });
     69 
     70 add_task(async function testEaryReturnAsync() {
     71  let lastElement = 0;
     72  await Async.yieldingForEach(makeArray(DEFAULT_YIELD_EVERY), async element => {
     73    lastElement = element;
     74    return element === 10;
     75  });
     76 
     77  Assert.equal(lastElement, 10);
     78 });
     79 
     80 add_task(async function testEarlyReturnPromise() {
     81  let lastElement = 0;
     82  await Async.yieldingForEach(makeArray(DEFAULT_YIELD_EVERY), element => {
     83    lastElement = element;
     84    return new Promise(resolve => resolve(element === 10));
     85  });
     86 
     87  Assert.equal(lastElement, 10);
     88 });