Codebase list golang-github-vbauerster-mpb / 39cdd40
TestBarPanics Vladimir Bauer 9 years ago
1 changed file(s) with 38 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
221221 }
222222 }
223223
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
224262 func removeLastRune(bytes []byte) []byte {
225263 _, size := utf8.DecodeLastRune(bytes)
226264 return bytes[:len(bytes)-size]