Skip to content

Commit 049f5b6

Browse files
committed
Stabilize test removing most of the time dependency
1 parent 57e583e commit 049f5b6

File tree

1 file changed

+80
-30
lines changed

1 file changed

+80
-30
lines changed

mithril-client-cli/src/utils/progress_reporter.rs

Lines changed: 80 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,31 @@ pub struct ProgressBarJsonFormatter;
7878
impl ProgressBarJsonFormatter {
7979
/// Get a json formatted string given the progress bar status
8080
pub fn format(progress_bar: &ProgressBar) -> String {
81-
format!(
82-
r#"{{"timestamp": "{}", "bytes_downloaded": {}, "bytes_total": {}, "seconds_left": {}.{:0>3}, "seconds_elapsed": {}.{:0>3}}}"#,
81+
ProgressBarJsonFormatter::format_values(
8382
Utc::now().to_rfc3339(),
8483
progress_bar.position(),
8584
progress_bar.length().unwrap_or(0),
86-
progress_bar.eta().as_secs(),
87-
progress_bar.eta().subsec_millis(),
88-
progress_bar.elapsed().as_secs(),
89-
progress_bar.elapsed().subsec_millis(),
85+
progress_bar.eta(),
86+
progress_bar.elapsed(),
87+
)
88+
}
89+
90+
fn format_values(
91+
timestamp: String,
92+
bytes_downloaded: u64,
93+
bytes_total: u64,
94+
duration_left: Duration,
95+
duration_elapsed: Duration,
96+
) -> String {
97+
format!(
98+
r#"{{"timestamp": "{}", "bytes_downloaded": {}, "bytes_total": {}, "seconds_left": {}.{:0>3}, "seconds_elapsed": {}.{:0>3}}}"#,
99+
timestamp,
100+
bytes_downloaded,
101+
bytes_total,
102+
duration_left.as_secs(),
103+
duration_left.subsec_millis(),
104+
duration_elapsed.as_secs(),
105+
duration_elapsed.subsec_millis(),
90106
)
91107
}
92108
}
@@ -152,11 +168,20 @@ mod tests {
152168
use indicatif::ProgressBar;
153169

154170
#[test]
155-
fn check_seconds_elapsed_in_json_report_with_more_than_100_milliseconds() {
156-
let progress_bar = ProgressBar::new(10).with_elapsed(Duration::from_millis(5124));
157-
158-
let json_string = ProgressBarJsonFormatter::format(&progress_bar);
171+
fn check_seconds_formatting_in_json_report_with_more_than_100_milliseconds() {
172+
let json_string = ProgressBarJsonFormatter::format_values(
173+
"".to_string(),
174+
0,
175+
0,
176+
Duration::from_millis(7569),
177+
Duration::from_millis(5124),
178+
);
159179

180+
assert!(
181+
json_string.contains(r#""seconds_left": 7.569"#),
182+
"Not expected value in json output: {}",
183+
json_string
184+
);
160185
assert!(
161186
json_string.contains(r#""seconds_elapsed": 5.124"#),
162187
"Not expected value in json output: {}",
@@ -165,11 +190,20 @@ mod tests {
165190
}
166191

167192
#[test]
168-
fn check_seconds_elapsed_in_json_report_with_less_than_100_milliseconds() {
169-
let progress_bar = ProgressBar::new(10).with_elapsed(Duration::from_millis(5004));
170-
171-
let json_string = ProgressBarJsonFormatter::format(&progress_bar);
193+
fn check_seconds_formatting_in_json_report_with_less_than_100_milliseconds() {
194+
let json_string = ProgressBarJsonFormatter::format_values(
195+
"".to_string(),
196+
0,
197+
0,
198+
Duration::from_millis(7006),
199+
Duration::from_millis(5004),
200+
);
172201

202+
assert!(
203+
json_string.contains(r#""seconds_left": 7.006"#),
204+
"Not expected value in json output: {}",
205+
json_string
206+
);
173207
assert!(
174208
json_string.contains(r#""seconds_elapsed": 5.004"#),
175209
"Not expected value in json output: {}",
@@ -178,38 +212,54 @@ mod tests {
178212
}
179213

180214
#[test]
181-
fn check_seconds_left_in_json_report_with_more_than_100_milliseconds() {
182-
let half_position = 5;
183-
let progress_bar = ProgressBar::new(half_position * 2);
184-
sleep(Duration::from_millis(123));
185-
progress_bar.set_position(half_position);
186-
let json_string = ProgressBarJsonFormatter::format(&progress_bar);
215+
fn check_seconds_formatting_in_json_report_with_milliseconds_ending_by_zeros() {
216+
let json_string = ProgressBarJsonFormatter::format_values(
217+
"".to_string(),
218+
0,
219+
0,
220+
Duration::from_millis(7200),
221+
Duration::from_millis(5100),
222+
);
187223

188-
let milliseconds = progress_bar.eta().subsec_millis();
189-
assert!(milliseconds > 100);
190224
assert!(
191-
json_string.contains(&format!(r#""seconds_left": 0.{}"#, milliseconds)),
225+
json_string.contains(r#""seconds_left": 7.200"#),
226+
"Not expected value in json output: {}",
227+
json_string
228+
);
229+
assert!(
230+
json_string.contains(r#""seconds_elapsed": 5.100"#),
192231
"Not expected value in json output: {}",
193232
json_string
194233
);
195234
}
196235

197236
#[test]
198-
fn check_seconds_left_in_json_report_with_less_than_100_milliseconds() {
199-
let half_position = 5;
200-
let progress_bar = ProgressBar::new(half_position * 2);
201-
sleep(Duration::from_millis(1));
202-
progress_bar.set_position(half_position);
237+
fn check_seconds_left_and_elapsed_time_are_used_by_the_formatter() {
238+
// 4 steps
239+
let progress_bar = ProgressBar::new(4);
240+
// 1 step done in 15 ms, left 45ms to finish the 4th steps
241+
sleep(Duration::from_millis(15));
242+
progress_bar.set_position(1);
243+
203244
let json_string = ProgressBarJsonFormatter::format(&progress_bar);
204245

246+
let milliseconds_left = progress_bar.eta().as_millis();
247+
let milliseconds_elapsed = progress_bar.elapsed().as_millis();
248+
249+
// Milliseconds in json may not be exactly the same as the one we get because of the test duration.
250+
// We need to have a difference not more than 4ms to keep the same 2 first milliseconds digits.
251+
let delta = 4;
252+
253+
assert!(((45 - delta)..=(45 + delta)).contains(&milliseconds_left));
205254
assert!(
206-
json_string.contains(r#""seconds_left": 0.0"#),
255+
json_string.contains(r#""seconds_left": 0.04"#), // Should be close to 0.045
207256
"Not expected value in json output: {}",
208257
json_string
209258
);
210259

260+
assert!(((15 - delta)..(15 + delta)).contains(&milliseconds_elapsed));
211261
assert!(
212-
!json_string.contains(r#""seconds_left": 0.000"#),
262+
json_string.contains(r#""seconds_elapsed": 0.01"#), // Should be close to 0.015
213263
"Not expected value in json output: {}",
214264
json_string
215265
);

0 commit comments

Comments
 (0)