1 //
2 // Sysinfo
3 //
4 // Copyright (c) 2015 Guillaume Gomez
5 //
6 
7 // Once https://github.com/rust-lang/rfcs/blob/master/text/1422-pub-restricted.md
8 // feature gets stabilized, we can move common parts in here.
9 
10 #[cfg(test)]
11 mod tests {
12     use crate::{utils, ProcessExt, System, SystemExt};
13 
14     #[test]
test_refresh_system()15     fn test_refresh_system() {
16         let mut sys = System::new();
17         sys.refresh_system();
18         // We don't want to test on unsupported systems.
19         if System::IS_SUPPORTED {
20             assert!(sys.total_memory() != 0);
21             assert!(sys.free_memory() != 0);
22         }
23         assert!(sys.total_memory() >= sys.free_memory());
24         assert!(sys.total_swap() >= sys.free_swap());
25     }
26 
27     #[test]
test_refresh_process()28     fn test_refresh_process() {
29         let mut sys = System::new();
30         assert!(sys.processes().is_empty(), "no process should be listed!");
31         sys.refresh_processes();
32         // We don't want to test on unsupported systems.
33 
34         #[cfg(not(feature = "apple-sandbox"))]
35         if System::IS_SUPPORTED {
36             assert!(
37                 sys.refresh_process(utils::get_current_pid().expect("failed to get current pid")),
38                 "process not listed",
39             );
40         }
41     }
42 
43     #[test]
test_get_process()44     fn test_get_process() {
45         let mut sys = System::new();
46         sys.refresh_processes();
47         if let Some(p) = sys.process(utils::get_current_pid().expect("failed to get current pid")) {
48             assert!(p.memory() > 0);
49         } else {
50             #[cfg(not(feature = "apple-sandbox"))]
51             assert!(!System::IS_SUPPORTED);
52         }
53     }
54 
55     #[test]
check_if_send_and_sync()56     fn check_if_send_and_sync() {
57         trait Foo {
58             fn foo(&self) {}
59         }
60         impl<T> Foo for T where T: Send {}
61 
62         trait Bar {
63             fn bar(&self) {}
64         }
65 
66         impl<T> Bar for T where T: Sync {}
67 
68         let mut sys = System::new();
69         sys.refresh_processes();
70         if let Some(p) = sys.process(utils::get_current_pid().expect("failed to get current pid")) {
71             p.foo(); // If this doesn't compile, it'll simply mean that the Process type
72                      // doesn't implement the Send trait.
73             p.bar(); // If this doesn't compile, it'll simply mean that the Process type
74                      // doesn't implement the Sync trait.
75         } else {
76             #[cfg(not(feature = "apple-sandbox"))]
77             assert!(!System::IS_SUPPORTED);
78         }
79     }
80 
81     #[test]
check_hostname_has_no_nuls()82     fn check_hostname_has_no_nuls() {
83         let sys = System::new();
84 
85         if let Some(hostname) = sys.host_name() {
86             assert!(!hostname.contains('\u{0}'))
87         }
88     }
89 
90     #[test]
check_uptime()91     fn check_uptime() {
92         let sys = System::new();
93         let uptime = sys.uptime();
94         if System::IS_SUPPORTED {
95             std::thread::sleep(std::time::Duration::from_millis(1000));
96             let new_uptime = sys.uptime();
97             assert!(uptime < new_uptime);
98         }
99     }
100 }
101