Codebase list golang-github-vbauerster-mpb / c7cbd9b
TestBarPriorityPopOrder Vladimir Bauer 3 years ago
1 changed file(s) with 42 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
165165 }
166166 }
167167
168 func TestUpdateBarPriority(t *testing.T) {
168 func TestBarPriorityPopOrder(t *testing.T) {
169169 shutdown := make(chan interface{})
170170 ctx, cancel := context.WithCancel(context.Background())
171171 p := mpb.NewWithContext(ctx,
182182 c: "c",
183183 }
184184
185 p.UpdateBarPriority(c, 2)
186 p.UpdateBarPriority(b, 3)
187
188185 cancel()
189186
190187 bars := (<-shutdown).([]*mpb.Bar)
195192 p.Wait()
196193 pq := mpb.PriorityQueue(bars)
197194
195 if bar := heap.Pop(&pq).(*mpb.Bar); bar != c {
196 t.Errorf("Expected bar c, got: %s", identity[bar])
197 }
198198 if bar := heap.Pop(&pq).(*mpb.Bar); bar != b {
199199 t.Errorf("Expected bar b, got: %s", identity[bar])
200200 }
201 if bar := heap.Pop(&pq).(*mpb.Bar); bar != a {
202 t.Errorf("Expected bar a, got: %s", identity[bar])
203 }
204 }
205
206 func TestUpdateBarPriority(t *testing.T) {
207 shutdown := make(chan interface{})
208 ctx, cancel := context.WithCancel(context.Background())
209 p := mpb.NewWithContext(ctx,
210 mpb.WithOutput(io.Discard),
211 mpb.WithShutdownNotifier(shutdown),
212 )
213 a := p.AddBar(100, mpb.BarPriority(1))
214 b := p.AddBar(100, mpb.BarPriority(2))
215 c := p.AddBar(100, mpb.BarPriority(3))
216
217 identity := map[*mpb.Bar]string{
218 a: "a",
219 b: "b",
220 c: "c",
221 }
222
223 p.UpdateBarPriority(c, 2)
224 p.UpdateBarPriority(b, 3)
225
226 cancel()
227
228 bars := (<-shutdown).([]*mpb.Bar)
229 if l := len(bars); l != 3 {
230 t.Errorf("Expected len of bars: %d, got: %d", 3, l)
231 }
232
233 p.Wait()
234 pq := mpb.PriorityQueue(bars)
235
236 if bar := heap.Pop(&pq).(*mpb.Bar); bar != b {
237 t.Errorf("Expected bar b, got: %s", identity[bar])
238 }
201239 if bar := heap.Pop(&pq).(*mpb.Bar); bar != c {
202240 t.Errorf("Expected bar c, got: %s", identity[bar])
203241 }