Skip to content

Commit

Permalink
Refactor how next/previous programs are selected. Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfernandez committed Feb 21, 2024
1 parent 5359131 commit 1778f9d
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 9 deletions.
145 changes: 139 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl App {
}

pub fn selected_program(&self) -> Option<BpfProgram> {
let items = self.items.lock().unwrap().clone();
let items = self.items.lock().unwrap();
let state = self.state.lock().unwrap();

match state.selected() {
Expand All @@ -142,8 +142,8 @@ impl App {
}
}

pub fn next(&mut self) {
let items = self.items.lock().unwrap().clone();
pub fn next_program(&mut self) {
let items = self.items.lock().unwrap();
let mut state = self.state.lock().unwrap();

let i = match state.selected() {
Expand All @@ -159,8 +159,8 @@ impl App {
state.select(Some(i));
}

pub fn previous(&mut self) {
let items = self.items.lock().unwrap().clone();
pub fn previous_program(&mut self) {
let items = self.items.lock().unwrap();
let mut state = self.state.lock().unwrap();

let i = match state.selected() {
Expand All @@ -171,8 +171,141 @@ impl App {
i - 1
}
}
None => 0,
None => items.len() - 1,
};
state.select(Some(i));
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_next_program() {
let mut app = App::new();
let prog_1 = BpfProgram{
id: "1".to_string(),
bpf_type: "test".to_string(),
name: "test".to_string(),
prev_runtime_ns: 100,
run_time_ns: 200,
prev_run_cnt: 1,
run_cnt: 2,
instant: Instant::now(),
period_ns: 0,
};

let prog_2 = BpfProgram{
id: "2".to_string(),
bpf_type: "test".to_string(),
name: "test".to_string(),
prev_runtime_ns: 100,
run_time_ns: 200,
prev_run_cnt: 1,
run_cnt: 2,
instant: Instant::now(),
period_ns: 0,
};

// Add some dummy BpfPrograms to the items vector
app.items.lock().unwrap().push(prog_1.clone());
app.items.lock().unwrap().push(prog_2.clone());

// Initially no item is selected
assert_eq!(app.selected_program(), None);

// After calling next, the first item should be selected
app.next_program();
assert_eq!(app.selected_program(), Some(prog_1.clone()));

// After calling next again, the second item should be selected
app.next_program();
assert_eq!(app.selected_program(), Some(prog_2.clone()));

// After calling next again, we should wrap around to the first item
app.next_program();
assert_eq!(app.selected_program(), Some(prog_1.clone()));
}

#[test]
fn test_previous_program() {
let mut app = App::new();
let prog_1 = BpfProgram{
id: "1".to_string(),
bpf_type: "test".to_string(),
name: "test".to_string(),
prev_runtime_ns: 100,
run_time_ns: 200,
prev_run_cnt: 1,
run_cnt: 2,
instant: Instant::now(),
period_ns: 0,
};

let prog_2 = BpfProgram{
id: "2".to_string(),
bpf_type: "test".to_string(),
name: "test".to_string(),
prev_runtime_ns: 100,
run_time_ns: 200,
prev_run_cnt: 1,
run_cnt: 2,
instant: Instant::now(),
period_ns: 0,
};

// Add some dummy BpfPrograms to the items vector
app.items.lock().unwrap().push(prog_1.clone());
app.items.lock().unwrap().push(prog_2.clone());

// Initially no item is selected
assert_eq!(app.selected_program(), None);

// After calling previous, the last item should be selected
app.previous_program();
assert_eq!(app.selected_program(), Some(prog_2.clone()));

// After calling previous again, the first item should be selected
app.previous_program();
assert_eq!(app.selected_program(), Some(prog_1.clone()));

// After calling previous again, we should wrap around to the last item
app.previous_program();
assert_eq!(app.selected_program(), Some(prog_2.clone()));
}

#[test]
fn test_toggle_graphs() {
let mut app = App::new();

// Initially, show_graphs is false
assert_eq!(app.show_graphs, false);

// After calling toggle_graphs, show_graphs should be true
app.toggle_graphs();
assert_eq!(app.show_graphs, true);

// Set max_cpu, max_eps, and max_runtime to non-zero values
app.max_cpu = 10.0;
app.max_eps = 5;
app.max_runtime = 100;
app.data_buf.lock().unwrap().push_back(PeriodMeasure {
cpu_time_percent: 10.0,
events_per_sec: 5,
average_runtime_ns: 100,
});

// After calling toggle_graphs, show_graphs should be false again
app.toggle_graphs();
assert_eq!(app.show_graphs, false);

// max_cpu, max_eps, and max_runtime should be reset to 0
assert_eq!(app.max_cpu, 0.0);
assert_eq!(app.max_eps, 0);
assert_eq!(app.max_runtime, 0);

// and data_buf should be empty again
assert!(app.data_buf.lock().unwrap().is_empty());
}
}
38 changes: 37 additions & 1 deletion src/bpf_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
use std::time::Instant;

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct BpfProgram {
pub id: String,
pub bpf_type: String,
Expand All @@ -30,6 +30,12 @@ pub struct BpfProgram {
pub period_ns: u128,
}

impl PartialEq for BpfProgram {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

impl BpfProgram {
pub fn period_average_runtime_ns(&self) -> u64 {
if self.run_cnt_delta() == 0 {
Expand Down Expand Up @@ -76,6 +82,36 @@ impl BpfProgram {
mod tests {
use super::*;

#[test]
fn test_partial_eq() {
let prog_1 = BpfProgram {
id: "1".to_string(),
bpf_type: "test".to_string(),
name: "test".to_string(),
prev_runtime_ns: 100,
run_time_ns: 200,
prev_run_cnt: 1,
run_cnt: 2,
instant: Instant::now(),
period_ns: 0,
};

let prog_2 = BpfProgram {
id: "2".to_string(),
bpf_type: "test".to_string(),
name: "test".to_string(),
prev_runtime_ns: 100,
run_time_ns: 200,
prev_run_cnt: 1,
run_cnt: 2,
instant: Instant::now(),
period_ns: 0,
};

assert_eq!(prog_1, prog_1);
assert_ne!(prog_1, prog_2);
}

#[test]
fn test_period_average_runtime_ns() {
let prog = BpfProgram {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ fn run_draw_loop<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> Result
match key.code {
KeyCode::Down => {
if !app.show_graphs {
app.next()
app.next_program()
}
}
KeyCode::Up => {
if !app.show_graphs {
app.previous()
app.previous_program()
}
}
KeyCode::Enter => app.toggle_graphs(),
Expand Down

0 comments on commit 1778f9d

Please sign in to comment.