tor-browser

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

estimate-usage-details-indexeddb.https.tentative.any.js (2678B)


      1 // META: title=StorageManager: estimate() usage details for indexeddb
      2 // META: script=helpers.js
      3 // META: script=../IndexedDB/resources/support-promises.js
      4 
      5 promise_test(async t => {
      6  const estimate = await navigator.storage.estimate()
      7  assert_equals(typeof estimate.usageDetails, 'object');
      8 }, 'estimate() resolves to dictionary with usageDetails member');
      9 
     10 promise_test(async t => {
     11  // We use 100KB here because db compaction usually happens every few MB
     12  // 100KB is large enough to avoid a false positive (small amounts of metadata
     13  // getting written for some random reason), and small enough to avoid
     14  // compaction with a reasonably high probability.
     15  const writeSize = 1024 * 100;
     16  const objectStoreName = 'store';
     17  const dbname = self.location.pathname;
     18 
     19  let usageAfterWrite, usageBeforeWrite;
     20  // TODO(crbug.com/906867): Refactor this test to better deal with db/log
     21  //                         compaction flakiness
     22  // The for loop here is to help make this test less flaky.  The reason it is
     23  // flaky is that database and/or log compaction could happen in the middle of
     24  // this loop.  The problem is that this test runs in a large batch of tests,
     25  // and previous tests might have created a lot of garbage which could trigger
     26  // compaction.  Suppose the initial estimate shows 1MB usage before creating
     27  // the db.  Compaction could happen after this step and before we measure
     28  // usage at the end, meaning the 1MB could be wiped to 0, an extra 1024 * 100
     29  // is put in, and the actual increase in usage does not reach our expected
     30  // increase.  Loop 10 times here to be safe (and reduce the number of bot
     31  // builds that fail); all it takes is one iteration without compaction for
     32  // this to pass.
     33  for (let i = 0; i < 10; i++) {
     34    indexedDB.deleteDatabase(dbname);
     35    const db = await createDB(dbname, objectStoreName, t);
     36    let estimate = await navigator.storage.estimate();
     37 
     38    // If usage is 0, usageDetails does not include the usage (undefined)
     39    usageBeforeWrite = estimate.usageDetails.indexedDB || 0;
     40 
     41    const txn = db.transaction(objectStoreName, 'readwrite');
     42    const valueToStore = largeValue(writeSize, Math.random() * 255);
     43    txn.objectStore(objectStoreName).add(valueToStore, 1);
     44 
     45    await transactionPromise(txn);
     46 
     47    estimate = await navigator.storage.estimate();
     48    usageAfterWrite = estimate.usageDetails.indexedDB;
     49    db.close();
     50 
     51    if (usageAfterWrite - usageBeforeWrite >= writeSize) {
     52      break;
     53    }
     54  }
     55 
     56  assert_greater_than_equal(usageAfterWrite - usageBeforeWrite,
     57      writeSize);
     58 }, 'estimate() usage details reflects increase in indexedDB after large ' +
     59   'value is stored');