Skip to content

Commit

Permalink
fix: priority queue delay logic was inverse
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Dec 17, 2024
1 parent 9ba70fb commit adb35ed
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.Now().Sub(start) > (2*time.Second) && time.Now().Sub(start) < (3*time.Second) {

Check failure on line 243 in collections/priorityqueue_test.go

View workflow job for this annotation

GitHub Actions / lint

S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple)
g.Expect(items).To(Equal([]string{"item1"}))
}
if time.Now().Sub(start) > (3*time.Second) && time.Now().Sub(start) < (5*time.Second) {

Check failure on line 246 in collections/priorityqueue_test.go

View workflow job for this annotation

GitHub Actions / lint

S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple)
g.Expect(items).To(Equal([]string{"item1", "item1-delay-03s"}))
}
if time.Now().Sub(start) > (6 * time.Second) {

Check failure on line 249 in collections/priorityqueue_test.go

View workflow job for this annotation

GitHub Actions / lint

S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple)
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 adb35ed

Please sign in to comment.