Quick error change for processes to be a bit more graceful, fix tests

This commit is contained in:
Clement Tsang 2019-12-30 22:39:49 -05:00
parent feeca2b258
commit d0a7a0dd72
4 changed files with 31 additions and 9 deletions

View File

@ -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

View File

@ -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();

View File

@ -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;

View File

@ -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()