]> git.lizzy.rs Git - rust.git/commitdiff
Make a time test less flaky
authorAlex Crichton <alex@alexcrichton.com>
Tue, 12 Nov 2013 19:44:14 +0000 (11:44 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 12 Nov 2013 19:44:14 +0000 (11:44 -0800)
This test was failing periodically on windows and other platforms, and in
debugging the issue locally I've found that the previous test was failing
at the assertion `ns0 <= ns1`. Upon inspecting the values, the two numbers were
very close to one another, but off by a little bit.

I believe that this is because `precise_time_s` goes from `u64` -> `f64` and
then we go again back to `u64` for the assertion. This conversion is a lossy one
that's not always guaranteed to succeed, so instead I've changed the test to
only compare against u64 instances.

src/libextra/time.rs

index aed42e8d7fe965cb59cafb4d43adb78db14e8868..d6e2a4164271b35b7f1d5668d101636622548cdf 100644 (file)
@@ -1005,18 +1005,17 @@ fn test_get_time() {
 
     fn test_precise_time() {
         let s0 = precise_time_s();
-        let ns1 = precise_time_ns();
-
         debug!("s0={} sec", f64::to_str_digits(s0, 9u));
         assert!(s0 > 0.);
-        let ns0 = (s0 * 1000000000.) as u64;
-        debug!("ns0={:?} ns", ns0);
 
-        debug!("ns1={:?} ns", ns0);
+        let ns0 = precise_time_ns();
+        let ns1 = precise_time_ns();
+        debug!("ns0={:?} ns", ns0);
+        debug!("ns1={:?} ns", ns1);
         assert!(ns1 >= ns0);
 
         let ns2 = precise_time_ns();
-        debug!("ns2={:?} ns", ns0);
+        debug!("ns2={:?} ns", ns2);
         assert!(ns2 >= ns1);
     }