js/xpconnect/tests/unit/test_watchdog_hibernate.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/unit/test_watchdog_hibernate.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,53 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +function testBody() {
     1.9 +
    1.10 +  setWatchdogEnabled(true);
    1.11 +
    1.12 +  // It's unlikely that we've ever hibernated at this point, but the timestamps
    1.13 +  // default to 0, so this should always be true.
    1.14 +  var now = Date.now() * 1000;
    1.15 +  var startHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStart");
    1.16 +  var stopHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStop");
    1.17 +  do_log_info("Pre-hibernation statistics:");
    1.18 +  do_log_info("now: " + now / 1000000);
    1.19 +  do_log_info("startHibernation: " + startHibernation / 1000000);
    1.20 +  do_log_info("stopHibernation: " + stopHibernation / 1000000);
    1.21 +  do_check_true(startHibernation < now);
    1.22 +  do_check_true(stopHibernation < now);
    1.23 +
    1.24 +  // When the watchdog runs, it hibernates if there's been no activity for the
    1.25 +  // last 2 seconds, otherwise it sleeps for 1 second. As such, given perfect
    1.26 +  // scheduling, we should never have more than 3 seconds of inactivity without
    1.27 +  // hibernating. To add some padding for automation, we mandate that hibernation
    1.28 +  // must begin between 2 and 5 seconds from now.
    1.29 +  var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    1.30 +  timer.initWithCallback(continueTest, 10000, Ci.nsITimer.TYPE_ONE_SHOT);
    1.31 +  simulateActivityCallback(false);
    1.32 +  yield;
    1.33 +
    1.34 +  simulateActivityCallback(true);
    1.35 +  busyWait(1000); // Give the watchdog time to wake up on the condvar.
    1.36 +  var stateChange = Cu.getWatchdogTimestamp("RuntimeStateChange");
    1.37 +  startHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStart");
    1.38 +  stopHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStop");
    1.39 +  do_log_info("Post-hibernation statistics:");
    1.40 +  do_log_info("stateChange: " + stateChange / 1000000);
    1.41 +  do_log_info("startHibernation: " + startHibernation / 1000000);
    1.42 +  do_log_info("stopHibernation: " + stopHibernation / 1000000);
    1.43 +  // XPCOM timers, JS times, and PR_Now() are apparently not directly
    1.44 +  // comparable, as evidenced by certain seemingly-impossible timing values
    1.45 +  // that occasionally get logged in windows automation. We're really just
    1.46 +  // making sure this behavior is roughly as expected on the macro scale,
    1.47 +  // so we add a 1 second fuzz factor here.
    1.48 +  const FUZZ_FACTOR = 1 * 1000 * 1000;
    1.49 +  do_check_true(stateChange > now + 10*1000*1000 - FUZZ_FACTOR);
    1.50 +  do_check_true(startHibernation > now + 2*1000*1000 - FUZZ_FACTOR);
    1.51 +  do_check_true(startHibernation < now + 5*1000*1000 + FUZZ_FACTOR);
    1.52 +  do_check_true(stopHibernation > now + 10*1000*1000 - FUZZ_FACTOR);
    1.53 +
    1.54 +  do_test_finished();
    1.55 +  yield;
    1.56 +}

mercurial