chore: Fix flaky `TestAcquireWithTiming` test (#15150)

Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
pull/14612/head
Christian Haudum 1 year ago committed by GitHub
parent 4b5925a28e
commit defe3af87f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 17
      pkg/querier/queryrange/limits_test.go

@ -772,14 +772,13 @@ func Test_MaxQuerySize_MaxLookBackPeriod(t *testing.T) {
}
func TestAcquireWithTiming(t *testing.T) {
ctx := context.Background()
sem := NewSemaphoreWithTiming(2)
// Channel to collect waiting times
waitingTimes := make(chan struct {
GoroutineID int
WaitingTime int64
WaitingTime time.Duration
}, 3)
tryAcquire := func(n int64, goroutineID int) {
@ -789,8 +788,8 @@ func TestAcquireWithTiming(t *testing.T) {
}
waitingTimes <- struct {
GoroutineID int
WaitingTime int64
}{goroutineID, elapsed.Milliseconds()}
WaitingTime time.Duration
}{goroutineID, elapsed}
defer sem.sem.Release(n)
@ -808,13 +807,13 @@ func TestAcquireWithTiming(t *testing.T) {
// Collect and sort waiting times
var waitingDurations []struct {
GoroutineID int
WaitingTime int64
WaitingTime time.Duration
}
for i := 0; i < 3; i++ {
waitingDurations = append(waitingDurations, <-waitingTimes)
}
// Find and check the waiting time for the third goroutine
var waiting3 int64
var waiting3 time.Duration
for _, waiting := range waitingDurations {
if waiting.GoroutineID == 3 {
waiting3 = waiting.WaitingTime
@ -822,7 +821,7 @@ func TestAcquireWithTiming(t *testing.T) {
}
}
// Check that the waiting time for the third request is larger than 0 milliseconds and less than or equal to 10-5=5 milliseconds
require.Greater(t, waiting3, 0*time.Millisecond)
require.LessOrEqual(t, waiting3, 5*time.Millisecond)
// Check that the waiting time for the third request is larger than 0 milliseconds and less than 10 milliseconds
require.Greater(t, waiting3, 0*time.Nanosecond)
require.Less(t, waiting3, 10*time.Millisecond)
}

Loading…
Cancel
Save