Codebase list golang-github-vbauerster-mpb / 60ca62e
TestBarGetID Vladimir Bauer 9 years ago
1 changed file(s) with 33 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "bytes"
4 "sync"
45 "testing"
56 "time"
67 "unicode/utf8"
154155 }
155156 }
156157
158 func TestBarGetID(t *testing.T) {
159 var wg sync.WaitGroup
160 var buf bytes.Buffer
161 p := mpb.New().SetOut(&buf)
162
163 numBars := 3
164 wg.Add(numBars)
165
166 bars := make([]*mpb.Bar, numBars)
167 for i := 0; i < numBars; i++ {
168 bars[i] = p.AddBarWithID(i, 100)
169
170 go func(bar *mpb.Bar) {
171 defer wg.Done()
172 for i := 0; i < 100; i++ {
173 time.Sleep(10 * time.Millisecond)
174 bar.Incr(1)
175 }
176 }(bars[i])
177 }
178
179 for wantID, bar := range bars {
180 gotID := bar.GetID()
181 if gotID != wantID {
182 t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID)
183 }
184 }
185
186 wg.Wait()
187 p.Stop()
188 }
189
157190 func removeLastRune(bytes []byte) []byte {
158191 _, size := utf8.DecodeLastRune(bytes)
159192 return bytes[:len(bytes)-size]