Browse Source

Give some leeway (5ms) when checking if a function took a certain amount of time to run.

Edward Lee 15 years ago
parent
commit
0fe964922b
1 changed files with 16 additions and 15 deletions
  1. 16 15
      test/unit/test_Sync.js

+ 16 - 15
test/unit/test_Sync.js

@@ -7,6 +7,12 @@ function time(func) {
   return new Date() - startTime;
 }
 
+// Make sure the function took a certain amount of time to run
+function checkTime(func, expect) {
+  // Some reason the timer sometimes fire slightly early, so give some room
+  do_check_true(time(func) >= expect - 5);
+}
+
 // Helper function to test sync functionality
 function slowAdd(onComplete, wait, num1, num2) {
   setTimeout(function() onComplete(num1 + num2), wait);
@@ -20,38 +26,34 @@ function slowThisGet(onComplete, wait, prop) {
 
 // Test the built-in Sync.sleep method.
 function test_Sync_sleep() {
-  let duration = time(function() {
+  checkTime(function() {
     Sync.sleep(100);
-  });
-  do_check_true(duration >= 100);
+  }, 100);
 }
 
 // Check that we can create a sync. function
 function test_Sync() {
-  let duration = time(function() {
+  checkTime(function() {
     let sum = Sync(slowAdd)(100, 1, 10);
     do_check_eq(sum, 11);
-  });
-  do_check_true(duration >= 100);
+  }, 100);
 }
 
 // Check that we can create a sync. function that gets "this" set
 function test_Sync_this() {
-  let duration = time(function() {
+  checkTime(function() {
     let val = Sync(slowThisGet, { five: 5 })(100, "five");
     do_check_eq(val, 5);
-  });
-  do_check_true(duration >= 100);
+  }, 100);
 }
 
 // Check that sync. function callbacks can be extracted
 function test_Sync_onComplete() {
   let add = Sync(slowAdd);
-  let duration = time(function() {
+  checkTime(function() {
     let sum = add(add.onComplete, 100, 1000, 234);
     do_check_eq(sum, 1234);
-  });
-  do_check_true(duration >= 100);
+  }, 100);
 }
 
 // Test sync of async function that indirectly takes the callback
@@ -66,11 +68,10 @@ function test_Sync_onComplete_indirect() {
     wait: 100
   };
 
-  let duration = time(function() {
+  checkTime(function() {
     let val = square(thing);
     do_check_eq(val, 9);
-  });
-  do_check_true(duration >= 100);
+  }, 100);
 }
 
 // Make sure the exported Sync object/function has Function properties