fix: bugs in refactored task queue and improved coverage

This commit is contained in:
garethgeorge
2024-04-11 01:46:50 -07:00
parent eb07230cc0
commit 834b74f0f3
3 changed files with 49 additions and 20 deletions

View File

@@ -2,6 +2,7 @@ package queue
import (
"context"
"math/rand"
"testing"
"time"
)
@@ -53,3 +54,33 @@ func TestTPQMixedReadinessStates(t *testing.T) {
}
}
}
func TestTPQStress(t *testing.T) {
tpq := NewTimePriorityQueue[int]()
start := time.Now()
totalEnqueued := 0
totalEnqueuedSum := 0
go func() {
ctx, _ := context.WithDeadline(context.Background(), start.Add(1*time.Second))
for ctx.Err() == nil {
v := rand.Intn(100)
tpq.Enqueue(time.Now().Add(time.Duration(rand.Intn(1000)-500)*time.Millisecond), rand.Intn(5), v)
totalEnqueuedSum += v
totalEnqueued++
}
}()
ctx, _ := context.WithDeadline(context.Background(), start.Add(3*time.Second))
totalDequeued := 0
sum := 0
for ctx.Err() == nil || totalDequeued < totalEnqueued {
sum += tpq.Dequeue(ctx)
totalDequeued++
}
if sum != totalEnqueuedSum {
t.Errorf("expected sum to be %d, got %d", totalEnqueuedSum, sum)
}
}