TestDecorStatisticsAvailableWidth refactoring
Vladimir Bauer
5 years ago
| 258 | 258 | if s.AvailableWidth != 80 { |
| 259 | 259 | t.Errorf("expected AvailableWidth %d got %d\n", 80, s.AvailableWidth) |
| 260 | 260 | } |
| 261 | return fmt.Sprintf("\x1b[31;1;4m%s\x1b[0m", strings.Repeat("1", 20)) | |
| 262 | } | |
| 261 | return fmt.Sprintf("\x1b[31;1;4m%s\x1b[0m", strings.Repeat("0", 20)) | |
| 262 | } | |
| 263 | checkDone := make(chan struct{}) | |
| 263 | 264 | td2 := func(s decor.Statistics) string { |
| 264 | if s.AvailableWidth != 60 { | |
| 265 | t.Errorf("expected AvailableWidth %d got %d\n", 60, s.AvailableWidth) | |
| 265 | defer func() { | |
| 266 | checkDone <- struct{}{} | |
| 267 | }() | |
| 268 | if s.AvailableWidth != 40 { | |
| 269 | t.Errorf("expected AvailableWidth %d got %d\n", 40, s.AvailableWidth) | |
| 266 | 270 | } |
| 267 | 271 | return "" |
| 268 | 272 | } |
| 269 | 273 | total := 100 |
| 274 | down := make(chan struct{}) | |
| 270 | 275 | p := mpb.New( |
| 271 | 276 | mpb.WithWidth(100), |
| 277 | mpb.WithShutdownNotifier(down), | |
| 272 | 278 | mpb.WithOutput(ioutil.Discard), |
| 273 | 279 | ) |
| 274 | 280 | bar := p.AddBar(int64(total), |
| 278 | 284 | decor.Any(td1), |
| 279 | 285 | ), |
| 280 | 286 | mpb.AppendDecorators( |
| 287 | decor.Name(strings.Repeat("0", 20)), | |
| 281 | 288 | decor.Any(td2), |
| 282 | 289 | ), |
| 283 | 290 | ) |
| 284 | for i := 0; i < total; i++ { | |
| 285 | time.Sleep(10 * time.Millisecond) | |
| 286 | bar.Increment() | |
| 287 | } | |
| 291 | go func() { | |
| 292 | for { | |
| 293 | select { | |
| 294 | case <-checkDone: | |
| 295 | bar.Abort(false) | |
| 296 | case <-down: | |
| 297 | return | |
| 298 | } | |
| 299 | } | |
| 300 | }() | |
| 301 | for !bar.Completed() { | |
| 302 | bar.Increment() | |
| 303 | } | |
| 304 | p.Wait() | |
| 288 | 305 | } |
| 289 | 306 | |
| 290 | 307 | func panicDecorator(panicMsg string, cond func(decor.Statistics) bool) decor.Decorator { |