TestBarGetID
Vladimir Bauer
9 years ago
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | 3 | "bytes" |
| 4 | "sync" | |
| 4 | 5 | "testing" |
| 5 | 6 | "time" |
| 6 | 7 | "unicode/utf8" |
| 154 | 155 | } |
| 155 | 156 | } |
| 156 | 157 | |
| 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 | ||
| 157 | 190 | func removeLastRune(bytes []byte) []byte { |
| 158 | 191 | _, size := utf8.DecodeLastRune(bytes) |
| 159 | 192 | return bytes[:len(bytes)-size] |