Skip to content

Commit d9cc84b

Browse files
authored
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
1 parent 5d2ee96 commit d9cc84b

File tree

91 files changed

+2680
-2278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+2680
-2278
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ insta = "1.34.0"
5353
pnet_base = "0.34.0"
5454
packet-builder = { version = "0.7.0", git = "https://github.com/cyqsimon/packet_builder.git", branch = "patch-update" }
5555
rstest = "0.18.2"
56+
itertools = "0.11.0"
5657

5758
[target.'cfg(target_os="windows")'.build-dependencies]
5859
anyhow = "1.0.75"

src/display/components/header_details.rs

+31-37
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,10 @@ use ratatui::{
88
text::Span,
99
widgets::Paragraph,
1010
};
11+
use unicode_width::UnicodeWidthStr;
1112

1213
use crate::display::{DisplayBandwidth, UIState};
1314

14-
const SECONDS_IN_DAY: u64 = 86400;
15-
16-
pub struct HeaderDetails<'a> {
17-
pub state: &'a UIState,
18-
pub elapsed_time: std::time::Duration,
19-
pub paused: bool,
20-
}
21-
2215
pub fn elapsed_time(last_start_time: Instant, cumulative_time: Duration, paused: bool) -> Duration {
2316
if paused {
2417
cumulative_time
@@ -27,27 +20,46 @@ pub fn elapsed_time(last_start_time: Instant, cumulative_time: Duration, paused:
2720
}
2821
}
2922

23+
fn format_duration(d: Duration) -> String {
24+
let s = d.as_secs();
25+
let days = match s / 86400 {
26+
0 => "".to_string(),
27+
1 => "1 day, ".to_string(),
28+
n => format!("{n} days, "),
29+
};
30+
format!(
31+
"{days}{:02}:{:02}:{:02}",
32+
(s / 3600) % 24,
33+
(s / 60) % 60,
34+
s % 60,
35+
)
36+
}
37+
38+
pub struct HeaderDetails<'a> {
39+
pub state: &'a UIState,
40+
pub elapsed_time: Duration,
41+
pub paused: bool,
42+
}
43+
3044
impl<'a> HeaderDetails<'a> {
31-
#[allow(clippy::int_plus_one)]
3245
pub fn render(&self, frame: &mut Frame<impl Backend>, rect: Rect) {
3346
let bandwidth = self.bandwidth_string();
34-
let mut elapsed_time = None;
35-
let print_elapsed_time = if self.state.cumulative_mode {
36-
elapsed_time = Some(self.elapsed_time_string());
37-
bandwidth.len() + elapsed_time.as_ref().unwrap().len() + 1 <= rect.width as usize
38-
} else {
39-
false
40-
};
41-
4247
let color = if self.paused {
4348
Color::Yellow
4449
} else {
4550
Color::Green
4651
};
4752

48-
if print_elapsed_time {
49-
self.render_elapsed_time(frame, rect, elapsed_time.as_ref().unwrap(), color);
53+
// do not render time in tests, otherwise the output becomes non-deterministic
54+
// see: https://github.com/imsnif/bandwhich/issues/303
55+
if cfg!(not(test)) && self.state.cumulative_mode {
56+
let elapsed_time = format_duration(self.elapsed_time);
57+
// only render if there is enough width
58+
if bandwidth.width() + 1 + elapsed_time.width() <= rect.width as usize {
59+
self.render_elapsed_time(frame, rect, &elapsed_time, color);
60+
}
5061
}
62+
5163
self.render_bandwidth(frame, rect, &bandwidth, color);
5264
}
5365

@@ -97,22 +109,4 @@ impl<'a> HeaderDetails<'a> {
97109
let paragraph = Paragraph::new(elapsed_time_text).alignment(Alignment::Right);
98110
frame.render_widget(paragraph, rect);
99111
}
100-
101-
fn days_string(&self) -> String {
102-
match self.elapsed_time.as_secs() / SECONDS_IN_DAY {
103-
0 => "".to_string(),
104-
1 => "1 day, ".to_string(),
105-
n => format!("{n} days, "),
106-
}
107-
}
108-
109-
fn elapsed_time_string(&self) -> String {
110-
format!(
111-
"{}{:02}:{:02}:{:02} ",
112-
self.days_string(),
113-
(self.elapsed_time.as_secs() % SECONDS_IN_DAY) / 3600,
114-
(self.elapsed_time.as_secs() % 3600) / 60,
115-
self.elapsed_time.as_secs() % 60
116-
)
117-
}
118112
}

src/display/ui.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,17 @@ where
123123
}
124124

125125
pub fn draw(&mut self, paused: bool, show_dns: bool, elapsed_time: Duration, ui_offset: usize) {
126-
let state = &self.state;
127-
let children = self.get_tables_to_display();
126+
let layout = Layout {
127+
header: HeaderDetails {
128+
state: &self.state,
129+
elapsed_time,
130+
paused,
131+
},
132+
children: self.get_tables_to_display(),
133+
footer: HelpText { paused, show_dns },
134+
};
128135
self.terminal
129-
.draw(|frame| {
130-
let size = frame.size();
131-
let total_bandwidth = HeaderDetails {
132-
state,
133-
elapsed_time,
134-
paused,
135-
};
136-
let help_text = HelpText { paused, show_dns };
137-
let layout = Layout {
138-
header: total_bandwidth,
139-
children,
140-
footer: help_text,
141-
};
142-
layout.render(frame, size, ui_offset);
143-
})
136+
.draw(|frame| layout.render(frame, frame.size(), ui_offset))
144137
.unwrap();
145138
}
146139

src/tests/cases/snapshots/bandwhich__tests__cases__ui__basic_only_addresses.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: src/tests/cases/ui.rs
3-
expression: "&terminal_draw_events_mirror[0]"
3+
expression: terminal_draw_events.lock().unwrap().join(SNAPSHOT_SECTION_SEPARATOR)
44
---
55
Total Up / Down: 0Bps / 0Bps
66
Utilization by remote address───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐

src/tests/cases/snapshots/bandwhich__tests__cases__ui__basic_only_connections.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: src/tests/cases/ui.rs
3-
expression: "&terminal_draw_events_mirror[0]"
3+
expression: terminal_draw_events.lock().unwrap().join(SNAPSHOT_SECTION_SEPARATOR)
44
---
55
Total Up / Down: 0Bps / 0Bps
66
Utilization by connection───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐

src/tests/cases/snapshots/bandwhich__tests__cases__ui__basic_only_processes.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: src/tests/cases/ui.rs
3-
expression: "&terminal_draw_events_mirror[0]"
3+
expression: terminal_draw_events.lock().unwrap().join(SNAPSHOT_SECTION_SEPARATOR)
44
---
55
Total Up / Down: 0Bps / 0Bps
66
Utilization by process name─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐

src/tests/cases/snapshots/bandwhich__tests__cases__ui__basic_processes_with_dns_queries.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: src/tests/cases/ui.rs
3-
expression: "&terminal_draw_events_mirror[0]"
3+
expression: terminal_draw_events.lock().unwrap().join(SNAPSHOT_SECTION_SEPARATOR)
44
---
55
Total Up / Down: 0Bps / 0Bps
66
Utilization by process name─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐

src/tests/cases/snapshots/bandwhich__tests__cases__ui__basic_startup.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: src/tests/cases/ui.rs
3-
expression: "&terminal_draw_events_mirror[0]"
3+
expression: terminal_draw_events.lock().unwrap().join(SNAPSHOT_SECTION_SEPARATOR)
44
---
55
Total Up / Down: 0Bps / 0Bps
66
Utilization by process name──────────────────────────────────────────────────────────────────┐┌Utilization by remote address────────────────────────────────────────────────────────────────┐
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,15 @@
11
---
22
source: src/tests/cases/ui.rs
3-
expression: "&terminal_draw_events_mirror[1]"
3+
expression: terminal_events.lock().unwrap().as_slice()
44
---
5-
24Bps / 25Bps
6-
7-
8-
1 1 24Bps / 25Bps 1.1.1.1 1 24Bps / 25Bps
9-
10-
11-
12-
13-
14-
15-
16-
17-
18-
19-
20-
21-
22-
23-
24-
25-
26-
27-
28-
29-
30-
31-
32-
<interface_name>:443 => 1.1.1.1:12345 (tcp) 1 24Bps / 25Bps
33-
34-
35-
36-
37-
38-
39-
40-
41-
42-
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
5+
[
6+
Clear,
7+
HideCursor,
8+
Draw,
9+
HideCursor,
10+
Flush,
11+
Draw,
12+
HideCursor,
13+
Flush,
14+
ShowCursor,
15+
]

src/tests/cases/snapshots/bandwhich__tests__cases__ui__bi_directional_traffic-3.snap

-15
This file was deleted.

0 commit comments

Comments
 (0)