@@ -166,6 +166,7 @@ mod tests {
166
166
167
167
use super :: * ;
168
168
use indicatif:: ProgressBar ;
169
+ use serde_json:: Value ;
169
170
170
171
#[ test]
171
172
fn check_seconds_formatting_in_json_report_with_more_than_100_milliseconds ( ) {
@@ -235,54 +236,60 @@ mod tests {
235
236
236
237
#[ test]
237
238
fn check_seconds_left_and_elapsed_time_are_used_by_the_formatter ( ) {
238
- let expected_milliseconds_left: u128 = 45 ;
239
- let expected_milliseconds_elapsed: u128 = 15 ;
239
+ fn format_duration ( duration : & Duration ) -> String {
240
+ format ! ( "{}.{}" , duration. as_secs( ) , duration. subsec_nanos( ) )
241
+ }
242
+ fn round_at_ms ( duration : Duration ) -> Duration {
243
+ Duration :: from_millis ( duration. as_millis ( ) as u64 )
244
+ }
240
245
241
246
// 4 steps
242
247
let progress_bar = ProgressBar :: new ( 4 ) ;
243
- // 1 step done in 150 ms, left 450ms to finish the 4th steps
244
- sleep ( Duration :: from_millis ( expected_milliseconds_elapsed as u64 ) ) ;
248
+ // 1 step done in 15 ms, left 45ms to finish the 4th steps
249
+ sleep ( Duration :: from_millis ( 15 ) ) ;
245
250
progress_bar. set_position ( 1 ) ;
246
251
252
+ let duration_left_before = round_at_ms ( progress_bar. eta ( ) ) ;
253
+ let duration_elapsed_before = round_at_ms ( progress_bar. elapsed ( ) ) ;
254
+
247
255
let json_string = ProgressBarJsonFormatter :: format ( & progress_bar) ;
248
256
249
- let milliseconds_left = progress_bar. eta ( ) . as_millis ( ) ;
250
- let milliseconds_elapsed = progress_bar. elapsed ( ) . as_millis ( ) ;
257
+ let duration_left_after = round_at_ms ( progress_bar. eta ( ) ) ;
258
+ let duration_elapsed_after = round_at_ms ( progress_bar. elapsed ( ) ) ;
251
259
252
260
// Milliseconds in json may not be exactly the same as the one we get because of the test duration.
253
- // We need to have a difference not more than 49ms to keep the same 1 first milliseconds digits.
254
- let delta = 4 ;
255
- // TODO: to remove, it-s just to investigation on CI.
256
- let error_message = format ! (
257
- "\n - milliseconds_left:{milliseconds_left}\n - milliseconds_elapsed:{milliseconds_elapsed}\n - json_string:{json_string}"
258
- ) ;
261
+ let delta = 0.1 ;
259
262
263
+ let json_value: Value = serde_json:: from_str ( & json_string) . unwrap ( ) ;
264
+ let seconds_left = json_value[ "seconds_left" ] . as_f64 ( ) . unwrap ( ) ;
265
+ let seconds_elapsed = json_value[ "seconds_elapsed" ] . as_f64 ( ) . unwrap ( ) ;
266
+
267
+ // We check that we pass the right values to format checking that time left is 3 times the time elapsed
260
268
assert ! (
261
- ( ( expected_milliseconds_left - delta) ..=( expected_milliseconds_left + delta) )
262
- . contains( & milliseconds_left) ,
263
- "milliseconds_left should be close to {} but it's {}. {}" ,
264
- & expected_milliseconds_left,
265
- & milliseconds_left,
266
- & error_message
267
- ) ;
268
- assert ! (
269
- json_string. contains( r#""seconds_left": 0.04"# ) , // Should be close to 0.450
270
- "Not expected value in json output: {}" ,
271
- json_string
269
+ seconds_elapsed * 3.0 - delta < seconds_left
270
+ && seconds_left < seconds_elapsed * 3.0 + delta,
271
+ "seconds_left should be close to 3*{} but it's {}." ,
272
+ & seconds_elapsed,
273
+ & seconds_left
272
274
) ;
273
275
276
+ let duration_left = Duration :: from_secs_f64 ( seconds_left) ;
274
277
assert ! (
275
- ( ( expected_milliseconds_elapsed - delta) ..=( expected_milliseconds_elapsed + delta) )
276
- . contains( & milliseconds_elapsed) ,
277
- "milliseconds_elapsed should be close to {} but it's {}. {}" ,
278
- & expected_milliseconds_elapsed,
279
- & milliseconds_elapsed,
280
- & error_message
278
+ duration_left_before <= duration_left && duration_left <= duration_left_after,
279
+ "Duration left: {} should be between {} and {}" ,
280
+ format_duration( & duration_left) ,
281
+ format_duration( & duration_left_before) ,
282
+ format_duration( & duration_left_after) ,
281
283
) ;
284
+
285
+ let duration_elapsed = Duration :: from_secs_f64 ( seconds_elapsed) ;
282
286
assert ! (
283
- json_string. contains( r#""seconds_elapsed": 0.01"# ) , // Should be close to 0.150
284
- "Not expected value in json output: {}" ,
285
- json_string
287
+ duration_elapsed_before <= duration_elapsed
288
+ && duration_elapsed <= duration_elapsed_after,
289
+ "Duration elapsed: {} should be between {} and {}" ,
290
+ format_duration( & duration_elapsed) ,
291
+ format_duration( & duration_elapsed_before) ,
292
+ format_duration( & duration_elapsed_after) ,
286
293
) ;
287
294
}
288
295
}
0 commit comments