mirror of
https://github.com/garethgeorge/backrest.git
synced 2025-12-16 18:45:36 +00:00
fix: bugs in refactored task queue and improved coverage
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user