1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#![cfg(not(windows))]
use rstest::rstest;
use std::convert::TryInto;
use thread_priority::*;
#[cfg(target_os = "linux")]
#[test]
fn get_and_set_priority_with_normal_and_crossplatform() {
let nice = unsafe { libc::getpriority(0, 0) };
assert_eq!(nice, 0);
crate::set_current_thread_priority(ThreadPriority::Crossplatform(30u8.try_into().unwrap()))
.unwrap();
let nice = unsafe { libc::getpriority(0, 0) };
assert!(nice > 0);
// Note that increasing priority requires extra permissions (e.g. sudo)
crate::set_current_thread_priority(ThreadPriority::Crossplatform(70u8.try_into().unwrap()))
.unwrap();
let nice = unsafe { libc::getpriority(0, 0) };
assert!(nice < 0);
}
#[cfg(target_os = "linux")]
#[rstest]
fn get_and_set_priority_with_normal_policies(
#[values(
ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Other),
ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Idle),
ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Batch)
)]
policy: ThreadSchedulePolicy,
#[values(ThreadPriority::Min, ThreadPriority::Max, ThreadPriority::Crossplatform(23u8.try_into().unwrap()))]
priority: ThreadPriority,
) {
let ret = set_thread_priority_and_policy(thread_native_id(), priority, policy);
if policy == ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Idle)
&& priority == ThreadPriority::Crossplatform(23u8.try_into().unwrap())
{
assert_eq!(ret, Err(Error::PriorityNotInRange(0..=0)));
} else {
assert!(ret.is_ok());
}
}
// In macOS it is allowed to specify number as a SCHED_OTHER policy priority.
#[cfg(any(
target_os = "macos",
target_os = "openbsd",
target_os = "freebsd",
target_os = "netbsd"
))]
#[rstest]
fn get_and_set_priority_with_normal_policies(
#[values(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Other))]
policy: ThreadSchedulePolicy,
#[values(ThreadPriority::Min, ThreadPriority::Max, ThreadPriority::Crossplatform(23u8.try_into().unwrap()))]
priority: ThreadPriority,
) {
assert!(set_thread_priority_and_policy(thread_native_id(), priority, policy).is_ok());
}
#[rstest]
#[cfg(target_os = "linux")]
#[case(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Idle), 0..=0)]
#[cfg(target_os = "linux")]
#[case(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Batch), -20..=19)]
#[case(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Other), -20..=19)]
#[case(ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Fifo), 0..=99)]
#[case(ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::RoundRobin), 0..=99)]
fn check_min_and_max_priority_values(
#[case] policy: ThreadSchedulePolicy,
#[case] posix_range: std::ops::RangeInclusive<i32>,
) {
let max_value = ThreadPriority::max_value_for_policy(policy).unwrap();
let min_value = ThreadPriority::min_value_for_policy(policy).unwrap();
assert!(posix_range.contains(&max_value));
assert!(posix_range.contains(&min_value));
}
#[rstest]
#[cfg(target_os = "linux")]
#[case(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Idle))]
#[cfg(target_os = "linux")]
#[case(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Batch))]
#[case(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Other))]
fn set_priority_with_normal_policy_but_with_invalid_value(#[case] policy: ThreadSchedulePolicy) {
let thread_id = thread_native_id();
#[cfg(target_os = "linux")]
let expected = if policy == ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Idle) {
// In Linux we should get an error whenever a non-zero value is passed as priority and a normal
// scheduling policy is used.
Err(Error::PriorityNotInRange(0..=0))
} else {
Ok(())
};
assert_eq!(
set_thread_priority_and_policy(
thread_id,
ThreadPriority::Crossplatform(23u8.try_into().unwrap()),
policy,
),
expected
);
}
#[cfg(any(
target_os = "macos",
target_os = "openbsd",
target_os = "freebsd",
target_os = "netbsd"
))]
#[test]
// In macOS the SCHED_OTHER policy allows having a non-zero priority value,
// but the crate doesn't use this opportunity for normal threads and uses niceness instead.
fn get_and_set_priority_with_normal_policy() {
let thread_id = thread_native_id();
let normal_policy = ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Other);
assert_eq!(
set_thread_priority_and_policy(
thread_id,
ThreadPriority::Crossplatform(23u8.try_into().unwrap()),
normal_policy,
),
Ok(())
);
assert_eq!(thread_schedule_policy(), Ok(normal_policy));
}
#[rstest]
#[case::fifo(ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Fifo))]
#[case::roundrobin(ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::RoundRobin))]
fn get_and_set_priority_with_realtime_policy_requires_capabilities(
#[case] realtime_policy: ThreadSchedulePolicy,
) {
let thread_id = thread_native_id();
let max_value = ThreadPriority::max_value_for_policy(realtime_policy).unwrap();
let min_value = ThreadPriority::min_value_for_policy(realtime_policy).unwrap();
assert_eq!(
set_thread_priority_and_policy(thread_id, ThreadPriority::Max, realtime_policy,),
Ok(())
);
assert_eq!(thread_schedule_policy(), Ok(realtime_policy));
assert_eq!(
thread_schedule_policy_param(thread_native_id()),
Ok((
realtime_policy,
ScheduleParams {
sched_priority: max_value
}
))
);
assert_eq!(
Thread::current(),
Ok(Thread {
priority: ThreadPriority::Crossplatform((max_value as u8).try_into().unwrap()),
id: thread_native_id()
})
);
assert_eq!(
set_thread_priority_and_policy(
thread_id,
ThreadPriority::Crossplatform(23u8.try_into().unwrap()),
realtime_policy,
),
Ok(())
);
assert_eq!(thread_schedule_policy(), Ok(realtime_policy));
assert_eq!(
thread_schedule_policy_param(thread_native_id()),
Ok((realtime_policy, ScheduleParams { sched_priority: 23 }))
);
assert_eq!(
Thread::current(),
Ok(Thread {
priority: ThreadPriority::Crossplatform(23u8.try_into().unwrap()),
id: thread_native_id()
})
);
assert_eq!(
set_thread_priority_and_policy(thread_id, ThreadPriority::Min, realtime_policy,),
Ok(())
);
assert_eq!(thread_schedule_policy(), Ok(realtime_policy));
assert_eq!(
thread_schedule_policy_param(thread_native_id()),
Ok((
realtime_policy,
ScheduleParams {
sched_priority: min_value
}
))
);
assert_eq!(
Thread::current(),
Ok(Thread {
priority: ThreadPriority::Crossplatform((min_value as u8).try_into().unwrap()),
id: thread_native_id()
})
);
}
|