TestBarPanics
Vladimir Bauer
9 years ago
| 221 | 221 | } |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | func TestBarPanics(t *testing.T) { | |
| 225 | var wg sync.WaitGroup | |
| 226 | var buf bytes.Buffer | |
| 227 | p := mpb.New().SetOut(&buf) | |
| 228 | ||
| 229 | wantPanic := "Upps!!!" | |
| 230 | numBars := 3 | |
| 231 | wg.Add(numBars) | |
| 232 | ||
| 233 | for i := 0; i < numBars; i++ { | |
| 234 | name := fmt.Sprintf("b#%02d:", i) | |
| 235 | bar := p.AddBarWithID(i, 100). | |
| 236 | PrependFunc(func(s *mpb.Statistics, yw chan<- int, mw <-chan int) string { | |
| 237 | if s.Id == 2 && s.Current >= 42 { | |
| 238 | panic(wantPanic) | |
| 239 | } | |
| 240 | return name | |
| 241 | }) | |
| 242 | ||
| 243 | go func() { | |
| 244 | defer wg.Done() | |
| 245 | for i := 0; i < 100; i++ { | |
| 246 | time.Sleep(10 * time.Millisecond) | |
| 247 | bar.Incr(1) | |
| 248 | } | |
| 249 | }() | |
| 250 | } | |
| 251 | ||
| 252 | wg.Wait() | |
| 253 | p.Stop() | |
| 254 | ||
| 255 | out := strings.Split(buf.String(), "\n") | |
| 256 | gotPanic := out[len(out)-2] | |
| 257 | if gotPanic != wantPanic { | |
| 258 | t.Errorf("Want panic: %s, got panic: %s\n", wantPanic, gotPanic) | |
| 259 | } | |
| 260 | } | |
| 261 | ||
| 224 | 262 | func removeLastRune(bytes []byte) []byte { |
| 225 | 263 | _, size := utf8.DecodeLastRune(bytes) |
| 226 | 264 | return bytes[:len(bytes)-size] |