tor-browser

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

test_watchdog_hibernate.js (2710B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 async function testBody() {
      6 
      7  setWatchdogEnabled(true);
      8 
      9  // It's unlikely that we've ever hibernated at this point, but the timestamps
     10  // default to 0, so this should always be true.
     11  var now = Date.now() * 1000;
     12  var startHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStart");
     13  var stopHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStop");
     14  do_log_info("Pre-hibernation statistics:");
     15  do_log_info("now: " + now / 1000000);
     16  do_log_info("startHibernation: " + startHibernation / 1000000);
     17  do_log_info("stopHibernation: " + stopHibernation / 1000000);
     18  Assert.less(startHibernation, now, "startHibernation ok");
     19  Assert.less(stopHibernation, now, "stopHibernation ok");
     20 
     21  // When the watchdog runs, it hibernates if there's been no activity for the
     22  // last 2 seconds, otherwise it sleeps for 1 second. As such, given perfect
     23  // scheduling, we should never have more than 3 seconds of inactivity without
     24  // hibernating. To add some padding for automation, we mandate that hibernation
     25  // must begin between 2 and 5 seconds from now.
     26 
     27  // Sleep for 10 seconds. Note: we don't use nsITimer here because then we may run
     28  // arbitrary (idle) events involving script before it fires.
     29  simulateNoScriptActivity(10);
     30 
     31  busyWait(1000); // Give the watchdog time to wake up on the condvar.
     32  var stateChange = Cu.getWatchdogTimestamp("ContextStateChange");
     33  startHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStart");
     34  stopHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStop");
     35  do_log_info("Post-hibernation statistics:");
     36  do_log_info("stateChange: " + stateChange / 1000000);
     37  do_log_info("startHibernation: " + startHibernation / 1000000);
     38  do_log_info("stopHibernation: " + stopHibernation / 1000000);
     39  // XPCOM timers, JS times, and PR_Now() are apparently not directly
     40  // comparable, as evidenced by certain seemingly-impossible timing values
     41  // that occasionally get logged in windows automation. We're really just
     42  // making sure this behavior is roughly as expected on the macro scale,
     43  // so we add a 1 second fuzz factor here.
     44  const FUZZ_FACTOR = 1 * 1000 * 1000;
     45  Assert.greater(stateChange, now + 10*1000*1000 - FUZZ_FACTOR, "stateChange ok");
     46  Assert.greater(startHibernation, now + 2*1000*1000 - FUZZ_FACTOR, "startHibernation ok");
     47  Assert.less(startHibernation, now + 5*1000*1000 + FUZZ_FACTOR, "startHibernation ok");
     48  Assert.greater(stopHibernation, now + 10*1000*1000 - FUZZ_FACTOR, "stopHibernation ok");
     49 }