• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use rustix::process::nice;
2 #[cfg(not(target_os = "redox"))]
3 use rustix::process::{getpriority_process, setpriority_process};
4 
5 #[cfg(not(target_os = "freebsd"))] // FreeBSD's nice(3) doesn't return the old value.
6 #[test]
test_priorities()7 fn test_priorities() {
8     let old = nice(0).unwrap();
9 
10     #[cfg(not(target_os = "redox"))]
11     {
12         let get_prio = getpriority_process(None).unwrap();
13         assert_eq!(get_prio, old);
14     }
15 
16     // Lower the priority by one.
17     let new = nice(1).unwrap();
18 
19     // If the test wasn't running with the lowest priority initially, test that
20     // we were able to lower the priority.
21     if old < 19 {
22         assert_eq!(old + 1, new);
23     }
24 
25     let get = nice(0).unwrap();
26     assert_eq!(new, get);
27 
28     #[cfg(not(target_os = "redox"))]
29     {
30         let get_prio = getpriority_process(None).unwrap();
31         assert_eq!(get_prio, new);
32 
33         setpriority_process(None, get + 1).unwrap();
34         let now = getpriority_process(None).unwrap();
35 
36         // If the test wasn't running with the lowest priority initially, test
37         // that we were able to lower the priority.
38         if get < 19 {
39             assert_eq!(get + 1, now);
40         }
41         setpriority_process(None, get + 10000).unwrap();
42         let now = getpriority_process(None).unwrap();
43         // Linux's max is 19; Darwin's max is 20.
44         assert!(now >= 19 && now <= 20);
45         // Darwin appears to return `EPERM` on an out of range `nice`.
46         if let Ok(again) = nice(1) {
47             assert_eq!(now, again);
48         }
49     }
50 }
51 
52 /// FreeBSD's `nice` doesn't return the new nice value, so use a specialized
53 /// test.
54 #[cfg(target_os = "freebsd")]
55 #[test]
test_priorities()56 fn test_priorities() {
57     let start = getpriority_process(None).unwrap();
58 
59     let _ = nice(0).unwrap();
60 
61     let now = getpriority_process(None).unwrap();
62     assert_eq!(start, now);
63 
64     let _ = nice(1).unwrap();
65 
66     let now = getpriority_process(None).unwrap();
67     assert_eq!(start + 1, now);
68 
69     setpriority_process(None, start + 2).unwrap();
70 
71     let now = getpriority_process(None).unwrap();
72     assert_eq!(start + 2, now);
73 
74     setpriority_process(None, 10000).unwrap();
75 
76     let now = getpriority_process(None).unwrap();
77     assert_eq!(now, 20);
78 
79     let _ = nice(1).unwrap();
80 
81     let now = getpriority_process(None).unwrap();
82     assert_eq!(now, 20);
83 }
84