js/xpconnect/tests/unit/head_watchdog.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/unit/head_watchdog.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     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 +//
     1.9 +// Pref management.
    1.10 +//
    1.11 +
    1.12 +const Cc = Components.classes;
    1.13 +const Ci = Components.interfaces;
    1.14 +const Cu = Components.utils;
    1.15 +
    1.16 +var gPrefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
    1.17 +
    1.18 +function setWatchdogEnabled(enabled) {
    1.19 +  gPrefs.setBoolPref("dom.use_watchdog", enabled);
    1.20 +}
    1.21 +
    1.22 +function isWatchdogEnabled() {
    1.23 +  return gPrefs.getBoolPref("dom.use_watchdog");
    1.24 +}
    1.25 +
    1.26 +//
    1.27 +// Utilities.
    1.28 +//
    1.29 +
    1.30 +function busyWait(ms) {
    1.31 +  var start = new Date();
    1.32 +  while ((new Date()) - start < ms) {}
    1.33 +}
    1.34 +
    1.35 +function do_log_info(aMessage)
    1.36 +{
    1.37 +  print("TEST-INFO | " + _TEST_FILE + " | " + aMessage);
    1.38 +}
    1.39 +
    1.40 +// We don't use do_execute_soon, because that inserts a
    1.41 +// do_test_{pending,finished} pair that gets screwed up when we terminate scripts
    1.42 +// from the operation callback.
    1.43 +function executeSoon(fn) {
    1.44 +  var tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
    1.45 +  tm.mainThread.dispatch({run: fn}, Ci.nsIThread.DISPATCH_NORMAL);
    1.46 +}
    1.47 +
    1.48 +//
    1.49 +// Asynchronous watchdog diagnostics.
    1.50 +//
    1.51 +// When running, the watchdog wakes up every second, and fires the operation
    1.52 +// callback if the script has been running for >= one second. As such, a script
    1.53 +// should never be able to run for two seconds or longer without servicing the
    1.54 +// operation callback. We wait 3 seconds, just to be safe.
    1.55 +//
    1.56 +
    1.57 +function checkWatchdog(expectInterrupt, continuation) {
    1.58 +  var lastWatchdogWakeup = Cu.getWatchdogTimestamp("WatchdogWakeup");
    1.59 +  setInterruptCallback(function() {
    1.60 +    // If the watchdog didn't actually trigger the operation callback, ignore
    1.61 +    // this call. This allows us to test the actual watchdog behavior without
    1.62 +    // interference from other sites where we trigger the operation callback.
    1.63 +    if (lastWatchdogWakeup == Cu.getWatchdogTimestamp("WatchdogWakeup")) {
    1.64 +      return true;
    1.65 +    }
    1.66 +    do_check_true(expectInterrupt);
    1.67 +    setInterruptCallback(undefined);
    1.68 +    // Schedule our continuation before we kill this script.
    1.69 +    executeSoon(continuation);
    1.70 +    return false;
    1.71 +  });
    1.72 +  executeSoon(function() {
    1.73 +    busyWait(3000);
    1.74 +    do_check_true(!expectInterrupt);
    1.75 +    setInterruptCallback(undefined);
    1.76 +    continuation();
    1.77 +  });
    1.78 +}
    1.79 +
    1.80 +var gGenerator;
    1.81 +function continueTest() {
    1.82 +  gGenerator.next();
    1.83 +}
    1.84 +
    1.85 +function run_test() {
    1.86 +
    1.87 +  // Run async.
    1.88 +  do_test_pending();
    1.89 +
    1.90 +  // Instantiate the generator and kick it off.
    1.91 +  gGenerator = testBody();
    1.92 +  gGenerator.next();
    1.93 +}
    1.94 +

mercurial