Bug 740769. (Cv1a) test_bug325418.js: Create separate observers, Initialize gStartTime[12] later, Use Date.now() instead of (new Date()).getTime(), "timer = null;" a bit later, Nits. r=bzbarsky.
authorSerge Gautherie <sgautherie.bz@free.fr>
Tue, 10 Apr 2012 15:59:43 +0200
changeset 94549 2374ebd5026f86aab18d3e4fbfd484e5b3f5cbbb
parent 94548 6fe5b0271cd125c7a7fca5b75bc04943c6b31d08
child 94610 3fa30b0edd155100e511fc2bd05dda82fd4666a2
child 98066 1fa9224072772a3fcd78c0b7040725c64c89a70e
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs740769, 325418
milestone14.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 740769. (Cv1a) test_bug325418.js: Create separate observers, Initialize gStartTime[12] later, Use Date.now() instead of (new Date()).getTime(), "timer = null;" a bit later, Nits. r=bzbarsky.
xpcom/tests/unit/test_bug325418.js
--- a/xpcom/tests/unit/test_bug325418.js
+++ b/xpcom/tests/unit/test_bug325418.js
@@ -1,31 +1,61 @@
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
+// 5 seconds.
+const kExpectedDelay1 = 5;
+// 1 second.
+const kExpectedDelay2 = 1;
+
+var gStartTime1;
+var gStartTime2;
 var timer;
-const start_time = (new Date()).getTime();
-const expected_time = 1;
 
-var observer = {
-  observe: function observeTC(subject, topic, data) {
+var observer1 = {
+  observe: function observeTC1(subject, topic, data) {
     if (topic == "timer-callback") {
+      // Stop timer, so it doesn't repeat (if test runs slowly).
       timer.cancel();
+
+      // Actual delay may not be exact, so convert to seconds and round down.
+      do_check_eq(Math.floor((Date.now() - gStartTime1) / 1000),
+                  kExpectedDelay1);
+
       timer = null;
 
-      // expected time may not be exact so convert to seconds and round down.
-      var result = Math.floor(((new Date()).getTime() - start_time) / 1000);
-      do_check_eq(result, expected_time);
+      do_print("1st timer triggered (before being cancelled). Should not have happened!");
+      do_check_true(false);
+    }
+  }
+};
+
+var observer2 = {
+  observe: function observeTC2(subject, topic, data) {
+    if (topic == "timer-callback") {
+      // Stop timer, so it doesn't repeat (if test runs slowly).
+      timer.cancel();
+
+      // Actual delay may not be exact, so convert to seconds and round down.
+      do_check_eq(Math.floor((Date.now() - gStartTime2) / 1000),
+                  kExpectedDelay2);
+
+      timer = null;
 
       do_test_finished();
     }
   }
 };
 
 function run_test() {
   do_test_pending();
 
   timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
-  // Start a 5 second timer, than cancel it and start a 1 second timer.
-  timer.init(observer, 5000, timer.TYPE_REPEATING_PRECISE);
+
+  // Initialize the timer (with some delay), then cancel it.
+  gStartTime1 = Date.now();
+  timer.init(observer1, kExpectedDelay1 * 1000, timer.TYPE_REPEATING_PRECISE);
   timer.cancel();
-  timer.init(observer, 1000, timer.TYPE_REPEATING_PRECISE);
+
+  // Re-initialize the timer (with a different delay).
+  gStartTime2 = Date.now();
+  timer.init(observer2, kExpectedDelay2 * 1000, timer.TYPE_REPEATING_PRECISE);
 }