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/. */
9 var {PromiseTestUtils} = ChromeUtils.importESModule("resource://testing-common/PromiseTestUtils.sys.mjs");
13 // Whitelisting these tests.
14 // As part of bug 1077403, the shutdown crash should be fixed.
16 // These tests may crash intermittently on shutdown if the DOM Promise uncaught
17 // rejection observers are still registered when the watchdog operates.
18 PromiseTestUtils.thisTestLeaksUncaughtRejectionsAndShouldBeFixed();
20 var gPrefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
22 function setWatchdogEnabled(enabled) {
23 gPrefs.setBoolPref("dom.use_watchdog", enabled);
26 function isWatchdogEnabled() {
27 return gPrefs.getBoolPref("dom.use_watchdog");
30 function setScriptTimeout(seconds) {
31 var oldTimeout = gPrefs.getIntPref("dom.max_script_run_time");
32 gPrefs.setIntPref("dom.max_script_run_time", seconds);
40 function busyWait(ms) {
41 var start = new Date();
42 while ((new Date()) - start < ms) {}
45 function do_log_info(aMessage)
47 print("TEST-INFO | " + _TEST_FILE + " | " + aMessage);
50 // We don't use do_execute_soon, because that inserts a
51 // do_test_{pending,finished} pair that gets screwed up when we terminate scripts
52 // from the operation callback.
53 function executeSoon(fn) {
54 var tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
55 tm.dispatchToMainThread({run: fn});
59 // Asynchronous watchdog diagnostics.
61 // When running, the watchdog wakes up every second, and fires the operation
62 // callback if the script has been running for >= the minimum script timeout.
63 // As such, if the script timeout is 1 second, a script should never be able to
64 // run for two seconds or longer without servicing the operation callback.
65 // We wait 3 seconds, just to be safe.
68 function checkWatchdog(expectInterrupt) {
69 var oldTimeout = setScriptTimeout(1);
70 var lastWatchdogWakeup = Cu.getWatchdogTimestamp("WatchdogWakeup");
72 return new Promise(resolve => {
73 let inBusyWait = false;
74 setInterruptCallback(function() {
75 // If the watchdog didn't actually trigger the operation callback, ignore
76 // this call. This allows us to test the actual watchdog behavior without
77 // interference from other sites where we trigger the operation callback.
78 if (lastWatchdogWakeup == Cu.getWatchdogTimestamp("WatchdogWakeup")) {
82 Assert.ok(true, "Not in busy wait, ignoring interrupt callback");
86 Assert.ok(expectInterrupt, "Interrupt callback fired");
87 setInterruptCallback(undefined);
88 setScriptTimeout(oldTimeout);
89 // Schedule the promise for resolution before we kill this script.
94 executeSoon(function() {
98 Assert.ok(!expectInterrupt, "Interrupt callback didn't fire");
99 setInterruptCallback(undefined);
100 setScriptTimeout(oldTimeout);
106 function run_test() {
111 // Run the async function.
112 testBody().then(() => {