@@ -78,15 +78,31 @@ pub struct ProgressBarJsonFormatter;
78
78
impl ProgressBarJsonFormatter {
79
79
/// Get a json formatted string given the progress bar status
80
80
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 (
83
82
Utc :: now ( ) . to_rfc3339 ( ) ,
84
83
progress_bar. position ( ) ,
85
84
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( ) ,
90
106
)
91
107
}
92
108
}
@@ -152,11 +168,20 @@ mod tests {
152
168
use indicatif:: ProgressBar ;
153
169
154
170
#[ 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
+ ) ;
159
179
180
+ assert ! (
181
+ json_string. contains( r#""seconds_left": 7.569"# ) ,
182
+ "Not expected value in json output: {}" ,
183
+ json_string
184
+ ) ;
160
185
assert ! (
161
186
json_string. contains( r#""seconds_elapsed": 5.124"# ) ,
162
187
"Not expected value in json output: {}" ,
@@ -165,11 +190,20 @@ mod tests {
165
190
}
166
191
167
192
#[ 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
+ ) ;
172
201
202
+ assert ! (
203
+ json_string. contains( r#""seconds_left": 7.006"# ) ,
204
+ "Not expected value in json output: {}" ,
205
+ json_string
206
+ ) ;
173
207
assert ! (
174
208
json_string. contains( r#""seconds_elapsed": 5.004"# ) ,
175
209
"Not expected value in json output: {}" ,
@@ -178,38 +212,54 @@ mod tests {
178
212
}
179
213
180
214
#[ 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
+ ) ;
187
223
188
- let milliseconds = progress_bar. eta ( ) . subsec_millis ( ) ;
189
- assert ! ( milliseconds > 100 ) ;
190
224
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"# ) ,
192
231
"Not expected value in json output: {}" ,
193
232
json_string
194
233
) ;
195
234
}
196
235
197
236
#[ 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
+
203
244
let json_string = ProgressBarJsonFormatter :: format ( & progress_bar) ;
204
245
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) ) ;
205
254
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
207
256
"Not expected value in json output: {}" ,
208
257
json_string
209
258
) ;
210
259
260
+ assert ! ( ( ( 15 - delta) ..( 15 + delta) ) . contains( & milliseconds_elapsed) ) ;
211
261
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
213
263
"Not expected value in json output: {}" ,
214
264
json_string
215
265
) ;
0 commit comments