diff --git a/bar_test.go b/bar_test.go index 2025561..083367c 100644 --- a/bar_test.go +++ b/bar_test.go @@ -2,6 +2,7 @@ import ( "bytes" + "sync" "testing" "time" "unicode/utf8" @@ -155,6 +156,38 @@ } } +func TestBarGetID(t *testing.T) { + var wg sync.WaitGroup + var buf bytes.Buffer + p := mpb.New().SetOut(&buf) + + numBars := 3 + wg.Add(numBars) + + bars := make([]*mpb.Bar, numBars) + for i := 0; i < numBars; i++ { + bars[i] = p.AddBarWithID(i, 100) + + go func(bar *mpb.Bar) { + defer wg.Done() + for i := 0; i < 100; i++ { + time.Sleep(10 * time.Millisecond) + bar.Incr(1) + } + }(bars[i]) + } + + for wantID, bar := range bars { + gotID := bar.GetID() + if gotID != wantID { + t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID) + } + } + + wg.Wait() + p.Stop() +} + func removeLastRune(bytes []byte) []byte { _, size := utf8.DecodeLastRune(bytes) return bytes[:len(bytes)-size]