Bug 1021774 - Add test for background hang monitor / thread hang stats; r=vladan
authorJim Chen <nchen@mozilla.com>
Fri, 20 Jun 2014 14:28:09 -0400
changeset 211799 bb4422ab5f084d2c6d7f2a18d033446819698dd4
parent 211798 f44458802800d095cf7a84c91136a1f3fb39ac72
child 211800 c03a1baf9f47d480d42e3065736247a8bd20ec5a
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvladan
bugs1021774
milestone33.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1021774 - Add test for background hang monitor / thread hang stats; r=vladan
toolkit/components/telemetry/tests/unit/test_ThreadHangStats.js
toolkit/components/telemetry/tests/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/toolkit/components/telemetry/tests/unit/test_ThreadHangStats.js
@@ -0,0 +1,77 @@
+const Ci = Components.interfaces;
+const Cc = Components.classes;
+const Cu = Components.utils;
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+function getMainThreadHangStats() {
+  let threads = Services.telemetry.threadHangStats;
+  return threads.find((thread) => (thread.name === "Gecko"));
+}
+
+function run_test() {
+  let startHangs = getMainThreadHangStats();
+
+  // We disable hang reporting in several situations (e.g. debug builds,
+  // official releases). In those cases, we don't have hang stats available
+  // and should exit the test early.
+  if (!startHangs) {
+    ok("Hang reporting not enabled.");
+    return;
+  }
+
+  // Run two events in the event loop:
+  // the first event causes a hang;
+  // the second event checks results from the first event.
+
+  do_execute_soon(() => {
+    // Cause a hang lasting 1 second.
+    let startTime = Date.now();
+    while ((Date.now() - startTime) < 1000) {
+    }
+  });
+
+  do_execute_soon(() => {
+    do_test_pending();
+
+    let check_results = () => {
+      let endHangs = getMainThreadHangStats();
+
+      // Because hangs are recorded asynchronously, if we don't see new hangs,
+      // we should wait for pending hangs to be recorded. On the other hand,
+      // if hang monitoring is broken, this test will time out.
+      if (endHangs.hangs.length === startHangs.hangs.length) {
+        do_timeout(100, check_results);
+        return;
+      }
+
+      let check_histogram = (histogram) => {
+        equal(typeof histogram, "object");
+        equal(histogram.histogram_type, 0);
+        equal(typeof histogram.min, "number");
+        equal(typeof histogram.max, "number");
+        equal(typeof histogram.sum, "number");
+        ok(Array.isArray(histogram.ranges));
+        ok(Array.isArray(histogram.counts));
+        equal(histogram.counts.length, histogram.ranges.length);
+      };
+
+      // Make sure the hang stats structure is what we expect.
+      equal(typeof endHangs, "object");
+      check_histogram(endHangs.activity);
+
+      ok(Array.isArray(endHangs.hangs));
+      notEqual(endHangs.hangs.length, 0);
+
+      ok(Array.isArray(endHangs.hangs[0].stack));
+      notEqual(endHangs.hangs[0].stack.length, 0);
+      equal(typeof endHangs.hangs[0].stack[0], "string");
+
+      check_histogram(endHangs.hangs[0].histogram);
+
+      do_test_finished();
+    };
+
+    check_results();
+  });
+}
--- a/toolkit/components/telemetry/tests/unit/xpcshell.ini
+++ b/toolkit/components/telemetry/tests/unit/xpcshell.ini
@@ -10,8 +10,9 @@ tail =
 # Bug 676989: test fails consistently on Android
 # fail-if = os == "android"
 [test_TelemetryPing_idle.js]
 [test_TelemetryStopwatch.js]
 [test_TelemetryPingBuildID.js]
 [test_ThirdPartyCookieProbe.js]
 [test_TelemetrySendOldPings.js]
 skip-if = debug == true || os == "android" # Disabled due to intermittent orange on Android
+[test_ThreadHangStats.js]