1 extern crate wasm_bindgen_test;
2 
3 use instant::Instant;
4 use std::time::Duration;
5 use wasm_bindgen_test::*;
6 
7 wasm_bindgen_test_configure!(run_in_browser);
8 // run these tests using: wasm-pack test --chrome --headless -- --features wasm-bindgen
9 
10 #[wasm_bindgen_test]
test_instant_now()11 fn test_instant_now() {
12     let now = Instant::now();
13     #[cfg(feature = "inaccurate")]
14     while now.elapsed().as_millis() == 0 {}
15     #[cfg(not(feature = "inaccurate"))]
16     assert!(now.elapsed().as_nanos() > 0);
17 }
18 
19 #[wasm_bindgen_test]
test_duration()20 fn test_duration() {
21     let now = Instant::now();
22     let one_sec = Duration::from_secs(1);
23     assert!(now.elapsed() < one_sec);
24 }
25 
26 // Duration::new will overflow when you have u64::MAX seconds and one billion nanoseconds.
27 // <https://doc.rust-lang.org/std/time/struct.Duration.html#method.new>
28 const ONE_BILLION: u32 = 1_000_000_000;
29 
30 #[wasm_bindgen_test]
test_checked_add()31 fn test_checked_add() {
32     let now = Instant::now();
33 
34     assert!(now.checked_add(Duration::from_millis(1)).is_some());
35     assert_eq!(
36         None,
37         now.checked_add(Duration::new(u64::MAX, ONE_BILLION - 1))
38     );
39 }
40 
41 #[wasm_bindgen_test]
test_checked_sub()42 fn test_checked_sub() {
43     let now = Instant::now();
44 
45     assert!(now.checked_sub(Duration::from_millis(1)).is_some());
46     assert!(now
47         .checked_sub(Duration::new(u64::MAX, ONE_BILLION - 1))
48         .is_none());
49 }
50