diff --git a/.golangci.yaml b/.golangci.yaml index 7b3fa8f9..0d92bbd9 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -52,7 +52,7 @@ linters: - sqlclosecheck - tenv # - testableexamples - # - thelper + - thelper - tparallel - unconvert - unparam diff --git a/cmd/client/cmd_test.go b/cmd/client/cmd_test.go index 27a312f9..7df86545 100644 --- a/cmd/client/cmd_test.go +++ b/cmd/client/cmd_test.go @@ -29,7 +29,7 @@ import ( ) func TestClientCmd(t *testing.T) { - server, err := server.NewStandalone(server.NewTestConfig(t)) + server, err := server.NewStandalone(server.NewTestConfig(t.TempDir())) assert.NoError(t, err) serviceAddress := fmt.Sprintf("localhost:%d", server.RpcPort()) diff --git a/common/condition_test.go b/common/condition_test.go index 35986cbf..fd79f75b 100644 --- a/common/condition_test.go +++ b/common/condition_test.go @@ -329,6 +329,8 @@ func BenchmarkCond32(b *testing.B) { } func benchmarkCond(b *testing.B, waiters int) { + b.Helper() + m := &sync.Mutex{} c := NewConditionContext(m) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) diff --git a/common/wait_group_test.go b/common/wait_group_test.go index 8cf195ca..280862d2 100644 --- a/common/wait_group_test.go +++ b/common/wait_group_test.go @@ -24,6 +24,8 @@ import ( ) func assertNotReady(t *testing.T, wg WaitGroup) { + t.Helper() + done := make(chan bool, 1) ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) defer cancel() diff --git a/coordinator/impl/coordinator_e2e_test.go b/coordinator/impl/coordinator_e2e_test.go index f05cb4ee..81461a26 100644 --- a/coordinator/impl/coordinator_e2e_test.go +++ b/coordinator/impl/coordinator_e2e_test.go @@ -32,6 +32,8 @@ import ( ) func newServer(t *testing.T) (s *server.Server, addr model.ServerAddress) { + t.Helper() + var err error s, err = server.New(server.Config{ PublicServiceAddr: "localhost:0", @@ -578,6 +580,8 @@ func TestCoordinator_RebalanceCluster(t *testing.T) { } func checkServerLists(t *testing.T, expected, actual []model.ServerAddress) { + t.Helper() + assert.Equal(t, len(expected), len(actual)) mExpected := map[string]bool{} for _, x := range expected { diff --git a/coordinator/impl/metadata_test.go b/coordinator/impl/metadata_test.go index f2771ffa..5bb05c8f 100644 --- a/coordinator/impl/metadata_test.go +++ b/coordinator/impl/metadata_test.go @@ -31,13 +31,19 @@ var ( return f }() metadataProviders = map[string]func(t *testing.T) MetadataProvider{ - "memory": func(_ *testing.T) MetadataProvider { + "memory": func(t *testing.T) MetadataProvider { + t.Helper() + return NewMetadataProviderMemory() }, "file": func(t *testing.T) MetadataProvider { + t.Helper() + return NewMetadataProviderFile(filepath.Join(t.TempDir(), "metadata")) }, - "configmap": func(_ *testing.T) MetadataProvider { + "configmap": func(t *testing.T) MetadataProvider { + t.Helper() + return NewMetadataProviderConfigMap(_fake, "ns", "n") }, } diff --git a/coordinator/impl/mock_test.go b/coordinator/impl/mock_test.go index a8b31c0d..a44ad106 100644 --- a/coordinator/impl/mock_test.go +++ b/coordinator/impl/mock_test.go @@ -127,6 +127,8 @@ type mockPerNodeChannels struct { } func (m *mockPerNodeChannels) expectBecomeLeaderRequest(t *testing.T, shard int64, term int64, replicationFactor uint32) { + t.Helper() + r := <-m.becomeLeaderRequests assert.Equal(t, shard, r.ShardId) @@ -135,6 +137,8 @@ func (m *mockPerNodeChannels) expectBecomeLeaderRequest(t *testing.T, shard int6 } func (m *mockPerNodeChannels) expectNewTermRequest(t *testing.T, shard int64, term int64) { + t.Helper() + r := <-m.newTermRequests assert.Equal(t, shard, r.ShardId) @@ -142,6 +146,8 @@ func (m *mockPerNodeChannels) expectNewTermRequest(t *testing.T, shard int64, te } func (m *mockPerNodeChannels) expectAddFollowerRequest(t *testing.T, shard int64, term int64) { + t.Helper() + r := <-m.addFollowerRequests assert.Equal(t, shard, r.ShardId) diff --git a/oxia/async_client_impl_test.go b/oxia/async_client_impl_test.go index f468cfc9..1a428840 100644 --- a/oxia/async_client_impl_test.go +++ b/oxia/async_client_impl_test.go @@ -34,7 +34,7 @@ func init() { } func TestAsyncClientImpl(t *testing.T) { - server, err := server.NewStandalone(server.NewTestConfig(t)) + server, err := server.NewStandalone(server.NewTestConfig(t.TempDir())) assert.NoError(t, err) serviceAddress := fmt.Sprintf("localhost:%d", server.RpcPort()) @@ -91,7 +91,7 @@ func TestAsyncClientImpl(t *testing.T) { } func TestSyncClientImpl_Notifications(t *testing.T) { - server, err := server.NewStandalone(server.NewTestConfig(t)) + server, err := server.NewStandalone(server.NewTestConfig(t.TempDir())) assert.NoError(t, err) serviceAddress := fmt.Sprintf("localhost:%d", server.RpcPort()) @@ -179,7 +179,7 @@ func TestSyncClientImpl_Notifications(t *testing.T) { } func TestAsyncClientImpl_NotificationsClose(t *testing.T) { - server, err := server.NewStandalone(server.NewTestConfig(t)) + server, err := server.NewStandalone(server.NewTestConfig(t.TempDir())) assert.NoError(t, err) serviceAddress := fmt.Sprintf("localhost:%d", server.RpcPort()) @@ -204,7 +204,7 @@ func TestAsyncClientImpl_NotificationsClose(t *testing.T) { } func TestAsyncClientImpl_Sessions(t *testing.T) { - server, err := server.NewStandalone(server.NewTestConfig(t)) + server, err := server.NewStandalone(server.NewTestConfig(t.TempDir())) assert.NoError(t, err) serviceAddress := fmt.Sprintf("localhost:%d", server.RpcPort()) diff --git a/oxia/cache_test.go b/oxia/cache_test.go index c903a49d..d11faf6d 100644 --- a/oxia/cache_test.go +++ b/oxia/cache_test.go @@ -38,7 +38,8 @@ var standalone *server.Standalone var serviceAddress string func TestMain(m *testing.M) { - config := server.NewTestConfig(nil) + dir, _ := os.MkdirTemp(os.TempDir(), "oxia-test-*") + config := server.NewTestConfig(dir) standalone, _ = server.NewStandalone(config) defer standalone.Close() serviceAddress = fmt.Sprintf("localhost:%d", standalone.RpcPort()) diff --git a/oxia/internal/metrics/metrics_test.go b/oxia/internal/metrics/metrics_test.go index 1d8afe26..b5f8b36f 100644 --- a/oxia/internal/metrics/metrics_test.go +++ b/oxia/internal/metrics/metrics_test.go @@ -142,6 +142,8 @@ func setup(sinceFunc func(time.Time) time.Duration) (*Metrics, metric.Reader) { } func assertTimer(t *testing.T, rm metricdata.ResourceMetrics, name string, expectedType string, expectedResult string) { + t.Helper() + sums, err := counter[float64](rm, name) assert.NoError(t, err) assertDataPoints(t, sums, float64(1), expectedType, expectedResult) @@ -152,6 +154,8 @@ func assertTimer(t *testing.T, rm metricdata.ResourceMetrics, name string, expec } func assertHistogram(t *testing.T, rm metricdata.ResourceMetrics, name string, hasHistogram bool, expectedSum int64, expectedType string, expectedResult string) { + t.Helper() + datapoints, err := histogram(rm, name) if hasHistogram { @@ -192,17 +196,23 @@ func histogram(rm metricdata.ResourceMetrics, name string) ([]metricdata.Histogr } func assertDataPoints[N int64 | float64](t *testing.T, datapoints []metricdata.DataPoint[N], expectedValue N, expectedType string, expectedResult string) { + t.Helper() + assert.Equal(t, 1, len(datapoints)) assert.Equal(t, expectedValue, datapoints[0].Value) assertAttributes(t, datapoints[0].Attributes, expectedType, expectedResult) } func assertAttributes(t *testing.T, attrs attribute.Set, expectedType string, expectedResult string) { + t.Helper() + assert.Equal(t, 2, attrs.Len()) assertAttribute(t, attrs, "type", expectedType) assertAttribute(t, attrs, "result", expectedResult) } func assertAttribute(t *testing.T, attrs attribute.Set, key attribute.Key, expected string) { + t.Helper() + value, ok := attrs.Value(key) assert.True(t, ok) assert.Equal(t, expected, value.AsString()) diff --git a/oxia/internal/shard_manager_test.go b/oxia/internal/shard_manager_test.go index 21af26ae..c9c5fc65 100644 --- a/oxia/internal/shard_manager_test.go +++ b/oxia/internal/shard_manager_test.go @@ -35,7 +35,7 @@ func (s *testShardStrategy) Get(key string) func(Shard) bool { } func TestWithStandalone(t *testing.T) { - server, err := server.NewStandalone(server.NewTestConfig(t)) + server, err := server.NewStandalone(server.NewTestConfig(t.TempDir())) assert.NoError(t, err) clientPool := common.NewClientPool() diff --git a/oxia/sync_client_impl_test.go b/oxia/sync_client_impl_test.go index da80f25a..cc3cac45 100644 --- a/oxia/sync_client_impl_test.go +++ b/oxia/sync_client_impl_test.go @@ -74,6 +74,8 @@ func TestCancelContext(t *testing.T) { } func assertCancellable(t *testing.T, operationFunc func(context.Context) error) { + t.Helper() + ctx, cancel := context.WithCancel(context.Background()) errCh := make(chan error) diff --git a/server/follower_controller_test.go b/server/follower_controller_test.go index 3f1869a1..2519b621 100644 --- a/server/follower_controller_test.go +++ b/server/follower_controller_test.go @@ -43,6 +43,8 @@ func init() { } func newTestWalFactory(t *testing.T) wal.WalFactory { + t.Helper() + return wal.NewWalFactory(&wal.WalFactoryOptions{ BaseWalDir: t.TempDir(), SegmentSize: 128 * 1024, @@ -563,6 +565,8 @@ func TestFollower_RejectTruncateInvalidTerm(t *testing.T) { } func prepareTestDb(t *testing.T) kv.Snapshot { + t.Helper() + kvFactory, err := kv.NewPebbleKVFactory(&kv.KVFactoryOptions{ DataDir: t.TempDir(), }) @@ -980,6 +984,8 @@ func closeChanIsNotNil(fc FollowerController) func() bool { func createAddRequest(t *testing.T, term int64, offset int64, kvs map[string]string, commitOffset int64) *proto.Append { + t.Helper() + br := &proto.WriteRequest{} for k, v := range kvs { diff --git a/server/kv/notifications_trimmer_test.go b/server/kv/notifications_trimmer_test.go index a4866155..e09c339b 100644 --- a/server/kv/notifications_trimmer_test.go +++ b/server/kv/notifications_trimmer_test.go @@ -76,6 +76,8 @@ func TestNotificationsTrimmer(t *testing.T) { } func firstNotification(t *testing.T, db DB) int64 { + t.Helper() + nextNotifications, err := db.ReadNextNotifications(context.Background(), 0) assert.NoError(t, err) diff --git a/server/leader_controller_test.go b/server/leader_controller_test.go index dc892152..67a62a8e 100644 --- a/server/leader_controller_test.go +++ b/server/leader_controller_test.go @@ -31,6 +31,8 @@ import ( ) func AssertProtoEqual(t *testing.T, expected, actual pb.Message) { + t.Helper() + if !pb.Equal(expected, actual) { protoMarshal := protojson.MarshalOptions{ EmitUnpopulated: true, diff --git a/server/session_manager_test.go b/server/session_manager_test.go index 7f041bd1..ee5e89ce 100644 --- a/server/session_manager_test.go +++ b/server/session_manager_test.go @@ -197,6 +197,8 @@ func TestSessionUpdateOperationCallback_OnPut(t *testing.T) { } func storageEntry(t *testing.T, sessionId int64) []byte { + t.Helper() + entry := &proto.StorageEntry{ Value: nil, VersionId: 0, @@ -318,10 +320,11 @@ func TestSessionManager(t *testing.T) { }, 10*time.Second, 30*time.Millisecond) assert.NoError(t, lc.Close()) - } func getData(t *testing.T, lc *leaderController, key string) string { + t.Helper() + resp, err := lc.db.Get(&proto.GetRequest{ Key: key, IncludeValue: true, @@ -334,6 +337,8 @@ func getData(t *testing.T, lc *leaderController, key string) string { } func keepAlive(t *testing.T, sManager *sessionManager, sessionId int64, err error, sleepTime time.Duration, heartbeatCount int) { + t.Helper() + go func() { assert.NoError(t, err) for i := 0; i < heartbeatCount; i++ { @@ -344,6 +349,8 @@ func keepAlive(t *testing.T, sManager *sessionManager, sessionId int64, err erro } func getSessionMetadata(t *testing.T, lc *leaderController, sessionId int64) *proto.SessionMetadata { + t.Helper() + resp, err := lc.db.Get(&proto.GetRequest{ Key: SessionKey(SessionId(sessionId)), IncludeValue: true, @@ -361,6 +368,8 @@ func getSessionMetadata(t *testing.T, lc *leaderController, sessionId int64) *pr } func createSessionManager(t *testing.T) (*sessionManager, *leaderController) { + t.Helper() + var shard int64 = 1 kvFactory, err := kv.NewPebbleKVFactory(testKVOptions) diff --git a/server/standalone.go b/server/standalone.go index 97a8104b..d572c342 100644 --- a/server/standalone.go +++ b/server/standalone.go @@ -17,9 +17,7 @@ package server import ( "context" "log/slog" - "os" "path/filepath" - "testing" "go.uber.org/multierr" @@ -47,14 +45,7 @@ type Standalone struct { metrics *metrics.PrometheusMetrics } -func NewTestConfig(t *testing.T) StandaloneConfig { - var dir string - if t == nil { - dir, _ = os.MkdirTemp(os.TempDir(), "oxia-test-*") - } else { - dir = t.TempDir() - } - +func NewTestConfig(dir string) StandaloneConfig { return StandaloneConfig{ Config: Config{ DataDir: filepath.Join(dir, "db"), diff --git a/server/wal/wal_test.go b/server/wal/wal_test.go index bf6fb879..d9ed55df 100644 --- a/server/wal/wal_test.go +++ b/server/wal/wal_test.go @@ -30,6 +30,8 @@ import ( const shard = int64(100) func NewTestWalFactory(t *testing.T) WalFactory { + t.Helper() + dir := t.TempDir() return NewWalFactory(&WalFactoryOptions{ BaseWalDir: dir, @@ -39,6 +41,8 @@ func NewTestWalFactory(t *testing.T) WalFactory { } func createWal(t *testing.T) (WalFactory, Wal) { + t.Helper() + f := NewTestWalFactory(t) w, err := f.NewWal(common.DefaultNamespace, shard, nil) assert.NoError(t, err) @@ -47,6 +51,8 @@ func createWal(t *testing.T) (WalFactory, Wal) { } func assertReaderReads(t *testing.T, r WalReader, entries []string) { + t.Helper() + for i := 0; i < len(entries); i++ { assert.True(t, r.HasNext()) e, err := r.ReadNext() @@ -57,6 +63,8 @@ func assertReaderReads(t *testing.T, r WalReader, entries []string) { } func assertReaderReadsEventually(t *testing.T, r WalReader, entries []string) chan error { + t.Helper() + ch := make(chan error) go func() { for i := 0; i < len(entries); i++ {