1 #[cfg(not(any(target_os = "redox", target_os = "fuchsia", target_os = "illumos")))]
2 use nix::sys::resource::{getrlimit, setrlimit, Resource};
3 
4 /// Tests the RLIMIT_NOFILE functionality of getrlimit(), where the resource RLIMIT_NOFILE refers
5 /// to the maximum file descriptor number that can be opened by the process (aka the maximum number
6 /// of file descriptors that the process can open, since Linux 4.5).
7 ///
8 /// We first fetch the existing file descriptor maximum values using getrlimit(), then edit the
9 /// soft limit to make sure it has a new and distinct value to the hard limit. We then setrlimit()
10 /// to put the new soft limit in effect, and then getrlimit() once more to ensure the limits have
11 /// been updated.
12 #[test]
13 #[cfg(not(any(target_os = "redox", target_os = "fuchsia", target_os = "illumos")))]
test_resource_limits_nofile()14 pub fn test_resource_limits_nofile() {
15     let (soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap();
16 
17     let soft_limit = Some(soft_limit.map_or(1024, |v| v - 1));
18     assert_ne!(soft_limit, hard_limit);
19     setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap();
20 
21     let (new_soft_limit, _) = getrlimit(Resource::RLIMIT_NOFILE).unwrap();
22     assert_eq!(new_soft_limit, soft_limit);
23 }
24