swnb_timer/
lib.rs

1#![warn(missing_docs)]
2#![allow(dead_code)]
3
4//! swnb_timer is a timer base on binaryHeap;
5//! support async style or callback style;
6//! see examples;
7
8mod time;
9
10pub use time::Timer;
11
12#[cfg(test)]
13mod tests {
14    use std::sync::{atomic::AtomicUsize, Arc};
15
16    use std::sync::atomic::Ordering::SeqCst;
17    use std::thread::sleep;
18    use std::time::Duration;
19
20    use super::*;
21
22    #[test]
23    fn set_timeout() {
24        let timer = Timer::new();
25        let count = Arc::new(AtomicUsize::new(0));
26        let count_clone = count.clone();
27        let _ = timer.set_timeout(
28            move || {
29                count_clone.fetch_add(1, SeqCst);
30                println!("run callback success");
31            },
32            Duration::from_secs(1),
33        );
34        std::thread::sleep(Duration::from_secs(1) + Duration::from_millis(20));
35        assert_eq!(count.load(SeqCst), 1);
36    }
37
38    #[test]
39    fn set_timeout_multi() {
40        let timer = Timer::new();
41        let count = Arc::new(AtomicUsize::new(0));
42        let count_clone1 = count.clone();
43        let _ = timer.set_timeout(
44            move || {
45                count_clone1.fetch_add(1, SeqCst);
46                println!("run callback success");
47            },
48            Duration::from_secs(1),
49        );
50
51        let count_clone2 = count.clone();
52        let _ = timer.set_timeout(
53            move || {
54                count_clone2.fetch_add(1, SeqCst);
55                println!("run callback success");
56            },
57            Duration::from_secs(1),
58        );
59
60        std::thread::sleep(Duration::from_secs(1) + Duration::from_millis(20));
61        assert_eq!(count.load(SeqCst), 2);
62    }
63
64    #[test]
65    fn cancel_timeout() {
66        let timer = Timer::new();
67        let count = Arc::new(AtomicUsize::new(0));
68        let count_clone = count.clone();
69        let cancel_timeout = timer.set_timeout(
70            move || {
71                count_clone.fetch_add(1, SeqCst);
72                println!("run callback success");
73            },
74            Duration::from_secs(1),
75        );
76        std::thread::sleep(Duration::from_millis(20));
77        cancel_timeout();
78        std::thread::sleep(Duration::from_secs(1));
79        assert_eq!(count.load(SeqCst), 0);
80    }
81
82    #[test]
83    fn test_interval() {
84        let timer = Timer::new();
85
86        let count: Arc<AtomicUsize> = Default::default();
87
88        let stop = timer.set_interval(
89            {
90                let count = count.clone();
91                move || {
92                    count.fetch_add(1, SeqCst);
93                    println!("increase");
94                }
95            },
96            Duration::from_secs(3),
97        );
98
99        sleep(Duration::from_secs(7));
100        stop();
101        sleep(Duration::from_secs(3));
102        assert_eq!(count.load(SeqCst), 2);
103    }
104
105    #[test]
106    fn test_time() {
107        let now = std::time::Instant::now();
108        let now_after = now + Duration::from_secs(10);
109        let now = std::time::Instant::now();
110        dbg!(now_after > now);
111    }
112}