Repository: nats-io/nats-server
Pull request #4515 (2023-09-12 10:04:08)

Allow more time in `TestFileStoreNumPendingLargeNumBlks`
This should hopefully de-flake `TestFileStoreNumPendingLargeNumBlks` a bit. By adding a new `require_LessThan` helper function, we also will produce a more meaningful log line that tells us what the bad values were if the test fails.

Signed-off-by: Neil Twigg <neil@nats.io>


Patch:
diff --git a/go.mod b/go.mod
index 07885260..25e4b8a8 100644
--- a/go.mod
+++ b/go.mod
@@ -11,7 +11,8 @@ require (
 	github.com/nats-io/nuid v1.0.1
 	go.uber.org/automaxprocs v1.5.3
 	golang.org/x/crypto v0.12.0
-	golang.org/x/sys v0.11.0
+	golang.org/x/exp v0.0.0-20230905200255-921286631fa9
+	golang.org/x/sys v0.12.0
 	golang.org/x/time v0.3.0
 )
 
diff --git a/go.sum b/go.sum
index 10ef8035..8ad42d6b 100644
--- a/go.sum
+++ b/go.sum
@@ -28,9 +28,11 @@ go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
 go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
 golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
 golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
+golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
+golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
 golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
-golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
+golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
 golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
 golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=


Test patch:
diff --git a/server/filestore_test.go b/server/filestore_test.go
index a51cd9ab..5b8234d2 100644
--- a/server/filestore_test.go
+++ b/server/filestore_test.go
@@ -5098,13 +5098,13 @@ func TestFileStoreNumPendingLargeNumBlks(t *testing.T) {
 
 	start := time.Now()
 	total, _ := fs.NumPending(4000, "zzz", false)
-	require_True(t, time.Since(start) < 10*time.Millisecond)
-	require_True(t, total == 6001)
+	require_LessThan(t, time.Since(start), 15*time.Millisecond)
+	require_Equal(t, total, 6001)
 
 	start = time.Now()
 	total, _ = fs.NumPending(6000, "zzz", false)
-	require_True(t, time.Since(start) < 10*time.Millisecond)
-	require_True(t, total == 4001)
+	require_LessThan(t, time.Since(start), 15*time.Millisecond)
+	require_Equal(t, total, 4001)
 
 	// Now delete a message in first half and second half.
 	fs.RemoveMsg(1000)
@@ -5112,13 +5112,13 @@ func TestFileStoreNumPendingLargeNumBlks(t *testing.T) {
 
 	start = time.Now()
 	total, _ = fs.NumPending(4000, "zzz", false)
-	require_True(t, time.Since(start) < 50*time.Millisecond)
-	require_True(t, total == 6000)
+	require_LessThan(t, time.Since(start), 50*time.Millisecond)
+	require_Equal(t, total, 6000)
 
 	start = time.Now()
 	total, _ = fs.NumPending(6000, "zzz", false)
-	require_True(t, time.Since(start) < 50*time.Millisecond)
-	require_True(t, total == 4000)
+	require_LessThan(t, time.Since(start), 50*time.Millisecond)
+	require_Equal(t, total, 4000)
 }
 
 func TestFileStoreSkipMsgAndNumBlocks(t *testing.T) {
diff --git a/server/test_test.go b/server/test_test.go
index 7ce6ae7a..204ce43b 100644
--- a/server/test_test.go
+++ b/server/test_test.go
@@ -21,6 +21,8 @@ import (
 	"strings"
 	"testing"
 	"time"
+
+	"golang.org/x/exp/constraints"
 )
 
 // DefaultTestOptions are default options for the unit tests.
@@ -133,6 +135,13 @@ func require_Len(t *testing.T, a, b int) {
 	}
 }
 
+func require_LessThan[T constraints.Ordered](t *testing.T, a, b T) {
+	t.Helper()
+	if a >= b {
+		t.Fatalf("require %v to be less than %v", a, b)
+	}
+}
+
 func checkNatsError(t *testing.T, e *ApiError, id ErrorIdentifier) {
 	t.Helper()
 	ae, ok := ApiErrors[id]