|
| 1 | +package priorityqueue |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | +) |
| 9 | + |
| 10 | +var _ = Describe("FairQueue", func() { |
| 11 | + DescribeTable("Ordering tests", |
| 12 | + Entry("simple", |
| 13 | + []string{"foo", "foo", "bar"}, |
| 14 | + []string{"bar", "foo", "foo"}, |
| 15 | + ), |
| 16 | + Entry("balanced", |
| 17 | + []string{"foo", "foo", "bar", "bar"}, |
| 18 | + []string{"bar", "foo", "bar", "foo"}, |
| 19 | + ), |
| 20 | + Entry("mixed-dimensional, multi-dimensional comes first", |
| 21 | + []string{"foo-foo", "foo-foo", "foo"}, |
| 22 | + []string{"foo", "foo-foo", "foo-foo"}, |
| 23 | + ), |
| 24 | + Entry("mixed-dimensional, single dimension comes first", |
| 25 | + []string{"foo", "foo-foo", "foo-foo"}, |
| 26 | + []string{"foo-foo", "foo", "foo-foo"}, |
| 27 | + ), |
| 28 | + Entry("complex mixed-dimensional", |
| 29 | + []string{"bar", "foo", "foo-foo", "foo-foo", "foo-bar"}, |
| 30 | + // First the foo with the latest-added second dimension, then the bar, |
| 31 | + // then the remaining foos based on the second dimension. |
| 32 | + []string{"foo-bar", "bar", "foo-foo", "foo", "foo-foo"}, |
| 33 | + ), |
| 34 | + |
| 35 | + func(adds []string, expected []string) { |
| 36 | + q := &fairq[string]{} |
| 37 | + for idx, add := range adds { |
| 38 | + q.add(&item[string]{Key: add, AddedCounter: uint64(idx)}, strings.Split(add, "-")...) |
| 39 | + } |
| 40 | + |
| 41 | + actual := make([]string, 0, len(expected)) |
| 42 | + for range len(expected) { |
| 43 | + item, didGetItem := q.dequeue() |
| 44 | + Expect(didGetItem).To(BeTrue()) |
| 45 | + actual = append(actual, item.Key) |
| 46 | + } |
| 47 | + |
| 48 | + _, didGetItem := q.dequeue() |
| 49 | + Expect(didGetItem).To(BeFalse()) |
| 50 | + Expect(actual).To(Equal(expected)) |
| 51 | + }, |
| 52 | + ) |
| 53 | + |
| 54 | + It("retains fairness across queue operations", func() { |
| 55 | + q := &fairq[string]{} |
| 56 | + q.add(&item[string]{Key: "foo"}, "foo") |
| 57 | + _, _ = q.dequeue() |
| 58 | + q.add(&item[string]{Key: "bar", AddedCounter: 1}, "bar") |
| 59 | + q.add(&item[string]{Key: "foo", AddedCounter: 2}, "foo") |
| 60 | + |
| 61 | + item, _ := q.dequeue() |
| 62 | + Expect(item.Key).To(Equal("bar")) |
| 63 | + Expect(q.isEmpty()).To(BeFalse()) |
| 64 | + }) |
| 65 | + |
| 66 | + It("sorts by added counter", func() { |
| 67 | + q := &fairq[string]{} |
| 68 | + q.add(&item[string]{Key: "foo", AddedCounter: 2}) |
| 69 | + q.add(&item[string]{Key: "bar", AddedCounter: 1}) |
| 70 | + |
| 71 | + item, _ := q.dequeue() |
| 72 | + Expect(item.Key).To(Equal("bar")) |
| 73 | + Expect(q.isEmpty()).To(BeFalse()) |
| 74 | + }) |
| 75 | + |
| 76 | + It("drains all items", func() { |
| 77 | + q := &fairq[string]{} |
| 78 | + q.add(&item[string]{Key: "foo"}, "foo") |
| 79 | + q.add(&item[string]{Key: "bar"}, "bar") |
| 80 | + q.add(&item[string]{Key: "baz"}, "baz") |
| 81 | + |
| 82 | + q.drain() |
| 83 | + _, gotItem := q.dequeue() |
| 84 | + Expect(gotItem).To(BeFalse()) |
| 85 | + Expect(q.isEmpty()).To(BeTrue()) |
| 86 | + }) |
| 87 | +}) |
0 commit comments