Skip to content

Commit

Permalink
fix: priority queue delay logic was inverse (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored Dec 17, 2024
1 parent 9ba70fb commit 31a0e12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
6 changes: 2 additions & 4 deletions collections/priorityqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,8 @@ func (queue *Queue[T]) Dequeue() (T, bool) {

// Peek for notBefore
v, _ := queue.heap.Peek()
if !v.notBefore.IsZero() {
if time.Until(v.notBefore) < 0 {
return zero, false
}
if !v.notBefore.IsZero() && time.Until(v.notBefore) > 0 {
return zero, false
}

wrapper, ok := queue.heap.Pop()
Expand Down
24 changes: 19 additions & 5 deletions collections/priorityqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,35 @@ func TestPriorityQueueWithDelay(t *testing.T) {
g.Expect(err).To(BeNil())
g.Expect(pq.Size()).To(BeNumerically("==", 0))

pq.EnqueueWithDelay("item1-delay-10s", 10*time.Second)
pq.EnqueueWithDelay("item1-delay-05s", 5*time.Second)
pq.EnqueueWithDelay("item1-delay-06s", 6*time.Second)
pq.EnqueueWithDelay("item1-delay-03s", 3*time.Second)
pq.Enqueue("item1")

start := time.Now()

var items []string
for {
if pq.Size() == 0 {
break
}

item, _ := pq.Dequeue()
items = append(items, item)
item, valid := pq.Dequeue()
if valid {
items = append(items, item)
}

if time.Since(start) > (1*time.Second) && time.Since(start) < (2*time.Second) {
g.Expect(items).To(Equal([]string{"item1"}))
}
if time.Since(start) > (3*time.Second) && time.Since(start) < (5*time.Second) {
g.Expect(items).To(Equal([]string{"item1", "item1-delay-03s"}))
}
if time.Since(start) > (6 * time.Second) {
g.Expect(items).To(Equal([]string{"item1", "item1-delay-03s", "item1-delay-06s"}))
}
}

g.Expect(items).To(Equal([]string{"item1", "item1-delay-05s", "item1-delay-10s"}))
g.Expect(items).To(Equal([]string{"item1", "item1-delay-03s", "item1-delay-06s"}))
}

func first[T1 any, T2 any](a T1, _ T2) T1 {
Expand Down

0 comments on commit 31a0e12

Please sign in to comment.