js/xpconnect/tests/unit/head_watchdog.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:8ba43ff1cbf6
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 //
6 // Pref management.
7 //
8
9 const Cc = Components.classes;
10 const Ci = Components.interfaces;
11 const Cu = Components.utils;
12
13 var gPrefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
14
15 function setWatchdogEnabled(enabled) {
16 gPrefs.setBoolPref("dom.use_watchdog", enabled);
17 }
18
19 function isWatchdogEnabled() {
20 return gPrefs.getBoolPref("dom.use_watchdog");
21 }
22
23 //
24 // Utilities.
25 //
26
27 function busyWait(ms) {
28 var start = new Date();
29 while ((new Date()) - start < ms) {}
30 }
31
32 function do_log_info(aMessage)
33 {
34 print("TEST-INFO | " + _TEST_FILE + " | " + aMessage);
35 }
36
37 // We don't use do_execute_soon, because that inserts a
38 // do_test_{pending,finished} pair that gets screwed up when we terminate scripts
39 // from the operation callback.
40 function executeSoon(fn) {
41 var tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
42 tm.mainThread.dispatch({run: fn}, Ci.nsIThread.DISPATCH_NORMAL);
43 }
44
45 //
46 // Asynchronous watchdog diagnostics.
47 //
48 // When running, the watchdog wakes up every second, and fires the operation
49 // callback if the script has been running for >= one second. As such, a script
50 // should never be able to run for two seconds or longer without servicing the
51 // operation callback. We wait 3 seconds, just to be safe.
52 //
53
54 function checkWatchdog(expectInterrupt, continuation) {
55 var lastWatchdogWakeup = Cu.getWatchdogTimestamp("WatchdogWakeup");
56 setInterruptCallback(function() {
57 // If the watchdog didn't actually trigger the operation callback, ignore
58 // this call. This allows us to test the actual watchdog behavior without
59 // interference from other sites where we trigger the operation callback.
60 if (lastWatchdogWakeup == Cu.getWatchdogTimestamp("WatchdogWakeup")) {
61 return true;
62 }
63 do_check_true(expectInterrupt);
64 setInterruptCallback(undefined);
65 // Schedule our continuation before we kill this script.
66 executeSoon(continuation);
67 return false;
68 });
69 executeSoon(function() {
70 busyWait(3000);
71 do_check_true(!expectInterrupt);
72 setInterruptCallback(undefined);
73 continuation();
74 });
75 }
76
77 var gGenerator;
78 function continueTest() {
79 gGenerator.next();
80 }
81
82 function run_test() {
83
84 // Run async.
85 do_test_pending();
86
87 // Instantiate the generator and kick it off.
88 gGenerator = testBody();
89 gGenerator.next();
90 }
91

mercurial