diff --git a/base/util_test_norace.go b/base/util_test_norace.go new file mode 100644 index 0000000000..ed601aa559 --- /dev/null +++ b/base/util_test_norace.go @@ -0,0 +1,19 @@ +// Copyright 2025-Present Couchbase, Inc. +// +// Use of this software is governed by the Business Source License included +// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified +// in that file, in accordance with the Business Source License, use of this +// software will be governed by the Apache License, Version 2.0, included in +// the file licenses/APL2.txt. + +//go:build !race +// +build !race + +package base + +import "testing" + +// IsRaceDetectorEnabled returns true if compiled with -race. Intended to be used for testing only. +func IsRaceDetectorEnabled(t *testing.T) bool { + return false +} diff --git a/base/util_test_race.go b/base/util_test_race.go new file mode 100644 index 0000000000..b34e39282b --- /dev/null +++ b/base/util_test_race.go @@ -0,0 +1,19 @@ +// Copyright 2025-Present Couchbase, Inc. +// +// Use of this software is governed by the Business Source License included +// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified +// in that file, in accordance with the Business Source License, use of this +// software will be governed by the Apache License, Version 2.0, included in +// the file licenses/APL2.txt. + +//go:build race +// +build race + +package base + +import "testing" + +// IsRaceDetectorEnabled returns true if compiled with -race. Intended to be used for testing only +func IsRaceDetectorEnabled(t *testing.T) bool { + return true +} diff --git a/db/database_test.go b/db/database_test.go index 046bf09d67..2507e00e49 100644 --- a/db/database_test.go +++ b/db/database_test.go @@ -13,6 +13,7 @@ import ( "encoding/json" "fmt" "log" + "runtime" "runtime/debug" "sort" "strconv" @@ -3363,6 +3364,12 @@ func TestUpdateCalculatedStatsPanic(t *testing.T) { } func Test_waitForBackgroundManagersToStop(t *testing.T) { + // use testing/synctest to have fake time once go 1.25 is available. In the meantime, windows and -race are + // slow enough thatthe time is increased, but this keeps interactive testing fast. + waitTime := 50 * time.Millisecond + if runtime.GOOS == "windows" || base.IsRaceDetectorEnabled(t) { + waitTime = 1 * time.Second + } t.Run("single unstoppable process", func(t *testing.T) { bgMngr := &BackgroundManager{ name: "test_unstoppable_runner", @@ -3375,7 +3382,7 @@ func Test_waitForBackgroundManagersToStop(t *testing.T) { require.NoError(t, err) startTime := time.Now() - deadline := 50 * time.Millisecond + deadline := waitTime waitForBackgroundManagersToStop(ctx, deadline, []*BackgroundManager{bgMngr}) assert.Greater(t, time.Since(startTime), deadline) assert.Equal(t, BackgroundProcessStateStopping, bgMngr.GetRunState()) @@ -3393,7 +3400,7 @@ func Test_waitForBackgroundManagersToStop(t *testing.T) { require.NoError(t, err) startTime := time.Now() - deadline := 100 * time.Millisecond + deadline := waitTime waitForBackgroundManagersToStop(ctx, deadline, []*BackgroundManager{bgMngr}) assert.Less(t, time.Since(startTime), deadline) assert.Equal(t, BackgroundProcessStateStopped, bgMngr.GetRunState()) @@ -3421,7 +3428,7 @@ func Test_waitForBackgroundManagersToStop(t *testing.T) { require.NoError(t, err) startTime := time.Now() - deadline := 100 * time.Millisecond + deadline := waitTime waitForBackgroundManagersToStop(ctx, deadline, []*BackgroundManager{stoppableBgMngr, unstoppableBgMngr}) assert.Greater(t, time.Since(startTime), deadline) assert.Equal(t, BackgroundProcessStateStopped, stoppableBgMngr.GetRunState())