Skip to content

Commit

Permalink
chore(*): enable thelper linter
Browse files Browse the repository at this point in the history
Signed-off-by: Soren Yang <[email protected]>
  • Loading branch information
lsytj0413 committed Dec 7, 2023
1 parent c7f7fd3 commit 40f1e7e
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ linters:
- sqlclosecheck
- tenv
# - testableexamples
# - thelper
- thelper
- tparallel
- unconvert
- unparam
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions common/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions common/wait_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions coordinator/impl/coordinator_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions coordinator/impl/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
}
Expand Down
6 changes: 6 additions & 0 deletions coordinator/impl/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -135,13 +137,17 @@ 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)
assert.Equal(t, term, r.Term)
}

func (m *mockPerNodeChannels) expectAddFollowerRequest(t *testing.T, shard int64, term int64) {
t.Helper()

r := <-m.addFollowerRequests

assert.Equal(t, shard, r.ShardId)
Expand Down
8 changes: 4 additions & 4 deletions oxia/async_client_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion oxia/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
10 changes: 10 additions & 0 deletions oxia/internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion oxia/internal/shard_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions oxia/sync_client_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions server/follower_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
})
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions server/kv/notifications_trimmer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions server/leader_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion server/session_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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++ {
Expand All @@ -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,
Expand All @@ -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)
Expand Down
11 changes: 1 addition & 10 deletions server/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ package server
import (
"context"
"log/slog"
"os"
"path/filepath"
"testing"

"go.uber.org/multierr"

Expand Down Expand Up @@ -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"),
Expand Down
8 changes: 8 additions & 0 deletions server/wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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++ {
Expand Down

0 comments on commit 40f1e7e

Please sign in to comment.