TestBarPriorityPopOrder
Vladimir Bauer
3 years ago
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | func TestUpdateBarPriority(t *testing.T) { | |
| 168 | func TestBarPriorityPopOrder(t *testing.T) { | |
| 169 | 169 | shutdown := make(chan interface{}) |
| 170 | 170 | ctx, cancel := context.WithCancel(context.Background()) |
| 171 | 171 | p := mpb.NewWithContext(ctx, |
| 182 | 182 | c: "c", |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | p.UpdateBarPriority(c, 2) | |
| 186 | p.UpdateBarPriority(b, 3) | |
| 187 | ||
| 188 | 185 | cancel() |
| 189 | 186 | |
| 190 | 187 | bars := (<-shutdown).([]*mpb.Bar) |
| 195 | 192 | p.Wait() |
| 196 | 193 | pq := mpb.PriorityQueue(bars) |
| 197 | 194 | |
| 195 | if bar := heap.Pop(&pq).(*mpb.Bar); bar != c { | |
| 196 | t.Errorf("Expected bar c, got: %s", identity[bar]) | |
| 197 | } | |
| 198 | 198 | if bar := heap.Pop(&pq).(*mpb.Bar); bar != b { |
| 199 | 199 | t.Errorf("Expected bar b, got: %s", identity[bar]) |
| 200 | 200 | } |
| 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 | } | |
| 201 | 239 | if bar := heap.Pop(&pq).(*mpb.Bar); bar != c { |
| 202 | 240 | t.Errorf("Expected bar c, got: %s", identity[bar]) |
| 203 | 241 | } |