TestBarPristinePopOrder
Vladimir Bauer
2 years ago
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | func TestBarPriorityPopOrder(t *testing.T) { | |
| 168 | func TestBarPristinePopOrder(t *testing.T) { | |
| 169 | 169 | shutdown := make(chan interface{}) |
| 170 | 170 | ctx, cancel := context.WithCancel(context.Background()) |
| 171 | 171 | 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() | |
| 186 | 181 | |
| 187 | 182 | bars := (<-shutdown).([]*mpb.Bar) |
| 188 | 183 | 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) | |
| 190 | 185 | } |
| 191 | 186 | |
| 192 | 187 | p.Wait() |
| 193 | 188 | pq := mpb.PriorityQueue(bars) |
| 194 | 189 | |
| 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 | } | |
| 203 | 195 | } |
| 204 | 196 | } |
| 205 | 197 |