Codebase list golang-github-vbauerster-mpb / 1e6e311
TestBarPristinePopOrder Vladimir Bauer 2 years ago
1 changed file(s) with 16 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
165165 }
166166 }
167167
168 func TestBarPriorityPopOrder(t *testing.T) {
168 func TestBarPristinePopOrder(t *testing.T) {
169169 shutdown := make(chan interface{})
170170 ctx, cancel := context.WithCancel(context.Background())
171171 p := mpb.NewWithContext(ctx,
172 mpb.WithOutput(io.Discard),
173 mpb.WithShutdownNotifier(shutdown),
174 )
175 a := p.AddBar(100, mpb.BarPriority(1))
176 b := p.AddBar(100, mpb.BarPriority(2))
177 c := p.AddBar(100, mpb.BarPriority(3))
178
179 identity := map[*mpb.Bar]string{
180 a: "a",
181 b: "b",
182 c: "c",
183 }
184
185 cancel()
172 mpb.WithOutput(io.Discard), // auto refresh is disabled
173 mpb.WithShutdownNotifier(shutdown),
174 )
175 a := p.AddBar(100, mpb.BarPriority(1), mpb.BarID(1))
176 b := p.AddBar(100, mpb.BarPriority(2), mpb.BarID(2))
177 c := p.AddBar(100, mpb.BarPriority(3), mpb.BarID(3))
178 pristineOrder := []*mpb.Bar{c, b, a}
179
180 go cancel()
186181
187182 bars := (<-shutdown).([]*mpb.Bar)
188183 if l := len(bars); l != 3 {
189 t.Errorf("Expected len of bars: %d, got: %d", 3, l)
184 t.Fatalf("Expected len of bars: %d, got: %d", 3, l)
190185 }
191186
192187 p.Wait()
193188 pq := mpb.PriorityQueue(bars)
194189
195 if bar := heap.Pop(&pq).(*mpb.Bar); bar != c {
196 t.Errorf("Expected bar c, got: %s", identity[bar])
197 }
198 if bar := heap.Pop(&pq).(*mpb.Bar); bar != b {
199 t.Errorf("Expected bar b, got: %s", identity[bar])
200 }
201 if bar := heap.Pop(&pq).(*mpb.Bar); bar != a {
202 t.Errorf("Expected bar a, got: %s", identity[bar])
190 for _, b := range pristineOrder {
191 // higher priority pops first
192 if bar := heap.Pop(&pq).(*mpb.Bar); bar.ID() != b.ID() {
193 t.Errorf("Expected bar id: %d, got bar id: %d", b.ID(), bar.ID())
194 }
203195 }
204196 }
205197