Skip to content

Commit

Permalink
refactor: introduce *test packages in lieu of //go:build test (#3238
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ARR4N authored Aug 2, 2024
1 parent 5282943 commit c2f1f46
Show file tree
Hide file tree
Showing 85 changed files with 769 additions and 735 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ linters-settings:
- standard
- default
- blank
- dot
- prefix(github.com/ava-labs/avalanchego)
- alias
- dot
skip-generated: true
custom-order: true
gosec:
Expand Down
11 changes: 5 additions & 6 deletions cache/test_cacher.go → cache/cachetest/cacher.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package cache
package cachetest

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/ids"
)

Expand All @@ -22,13 +21,13 @@ func TestIntSizeFunc(ids.ID, int64) int {
// CacherTests is a list of all Cacher tests
var CacherTests = []struct {
Size int
Func func(t *testing.T, c Cacher[ids.ID, int64])
Func func(t *testing.T, c cache.Cacher[ids.ID, int64])
}{
{Size: 1, Func: TestBasic},
{Size: 2, Func: TestEviction},
}

func TestBasic(t *testing.T, cache Cacher[ids.ID, int64]) {
func TestBasic(t *testing.T, cache cache.Cacher[ids.ID, int64]) {
require := require.New(t)

id1 := ids.ID{1}
Expand Down Expand Up @@ -63,7 +62,7 @@ func TestBasic(t *testing.T, cache Cacher[ids.ID, int64]) {
require.Equal(expectedValue2, value)
}

func TestEviction(t *testing.T, cache Cacher[ids.ID, int64]) {
func TestEviction(t *testing.T, cache cache.Cacher[ids.ID, int64]) {
require := require.New(t)

id1 := ids.ID{1}
Expand Down
9 changes: 6 additions & 3 deletions cache/lru_cache_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package cache
package cache_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/cache/cachetest"
"github.com/ava-labs/avalanchego/ids"

. "github.com/ava-labs/avalanchego/cache"
)

func TestLRU(t *testing.T) {
cache := &LRU[ids.ID, int64]{Size: 1}

TestBasic(t, cache)
cachetest.TestBasic(t, cache)
}

func TestLRUEviction(t *testing.T) {
cache := &LRU[ids.ID, int64]{Size: 2}

TestEviction(t, cache)
cachetest.TestEviction(t, cache)
}

func TestLRUResize(t *testing.T) {
Expand Down
13 changes: 8 additions & 5 deletions cache/lru_sized_cache_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package cache
package cache_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/cache/cachetest"
"github.com/ava-labs/avalanchego/ids"

. "github.com/ava-labs/avalanchego/cache"
)

func TestSizedLRU(t *testing.T) {
cache := NewSizedLRU[ids.ID, int64](TestIntSize, TestIntSizeFunc)
cache := NewSizedLRU[ids.ID, int64](cachetest.TestIntSize, cachetest.TestIntSizeFunc)

TestBasic(t, cache)
cachetest.TestBasic(t, cache)
}

func TestSizedLRUEviction(t *testing.T) {
cache := NewSizedLRU[ids.ID, int64](2*TestIntSize, TestIntSizeFunc)
cache := NewSizedLRU[ids.ID, int64](2*cachetest.TestIntSize, cachetest.TestIntSizeFunc)

TestEviction(t, cache)
cachetest.TestEviction(t, cache)
}

func TestSizedLRUWrongKeyEvictionRegression(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions cache/metercacher/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/cache/cachetest"
"github.com/ava-labs/avalanchego/ids"
)

Expand All @@ -29,13 +30,13 @@ func TestInterface(t *testing.T) {
{
description: "sized cache LRU",
setup: func(size int) cache.Cacher[ids.ID, int64] {
return cache.NewSizedLRU[ids.ID, int64](size*cache.TestIntSize, cache.TestIntSizeFunc)
return cache.NewSizedLRU[ids.ID, int64](size*cachetest.TestIntSize, cachetest.TestIntSizeFunc)
},
},
}

for _, scenario := range scenarios {
for _, test := range cache.CacherTests {
for _, test := range cachetest.CacherTests {
baseCache := scenario.setup(test.Size)
c, err := New("", prometheus.NewRegistry(), baseCache)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit c2f1f46

Please sign in to comment.