tor-browser

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

test_connection_idle_maintenance.js (3010B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 /* exported testSteps */
      7 async function testSteps() {
      8  // A constant used to deal with small decrease in usage when transactions are
      9  // transferred from the WAL file back into the original database, also called
     10  // as checkpointing.
     11  const cosmologicalConstant = 20000;
     12 
     13  // The length of time that database connections will be held open after all
     14  // transactions have completed before doing idle maintenance.
     15  const connectionIdleMaintenanceMS = 2 * 1000;
     16 
     17  const name = "test_connection_idle_maintenance";
     18  const abc = "abcdefghijklmnopqrstuvwxyz";
     19 
     20  // IndexedDB on Android does `PRAGMA auto_vacuum = FULL`, so the freelist
     21  // pages are moved to the end of the database file and the database file is
     22  // truncated to remove the freelist pages at every transaction commit.
     23  if (mozinfo.os == "android") {
     24    info("Test disabled on Android for now");
     25    return;
     26  }
     27 
     28  info("Creating database");
     29 
     30  let request = indexedDB.open(name, 1);
     31 
     32  let event = await expectingUpgrade(request);
     33 
     34  let database = event.target.result;
     35 
     36  let objectStore = database.createObjectStore(name);
     37 
     38  // Add lots of data...
     39  for (let i = 0; i < 10000; i++) {
     40    objectStore.add(abc, i);
     41  }
     42 
     43  // And then clear it so that maintenance has some space to reclaim.
     44  objectStore.clear();
     45 
     46  await expectingSuccess(request);
     47 
     48  info("Getting database usage before maintenance");
     49 
     50  let databaseUsageBeforeMaintenance = await new Promise(function (resolve) {
     51    getCurrentUsage(function (request) {
     52      resolve(request.result.databaseUsage);
     53    });
     54  });
     55 
     56  info("Waiting for maintenance to start");
     57 
     58  // This time is a double of connectionIdleMaintenanceMS which should be
     59  // pessimistic enough to work with randomly slowed down threads in the chaos
     60  // mode.
     61  await new Promise(function (resolve) {
     62    do_timeout(2 * connectionIdleMaintenanceMS, resolve);
     63  });
     64 
     65  info("Waiting for maintenance to finish");
     66 
     67  // This time is a double of connectionIdleMaintenanceMS which should be
     68  // pessimistic enough to work with randomly slowed down threads in the chaos
     69  // mode.
     70  await new Promise(function (resolve) {
     71    do_timeout(2 * connectionIdleMaintenanceMS, resolve);
     72  });
     73 
     74  info("Getting database usage after maintenance");
     75 
     76  let databaseUsageAfterMaintenance = await new Promise(function (resolve) {
     77    getCurrentUsage(function (request) {
     78      resolve(request.result.databaseUsage);
     79    });
     80  });
     81 
     82  info(
     83    "Database usage before: " +
     84      databaseUsageBeforeMaintenance +
     85      ". " +
     86      "Database usage after: " +
     87      databaseUsageAfterMaintenance
     88  );
     89 
     90  // Checkpointing done immediately after the maintenance can slightly decrease
     91  // the usage even when the maintenance was not run at all.
     92  ok(
     93    databaseUsageBeforeMaintenance - databaseUsageAfterMaintenance >=
     94      cosmologicalConstant,
     95    "Maintenance significantly decreased database usage"
     96  );
     97 }