summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcyqsimon <28627918+cyqsimon@users.noreply.github.com>2023-10-17 16:29:47 +0800
committerGitHub <noreply@github.com>2023-10-17 16:29:47 +0800
commitd9cc84b3732fe9cb0c819b93d67d2a6420f6a065 (patch)
tree4bcf315523d7c27d5e258f0b6beae34a3eaffbfa
parent5d2ee96900f18a106d532f8b6ccf56ddf62951d7 (diff)
Partially fix flaky tests (#308)
* Minor code style changes * Disable rendering of timestamps in tests * Update test snapshots * Test everything with insta macros (no more `assert(_eq)?`) - This has the benefit of creating snapshots for everything, allowing for later diffing * Don't use `assert_debug_snapshot` for large string outputs - This makes snapshots more human-inspectable * Code style improvement on two tests - `pause_by_space` - `rearranged_by_tab` * Minor code style improvements
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--src/display/components/header_details.rs68
-rw-r--r--src/display/ui.rs27
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__basic_only_addresses.snap2
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__basic_only_connections.snap2
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__basic_only_processes.snap2
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__basic_processes_with_dns_queries.snap2
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__basic_startup.snap2
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__bi_directional_traffic-2.snap64
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__bi_directional_traffic-3.snap15
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__bi_directional_traffic.snap54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-full-width-under-30-height-draw_events-1.snap34
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-full-width-under-30-height-draw_events.snap (renamed from src/tests/cases/snapshots/bandwhich__tests__cases__ui__rearranged_by_tab-4.snap)64
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-120-width-full-height-draw_events-1.snap55
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-120-width-full-height-draw_events.snap (renamed from src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-120-width-full-height-draw_events-0.snap)54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-120-width-under-30-height-draw_events-1.snap34
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-120-width-under-30-height-draw_events.snap (renamed from src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-120-width-under-30-height-draw_events-0.snap)33
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-50-width-under-50-height-draw_events-1.snap55
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-50-width-under-50-height-draw_events.snap (renamed from src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-50-width-under-50-height-draw_events-0.snap)54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-70-width-under-30-height-draw_events-0.snap34
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-70-width-under-30-height-draw_events-1.snap34
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-under-70-width-under-30-height-draw_events.snap (renamed from src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout-full-width-under-30-height-draw_events-0.snap)91
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_connections_from_remote_address-2.snap64
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_connections_from_remote_address-3.snap15
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_connections_from_remote_address.snap54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_packets_of_traffic_from_different_connections-2.snap64
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_packets_of_traffic_from_different_connections-3.snap15
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_packets_of_traffic_from_different_connections.snap54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_packets_of_traffic_from_single_connection-2.snap64
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_packets_of_traffic_from_single_connection-3.snap15
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_packets_of_traffic_from_single_connection.snap54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_processes_with_multiple_connections-2.snap64
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_processes_with_multiple_connections-3.snap15
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__multiple_processes_with_multiple_connections.snap54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__no_resolve_mode-2.snap67
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__no_resolve_mode-3.snap18
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__no_resolve_mode.snap106
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__one_packet_of_traffic-2.snap64
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__one_packet_of_traffic-3.snap15
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__one_packet_of_traffic.snap54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__one_process_with_multiple_connections-2.snap64
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__one_process_with_multiple_connections-3.snap15
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__one_process_with_multiple_connections.snap54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__pause_by_space-2.snap67
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__pause_by_space-3.snap55
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__pause_by_space-4.snap18
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__pause_by_space.snap106
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__rearranged_by_tab-2.snap73
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__rearranged_by_tab-3.snap55
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__rearranged_by_tab-5.snap55
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__rearranged_by_tab-6.snap24
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__rearranged_by_tab.snap210
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes-2.snap67
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes-3.snap18
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes.snap106
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes_bi_directional-2.snap67
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes_bi_directional-3.snap18
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes_bi_directional.snap106
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes_bi_directional_total-2.snap67
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes_bi_directional_total-3.snap18
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes_bi_directional_total.snap108
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes_total-2.snap67
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes_total-3.snap18
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_multiple_processes_total.snap108
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_one_process-2.snap67
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_one_process-3.snap18
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_one_process.snap106
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_one_process_total-2.snap67
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_one_process_total-3.snap18
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__sustained_traffic_from_one_process_total.snap108
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__traffic_with_host_names-2.snap67
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__traffic_with_host_names-3.snap18
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__traffic_with_host_names.snap106
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__traffic_with_winch_event-2.snap67
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__traffic_with_winch_event-3.snap55
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__traffic_with_winch_event-4.snap18
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__traffic_with_winch_event.snap106
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__truncate_long_hostnames-2.snap67
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__truncate_long_hostnames-3.snap18
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__truncate_long_hostnames.snap106
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__two_packets_only_addresses-2.snap55
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__two_packets_only_addresses.snap54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__two_packets_only_connections-2.snap55
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__two_packets_only_connections.snap54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__two_packets_only_processes-2.snap55
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__two_packets_only_processes.snap54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__two_windows_split_horizontally.snap54
-rw-r--r--src/tests/cases/snapshots/bandwhich__tests__cases__ui__two_windows_split_vertically.snap2
-rw-r--r--src/tests/cases/test_utils.rs28
-rw-r--r--src/tests/cases/ui.rs333
91 files changed, 2680 insertions, 2278 deletions
diff --git a/Cargo.lock b/Cargo.lock
index f0388a5..eb17e8b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -155,6 +155,7 @@ dependencies = [
"http_req",
"insta",
"ipnetwork",
+ "itertools",
"log",
"netstat2",
"packet-builder",
diff --git a/Cargo.toml b/Cargo.toml
index bf538e4..359e248 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -53,6 +53,7 @@ insta = "1.34.0"
pnet_base = "0.34.0"
packet-builder = { version = "0.7.0", git = "https://github.com/cyqsimon/packet_builder.git", branch = "patch-update" }
rstest = "0.18.2"
+itertools = "0.11.0"
[target.'cfg(target_os="windows")'.build-dependencies]
anyhow = "1.0.75"
diff --git a/src/display/components/header_details.rs b/src/display/components/header_details.rs
index 2c32a18..e997406 100644
--- a/src/display/components/header_details.rs
+++ b/src/display/components/header_details.rs
@@ -8,17 +8,10 @@ use ratatui::{
text::Span,
widgets::Paragraph,
};
+use unicode_width::UnicodeWidthStr;
use crate::display::{DisplayBandwidth, UIState};
-const SECONDS_IN_DAY: u64 = 86400;
-
-pub struct HeaderDetails<'a> {
- pub state: &'a UIState,
- pub elapsed_time: std::time::Duration,
- pub paused: bool,
-}
-
pub fn elapsed_time(last_start_time: Instant, cumulative_time: Duration, paused: bool) -> Duration {
if paused {
cumulative_time
@@ -27,27 +20,46 @@ pub fn elapsed_time(last_start_time: Instant, cumulative_time: Duration, paused:
}
}
+fn format_duration(d: Duration) -> String {
+ let s = d.as_secs();
+ let days = match s / 86400 {
+ 0 => "".to_string(),
+ 1 => "1 day, ".to_string(),
+ n => format!("{n} days, "),
+ };
+ format!(
+ "{days}{:02}:{:02}:{:02}",
+ (s / 3600) % 24,
+ (s / 60) % 60,
+ s % 60,
+ )
+}
+
+pub struct HeaderDetails<'a> {
+ pub state: &'a UIState,
+ pub elapsed_time: Duration,
+ pub paused: bool,
+}
+
impl<'a> HeaderDetails<'a> {
- #[allow(clippy::int_plus_one)]
pub fn render(&self, frame: &mut Frame<impl Backend>, rect: Rect) {
let bandwidth = self.bandwidth_string();
- let mut elapsed_time = None;
- let print_elapsed_time = if self.state.cumulative_mode {
- elapsed_time = Some(self.elapsed_time_string());
- bandwidth.len() + elapsed_time.as_ref().unwrap().len() + 1 <= rect.width as usize
- } else {
- false
- };
-
let color = if self.paused {
Color::Yellow
} else {
Color::Green
};
- if print_elapsed_time {
- self.render_elapsed_time(frame, rect, elapsed_time.as_ref().unwrap(), color);
+ // do not render time in tests, otherwise the output becomes non-deterministic
+ // see: https://github.com/imsnif/bandwhich/issues/303
+ if cfg!(not(test)) && self.state.cumulative_mode {
+ let elapsed_time = format_duration(self.elapsed_time);
+ // only render if there is enough width
+ if bandwidth.width() + 1 + elapsed_time.width() <= rect.width as usize {
+ self.render_elapsed_time(frame, rect, &elapsed_time, color);
+ }
}
+
self.render_bandwidth(frame, rect, &bandwidth, color);
}
@@ -97,22 +109,4 @@ impl<'a> HeaderDetails<'a> {
let paragraph = Paragraph::new(elapsed_time_text).alignment(Alignment::Right);
frame.render_widget(paragraph, rect);
}
-
- fn days_string(&self) -> String {
- match self.elapsed_time.as_secs() / SECONDS_IN_DAY {
- 0 => "".to_string(),
- 1 => "1 day, ".to_string(),
- n => format!("{n} days, "),
- }
- }
-
- fn elapsed_time_string(&self) -> String {
- format!(
- "{}{:02}:{:02}:{:02} ",
- self.days_string(),
- (self.elapsed_time.as_secs() % SECONDS_IN_DAY) / 3600,
- (self.elapsed_time.as_secs() % 3600) / 60,
- self.elapsed_time.as_secs() % 60
- )
- }
}
diff --git a/src/display/ui.rs b/src/display/ui.rs
index 3241b0b..00532e2 100644
--- a/src/display/ui.rs
+++ b/src/display/ui.rs
@@ -123,24 +123,17 @@ where
}
pub fn draw(&mut self, paused: bool, show_dns: bool, elapsed_time: Duration, ui_offset: usize) {
- let state = &self.state;
- let children = self.get_tables_to_display();
+ let layout = Layout {
+ header: HeaderDetails {
+ state: &self.state,
+ elapsed_time,
+ paused,
+ },
+ children: self.get_tables_to_display(),
+ footer: HelpText { paused, show_dns },
+ };
self.terminal
- .draw(|frame| {
- let size = frame.size();
- let total_bandwidth = HeaderDetails {
- state,
- elapsed_time,
- paused,
- };
- let help_text = HelpText { paused, show_dns };
- let layout = Layout {
-