Skip to content

Commit

Permalink
Add test for new method
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceg committed Aug 28, 2023
1 parent 46b9b74 commit 668c7f9
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ mod doctest {

#[cfg(test)]
mod test {
use std::collections::HashMap;

use crate::*;

#[cfg(feature = "unknown-ci")]
Expand Down Expand Up @@ -283,6 +285,42 @@ mod test {
.any(|(_, proc_)| proc_.cpu_usage() > 0.0));
}

#[test]
fn check_processes_total_accumulated_cpu_usage() {
if System::IS_SUPPORTED {
let mut s = System::new();

s.refresh_processes();
let all_procs: HashMap<_, _> = s
.processes()
.iter()
.map(|(pid, proc)| (*pid, proc.total_accumulated_cpu_usage()))
.collect();
// All accumulated CPU usages will be non-negative.
assert!(all_procs.values().all(|&usage| usage >= 0.0));

// Wait a bit to update CPU usage values
std::thread::sleep(System::MINIMUM_CPU_UPDATE_INTERVAL);
s.refresh_processes();
// They will still all be non-negative.
assert!(s
.processes()
.values()
.all(|proc| proc.total_accumulated_cpu_usage() >= 0.0));
// They will all have either remained the same or
// increased no more than a valid amount.
let max_delta =
s.cpus().len() as f32 * System::MINIMUM_CPU_UPDATE_INTERVAL.as_secs_f64() as f32;
assert!(s
.processes()
.iter()
.all(|(pid, proc)| all_procs.get(pid).map_or(true, |&prev| {
let delta = proc.total_accumulated_cpu_usage() - prev;
delta >= 0.0 && delta <= max_delta
})));
}
}

#[test]
fn check_cpu_usage() {
if !System::IS_SUPPORTED {
Expand Down

0 comments on commit 668c7f9

Please sign in to comment.