Skip to content

Commit

Permalink
*: make unit test great (tikv#7952)
Browse files Browse the repository at this point in the history
ref tikv#7969

Signed-off-by: husharp <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
HuSharp and ti-chi-bot[bot] authored Apr 10, 2024
1 parent b9240a0 commit f0eb74b
Show file tree
Hide file tree
Showing 48 changed files with 1,010 additions and 139 deletions.
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ BUILD_BIN_PATH := $(ROOT_PATH)/bin

build: pd-server pd-ctl pd-recover

tools: pd-tso-bench pd-heartbeat-bench regions-dump stores-dump pd-api-bench
tools: pd-tso-bench pd-heartbeat-bench regions-dump stores-dump pd-api-bench pd-ut

PD_SERVER_DEP :=
ifeq ($(SWAGGER), 1)
Expand Down Expand Up @@ -108,7 +108,6 @@ pd-server-basic:
.PHONY: pre-build build tools pd-server pd-server-basic

# Tools

pd-ctl:
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/pd-ctl pd-ctl/main.go
pd-tso-bench:
Expand All @@ -127,8 +126,12 @@ regions-dump:
cd tools && CGO_ENABLED=0 go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/regions-dump regions-dump/main.go
stores-dump:
cd tools && CGO_ENABLED=0 go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/stores-dump stores-dump/main.go
pd-ut: pd-xprog
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/pd-ut pd-ut/ut.go
pd-xprog:
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -tags xprog -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/xprog pd-ut/xprog.go

.PHONY: pd-ctl pd-tso-bench pd-recover pd-analysis pd-heartbeat-bench simulator regions-dump stores-dump pd-api-bench
.PHONY: pd-ctl pd-tso-bench pd-recover pd-analysis pd-heartbeat-bench simulator regions-dump stores-dump pd-api-bench pd-ut

#### Docker image ####

Expand Down Expand Up @@ -225,6 +228,12 @@ failpoint-disable: install-tools

#### Test ####

ut: pd-ut
@$(FAILPOINT_ENABLE)
./bin/pd-ut run --race
@$(CLEAN_UT_BINARY)
@$(FAILPOINT_DISABLE)

PACKAGE_DIRECTORIES := $(subst $(PD_PKG)/,,$(PACKAGES))
TEST_PKGS := $(filter $(shell find . -iname "*_test.go" -exec dirname {} \; | \
sort -u | sed -e "s/^\./github.com\/tikv\/pd/"),$(PACKAGES))
Expand Down Expand Up @@ -303,13 +312,16 @@ split:

clean: failpoint-disable clean-test clean-build

CLEAN_UT_BINARY := find . -name '*.test.bin'| xargs rm -f

clean-test:
# Cleaning test tmp...
rm -rf /tmp/test_pd*
rm -rf /tmp/pd-tests*
rm -rf /tmp/test_etcd*
rm -f $(REAL_CLUSTER_TEST_PATH)/playground.log
go clean -testcache
@$(CLEAN_UT_BINARY)

clean-build:
# Cleaning building files...
Expand Down
2 changes: 0 additions & 2 deletions client/resource_group/controller/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type example struct {
}

func TestDurationJSON(t *testing.T) {
t.Parallel()
re := require.New(t)
example := &example{}

Expand All @@ -41,7 +40,6 @@ func TestDurationJSON(t *testing.T) {
}

func TestDurationTOML(t *testing.T) {
t.Parallel()
re := require.New(t)
example := &example{}

Expand Down
3 changes: 0 additions & 3 deletions pkg/audit/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
)

func TestLabelMatcher(t *testing.T) {
t.Parallel()
re := require.New(t)
matcher := &LabelMatcher{"testSuccess"}
labels1 := &BackendLabels{Labels: []string{"testFail", "testSuccess"}}
Expand All @@ -42,7 +41,6 @@ func TestLabelMatcher(t *testing.T) {
}

func TestPrometheusHistogramBackend(t *testing.T) {
t.Parallel()
re := require.New(t)
serviceAuditHistogramTest := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Expand Down Expand Up @@ -90,7 +88,6 @@ func TestPrometheusHistogramBackend(t *testing.T) {
}

func TestLocalLogBackendUsingFile(t *testing.T) {
t.Parallel()
re := require.New(t)
backend := NewLocalLogBackend(true)
fname := testutil.InitTempFileLogger("info")
Expand Down
5 changes: 0 additions & 5 deletions pkg/autoscaling/calculation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
)

func TestGetScaledTiKVGroups(t *testing.T) {
t.Parallel()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -214,7 +213,6 @@ func (q *mockQuerier) Query(options *QueryOptions) (QueryResult, error) {
}

func TestGetTotalCPUUseTime(t *testing.T) {
t.Parallel()
re := require.New(t)
querier := &mockQuerier{}
instances := []instance{
Expand All @@ -237,7 +235,6 @@ func TestGetTotalCPUUseTime(t *testing.T) {
}

func TestGetTotalCPUQuota(t *testing.T) {
t.Parallel()
re := require.New(t)
querier := &mockQuerier{}
instances := []instance{
Expand All @@ -260,7 +257,6 @@ func TestGetTotalCPUQuota(t *testing.T) {
}

func TestScaleOutGroupLabel(t *testing.T) {
t.Parallel()
re := require.New(t)
var jsonStr = []byte(`
{
Expand Down Expand Up @@ -303,7 +299,6 @@ func TestScaleOutGroupLabel(t *testing.T) {
}

func TestStrategyChangeCount(t *testing.T) {
t.Parallel()
re := require.New(t)
var count uint64 = 2
strategy := &Strategy{
Expand Down
6 changes: 0 additions & 6 deletions pkg/autoscaling/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ func (c *normalClient) Do(_ context.Context, req *http.Request) (response *http.
}

func TestRetrieveCPUMetrics(t *testing.T) {
t.Parallel()
re := require.New(t)
client := &normalClient{
mockData: make(map[string]*response),
Expand Down Expand Up @@ -225,7 +224,6 @@ func (c *emptyResponseClient) Do(_ context.Context, req *http.Request) (r *http.
}

func TestEmptyResponse(t *testing.T) {
t.Parallel()
re := require.New(t)
client := &emptyResponseClient{}
querier := NewPrometheusQuerier(client)
Expand Down Expand Up @@ -253,7 +251,6 @@ func (c *errorHTTPStatusClient) Do(_ context.Context, req *http.Request) (r *htt
}

func TestErrorHTTPStatus(t *testing.T) {
t.Parallel()
re := require.New(t)
client := &errorHTTPStatusClient{}
querier := NewPrometheusQuerier(client)
Expand All @@ -279,7 +276,6 @@ func (c *errorPrometheusStatusClient) Do(_ context.Context, req *http.Request) (
}

func TestErrorPrometheusStatus(t *testing.T) {
t.Parallel()
re := require.New(t)
client := &errorPrometheusStatusClient{}
querier := NewPrometheusQuerier(client)
Expand All @@ -290,7 +286,6 @@ func TestErrorPrometheusStatus(t *testing.T) {
}

func TestGetInstanceNameFromAddress(t *testing.T) {
t.Parallel()
re := require.New(t)
testCases := []struct {
address string
Expand Down Expand Up @@ -328,7 +323,6 @@ func TestGetInstanceNameFromAddress(t *testing.T) {
}

func TestGetDurationExpression(t *testing.T) {
t.Parallel()
re := require.New(t)
testCases := []struct {
duration time.Duration
Expand Down
3 changes: 0 additions & 3 deletions pkg/balancer/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
)

func TestBalancerPutAndDelete(t *testing.T) {
t.Parallel()
re := require.New(t)
balancers := []Balancer[uint32]{
NewRoundRobin[uint32](),
Expand Down Expand Up @@ -56,7 +55,6 @@ func TestBalancerPutAndDelete(t *testing.T) {
}

func TestBalancerDuplicate(t *testing.T) {
t.Parallel()
re := require.New(t)
balancers := []Balancer[uint32]{
NewRoundRobin[uint32](),
Expand All @@ -77,7 +75,6 @@ func TestBalancerDuplicate(t *testing.T) {
}

func TestRoundRobin(t *testing.T) {
t.Parallel()
re := require.New(t)
balancer := NewRoundRobin[uint32]()
for i := 0; i < 100; i++ {
Expand Down
6 changes: 0 additions & 6 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

func TestExpireRegionCache(t *testing.T) {
t.Parallel()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -121,7 +120,6 @@ func sortIDs(ids []uint64) []uint64 {
}

func TestLRUCache(t *testing.T) {
t.Parallel()
re := require.New(t)
cache := newLRU(3)

Expand Down Expand Up @@ -199,7 +197,6 @@ func TestLRUCache(t *testing.T) {
}

func TestFifoCache(t *testing.T) {
t.Parallel()
re := require.New(t)
cache := NewFIFO(3)
cache.Put(1, "1")
Expand Down Expand Up @@ -227,7 +224,6 @@ func TestFifoCache(t *testing.T) {
}

func TestFifoFromLastSameElems(t *testing.T) {
t.Parallel()
re := require.New(t)
type testStruct struct {
value string
Expand Down Expand Up @@ -260,7 +256,6 @@ func TestFifoFromLastSameElems(t *testing.T) {
}

func TestTwoQueueCache(t *testing.T) {
t.Parallel()
re := require.New(t)
cache := newTwoQueue(3)
cache.Put(1, "1")
Expand Down Expand Up @@ -345,7 +340,6 @@ func (pq PriorityQueueItemTest) ID() uint64 {
}

func TestPriorityQueue(t *testing.T) {
t.Parallel()
re := require.New(t)
testData := []PriorityQueueItemTest{0, 1, 2, 3, 4, 5}
pq := NewPriorityQueue(0)
Expand Down
2 changes: 0 additions & 2 deletions pkg/codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
)

func TestDecodeBytes(t *testing.T) {
t.Parallel()
re := require.New(t)
key := "abcdefghijklmnopqrstuvwxyz"
for i := 0; i < len(key); i++ {
Expand All @@ -32,7 +31,6 @@ func TestDecodeBytes(t *testing.T) {
}

func TestTableID(t *testing.T) {
t.Parallel()
re := require.New(t)
key := EncodeBytes([]byte("t\x80\x00\x00\x00\x00\x00\x00\xff"))
re.Equal(int64(0xff), key.TableID())
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/rangetree/range_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func bucketDebrisFactory(startKey, endKey []byte, item RangeItem) []RangeItem {
}

func TestRingPutItem(t *testing.T) {
t.Parallel()
re := require.New(t)
bucketTree := NewRangeTree(2, bucketDebrisFactory)
bucketTree.Update(newSimpleBucketItem([]byte("002"), []byte("100")))
Expand Down Expand Up @@ -120,7 +119,6 @@ func TestRingPutItem(t *testing.T) {
}

func TestDebris(t *testing.T) {
t.Parallel()
re := require.New(t)
ringItem := newSimpleBucketItem([]byte("010"), []byte("090"))
var overlaps []RangeItem
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/storelimit/limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func TestStoreLimit(t *testing.T) {
}

func TestSlidingWindow(t *testing.T) {
t.Parallel()
re := require.New(t)
capacity := int64(defaultWindowSize)
s := NewSlidingWindows()
Expand Down Expand Up @@ -92,7 +91,6 @@ func TestSlidingWindow(t *testing.T) {
}

func TestWindow(t *testing.T) {
t.Parallel()
re := require.New(t)
capacity := int64(100 * 10)
s := newWindow(capacity)
Expand Down
4 changes: 0 additions & 4 deletions pkg/encryption/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
)

func TestAdjustDefaultValue(t *testing.T) {
t.Parallel()
re := require.New(t)
config := &Config{}
err := config.Adjust()
Expand All @@ -35,21 +34,18 @@ func TestAdjustDefaultValue(t *testing.T) {
}

func TestAdjustInvalidDataEncryptionMethod(t *testing.T) {
t.Parallel()
re := require.New(t)
config := &Config{DataEncryptionMethod: "unknown"}
re.Error(config.Adjust())
}

func TestAdjustNegativeRotationDuration(t *testing.T) {
t.Parallel()
re := require.New(t)
config := &Config{DataKeyRotationPeriod: typeutil.NewDuration(time.Duration(int64(-1)))}
re.Error(config.Adjust())
}

func TestAdjustInvalidMasterKeyType(t *testing.T) {
t.Parallel()
re := require.New(t)
config := &Config{MasterKey: MasterKeyConfig{Type: "unknown"}}
re.Error(config.Adjust())
Expand Down
5 changes: 0 additions & 5 deletions pkg/encryption/crypter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
)

func TestEncryptionMethodSupported(t *testing.T) {
t.Parallel()
re := require.New(t)
re.Error(CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_PLAINTEXT))
re.Error(CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_UNKNOWN))
Expand All @@ -34,7 +33,6 @@ func TestEncryptionMethodSupported(t *testing.T) {
}

func TestKeyLength(t *testing.T) {
t.Parallel()
re := require.New(t)
_, err := KeyLength(encryptionpb.EncryptionMethod_PLAINTEXT)
re.Error(err)
Expand All @@ -52,7 +50,6 @@ func TestKeyLength(t *testing.T) {
}

func TestNewIv(t *testing.T) {
t.Parallel()
re := require.New(t)
ivCtr, err := NewIvCTR()
re.NoError(err)
Expand All @@ -63,7 +60,6 @@ func TestNewIv(t *testing.T) {
}

func TestNewDataKey(t *testing.T) {
t.Parallel()
re := require.New(t)
for _, method := range []encryptionpb.EncryptionMethod{
encryptionpb.EncryptionMethod_AES128_CTR,
Expand All @@ -82,7 +78,6 @@ func TestNewDataKey(t *testing.T) {
}

func TestAesGcmCrypter(t *testing.T) {
t.Parallel()
re := require.New(t)
key, err := hex.DecodeString("ed568fbd8c8018ed2d042a4e5d38d6341486922d401d2022fb81e47c900d3f07")
re.NoError(err)
Expand Down
Loading

0 comments on commit f0eb74b

Please sign in to comment.