diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2019-12-30 22:39:49 -0500 |
---|---|---|
committer | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2019-12-30 22:39:49 -0500 |
commit | d0a7a0dd72b6b23995a725fd460f2d97b6eff11e (patch) | |
tree | 6bbe69adefe92173469fdf783b4db0783ae767d0 | |
parent | feeca2b2584bcca4822334d18d47894f9deffe84 (diff) |
Quick error change for processes to be a bit more graceful, fix tests
-rw-r--r-- | .travis.yml | 2 | ||||
-rw-r--r-- | src/app/data_collection/processes.rs | 16 | ||||
-rw-r--r-- | src/constants.rs | 2 | ||||
-rw-r--r-- | tests/arg_rate_tests.rs | 20 |
4 files changed, 31 insertions, 9 deletions
diff --git a/.travis.yml b/.travis.yml index aadc5853..72075010 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ os: jobs: allow_failures: - rust: nightly - - env: TARGET=x86_64-pc-windows-gnu # Seems to cause problems + - env: TARGET=x86_64-pc-windows-gnu # Seems to cause problems. TODO: Add test for it, but keep allow fail. before_install: - export RUST_BACKTRACE=1 diff --git a/src/app/data_collection/processes.rs b/src/app/data_collection/processes.rs index 8e2a42b5..0eda01f5 100644 --- a/src/app/data_collection/processes.rs +++ b/src/app/data_collection/processes.rs @@ -1,3 +1,4 @@ +use crate::utils::error; use std::cmp::Ordering; use std::{collections::HashMap, process::Command, time::Instant}; use sysinfo::{ProcessExt, System, SystemExt}; @@ -26,7 +27,7 @@ pub struct ProcessData { pub command: String, } -fn cpu_usage_calculation(prev_idle: &mut f64, prev_non_idle: &mut f64) -> std::io::Result<(f64, f64)> { +fn cpu_usage_calculation(prev_idle: &mut f64, prev_non_idle: &mut f64) -> error::Result<(f64, f64)> { // From SO answer: https://stackoverflow.com/a/23376195 let mut path = std::path::PathBuf::new(); path.push("/proc"); @@ -41,7 +42,12 @@ fn cpu_usage_calculation(prev_idle: &mut f64, prev_non_idle: &mut f64) -> std::i // SC in case that the parsing will fail due to length: if val.len() <= 10 { - return Ok((1.0, 0.0)); // TODO: This is not the greatest... + return Err(error::BottomError::InvalidIO { + message: format!( + "CPU parsing will fail due to too short of a return value; saw {} values, expected 10 values.", + val.len() + ), + }); } let user: f64 = val[1].parse::<_>().unwrap_or(0_f64); @@ -180,7 +186,8 @@ pub fn get_sorted_processes_list( let ps_stdout = String::from_utf8_lossy(&ps_result.stdout); let split_string = ps_stdout.split('\n'); //debug!("{:?}", split_string); - if let Ok((cpu_usage, cpu_percentage)) = cpu_usage_calculation(prev_idle, prev_non_idle) { + let cpu_calc = cpu_usage_calculation(prev_idle, prev_non_idle); + if let Ok((cpu_usage, cpu_percentage)) = cpu_calc { let process_stream = split_string.collect::<Vec<&str>>(); for process in process_stream { @@ -190,6 +197,9 @@ pub fn get_sorted_processes_list( } } } + } else { + error!("Unable to properly parse CPU data in Linux."); + error!("Result: {:?}", cpu_calc.err()); } } else { let process_hashmap = sys.get_process_list(); diff --git a/src/constants.rs b/src/constants.rs index e827b8fd..1dad85a9 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,6 +1,6 @@ // TODO: Store like three minutes of data, then change how much is shown based on scaling! pub const STALE_MAX_MILLISECONDS: u64 = 180 * 1000; // We wish to store at most 60 seconds worth of data. This may change in the future, or be configurable. -pub const TIME_STARTS_FROM: u64 = 60 * 1000; // TODO: Fix this +pub const TIME_STARTS_FROM: u64 = 60 * 1000; pub const TICK_RATE_IN_MILLISECONDS: u64 = 200; // We use this as it's a good value to work with. pub const DEFAULT_REFRESH_RATE_IN_MILLISECONDS: u128 = 1000; pub const MAX_KEY_TIMEOUT_IN_MILLISECONDS: u128 = 1000; diff --git a/tests/arg_rate_tests.rs b/tests/arg_rate_tests.rs index 6f71325a..a32e1b76 100644 --- a/tests/arg_rate_tests.rs +++ b/tests/arg_rate_tests.rs @@ -6,9 +6,21 @@ use std::process::Command; //======================RATES======================// +fn get_os_binary_loc() -> String { + if cfg!(target_os = "linux") { + "./target/x86_64-unknown-linux-gnu/debug/btm".to_string() + } else if cfg!(target_os = "windows") { + "./target/x86_64-pc-windows-msvc/debug/btm".to_string() + } else if cfg!(target_os = "macos") { + "./target/x86_64-apple-darwin/debug/btm".to_string() + } else { + "".to_string() + } +} + #[test] fn test_small_rate() -> Result<(), Box<dyn std::error::Error>> { - Command::new("./target/debug/btm") + Command::new(get_os_binary_loc()) .arg("-r") .arg("249") .assert() @@ -19,7 +31,7 @@ fn test_small_rate() -> Result<(), Box<dyn std::error::Error>> { #[test] fn test_large_rate() -> Result<(), Box<dyn std::error::Error>> { - Command::new("./target/debug/btm") + Command::new(get_os_binary_loc()) .arg("-r") .arg("18446744073709551616") .assert() @@ -31,7 +43,7 @@ fn test_large_rate() -> Result<(), Box<dyn std::error::Error>> { #[test] fn test_negative_rate() -> Result<(), Box<dyn std::error::Error>> { // This test should auto fail due to how clap works - Command::new("./target/debug/btm") + Command::new(get_os_binary_loc()) .arg("-r") .arg("-1000") .assert() @@ -43,7 +55,7 @@ fn test_negative_rate() -> Result<(), Box<dyn std::error::Error>> { #[test] fn test_invalid_rate() -> Result<(), Box<dyn std::error::Error>> { - Command::new("./target/debug/btm") + Command::new(get_os_binary_loc()) .arg("-r") .arg("100-1000") .assert() |