tor-browser

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

test_onGarbageCollection-01.js (1681B)


      1 // Test basic usage of onGarbageCollection
      2 
      3 const root = newGlobal();
      4 const dbg = new Debugger();
      5 const wrappedRoot = dbg.addDebuggee(root)
      6 
      7 const NUM_SLICES = root.NUM_SLICES = 10;
      8 
      9 let fired = false;
     10 let slicesFound = 0;
     11 
     12 Services.prefs.setBoolPref("security.allow_eval_with_system_principal", true);
     13 registerCleanupFunction(() => {
     14  Services.prefs.clearUserPref("security.allow_eval_with_system_principal");
     15 });
     16 
     17 dbg.memory.onGarbageCollection = data => {
     18  fired = true;
     19 
     20  print("Got onGarbageCollection: " + JSON.stringify(data, null, 2));
     21 
     22  equal(typeof data.reason, "string");
     23  equal(typeof data.nonincrementalReason == "string" || data.nonincrementalReason === null,
     24        true);
     25 
     26  let lastStartTimestamp = 0;
     27  for (let i = 0; i < data.collections.length; i++) {
     28    let slice = data.collections[i];
     29 
     30    equal(slice.startTimestamp >= lastStartTimestamp, true);
     31    equal(slice.startTimestamp <= slice.endTimestamp, true);
     32 
     33    lastStartTimestamp = slice.startTimestamp;
     34  }
     35 
     36  equal(data.collections.length >= 1, true);
     37  slicesFound += data.collections.length;
     38 }
     39 
     40 function run_test() {
     41  do_test_pending();
     42 
     43  root.eval(
     44    `
     45    this.allocs = [];
     46 
     47    // GC slices
     48    for (var i = 0; i < NUM_SLICES; i++) {
     49      this.allocs.push({});
     50      gcslice();
     51    }
     52 
     53    // Full GC
     54    this.allocs.push({});
     55    gc();
     56    `
     57  );
     58 
     59  executeSoon(() => {
     60    equal(fired, true, "The GC hook should have fired at least once");
     61 
     62    // NUM_SLICES + 1 full gc + however many were triggered naturally (due to
     63    // whatever zealousness setting).
     64    print("Found " + slicesFound + " slices");
     65    equal(slicesFound >= NUM_SLICES + 1, true);
     66 
     67    do_test_finished();
     68  });
     69 }