michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function testBody() { michael@0: michael@0: setWatchdogEnabled(true); michael@0: michael@0: // It's unlikely that we've ever hibernated at this point, but the timestamps michael@0: // default to 0, so this should always be true. michael@0: var now = Date.now() * 1000; michael@0: var startHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStart"); michael@0: var stopHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStop"); michael@0: do_log_info("Pre-hibernation statistics:"); michael@0: do_log_info("now: " + now / 1000000); michael@0: do_log_info("startHibernation: " + startHibernation / 1000000); michael@0: do_log_info("stopHibernation: " + stopHibernation / 1000000); michael@0: do_check_true(startHibernation < now); michael@0: do_check_true(stopHibernation < now); michael@0: michael@0: // When the watchdog runs, it hibernates if there's been no activity for the michael@0: // last 2 seconds, otherwise it sleeps for 1 second. As such, given perfect michael@0: // scheduling, we should never have more than 3 seconds of inactivity without michael@0: // hibernating. To add some padding for automation, we mandate that hibernation michael@0: // must begin between 2 and 5 seconds from now. michael@0: var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: timer.initWithCallback(continueTest, 10000, Ci.nsITimer.TYPE_ONE_SHOT); michael@0: simulateActivityCallback(false); michael@0: yield; michael@0: michael@0: simulateActivityCallback(true); michael@0: busyWait(1000); // Give the watchdog time to wake up on the condvar. michael@0: var stateChange = Cu.getWatchdogTimestamp("RuntimeStateChange"); michael@0: startHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStart"); michael@0: stopHibernation = Cu.getWatchdogTimestamp("WatchdogHibernateStop"); michael@0: do_log_info("Post-hibernation statistics:"); michael@0: do_log_info("stateChange: " + stateChange / 1000000); michael@0: do_log_info("startHibernation: " + startHibernation / 1000000); michael@0: do_log_info("stopHibernation: " + stopHibernation / 1000000); michael@0: // XPCOM timers, JS times, and PR_Now() are apparently not directly michael@0: // comparable, as evidenced by certain seemingly-impossible timing values michael@0: // that occasionally get logged in windows automation. We're really just michael@0: // making sure this behavior is roughly as expected on the macro scale, michael@0: // so we add a 1 second fuzz factor here. michael@0: const FUZZ_FACTOR = 1 * 1000 * 1000; michael@0: do_check_true(stateChange > now + 10*1000*1000 - FUZZ_FACTOR); michael@0: do_check_true(startHibernation > now + 2*1000*1000 - FUZZ_FACTOR); michael@0: do_check_true(startHibernation < now + 5*1000*1000 + FUZZ_FACTOR); michael@0: do_check_true(stopHibernation > now + 10*1000*1000 - FUZZ_FACTOR); michael@0: michael@0: do_test_finished(); michael@0: yield; michael@0: }