Skip to content

Commit

Permalink
server/cluster: migrate test framework to testify (#5203)
Browse files Browse the repository at this point in the history
ref #4813

As the title says.

Signed-off-by: LLThomas <[email protected]>
  • Loading branch information
LLThomas authored Jun 23, 2022
1 parent bbe48e4 commit e4ca2e6
Show file tree
Hide file tree
Showing 6 changed files with 1,409 additions and 1,315 deletions.
62 changes: 32 additions & 30 deletions server/cluster/cluster_stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ package cluster

import (
"fmt"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/stretchr/testify/require"
)

var _ = Suite(&testClusterStatSuite{})

type testClusterStatSuite struct {
}

func cpu(usage int64) []*pdpb.RecordPair {
n := 10
name := "cpu"
Expand All @@ -39,30 +35,32 @@ func cpu(usage int64) []*pdpb.RecordPair {
return pairs
}

func (s *testClusterStatSuite) TestCPUEntriesAppend(c *C) {
func TestCPUEntriesAppend(t *testing.T) {
re := require.New(t)
N := 10

checkAppend := func(appended bool, usage int64, threads ...string) {
entries := NewCPUEntries(N)
c.Assert(entries, NotNil)
re.NotNil(entries)
for i := 0; i < N; i++ {
entry := &StatEntry{
CpuUsages: cpu(usage),
}
c.Assert(entries.Append(entry, threads...), Equals, appended)
re.Equal(appended, entries.Append(entry, threads...))
}
c.Assert(entries.cpu.Get(), Equals, float64(usage))
re.Equal(float64(usage), entries.cpu.Get())
}

checkAppend(true, 20)
checkAppend(true, 20, "cpu")
checkAppend(false, 0, "cup")
}

func (s *testClusterStatSuite) TestCPUEntriesCPU(c *C) {
func TestCPUEntriesCPU(t *testing.T) {
re := require.New(t)
N := 10
entries := NewCPUEntries(N)
c.Assert(entries, NotNil)
re.NotNil(entries)

usages := cpu(20)
for i := 0; i < N; i++ {
Expand All @@ -71,13 +69,14 @@ func (s *testClusterStatSuite) TestCPUEntriesCPU(c *C) {
}
entries.Append(entry)
}
c.Assert(entries.CPU(), Equals, float64(20))
re.Equal(float64(20), entries.CPU())
}

func (s *testClusterStatSuite) TestStatEntriesAppend(c *C) {
func TestStatEntriesAppend(t *testing.T) {
re := require.New(t)
N := 10
cst := NewStatEntries(N)
c.Assert(cst, NotNil)
re.NotNil(cst)
ThreadsCollected = []string{"cpu:"}

// fill 2*N entries, 2 entries for each store
Expand All @@ -86,19 +85,20 @@ func (s *testClusterStatSuite) TestStatEntriesAppend(c *C) {
StoreId: uint64(i % N),
CpuUsages: cpu(20),
}
c.Assert(cst.Append(entry), IsTrue)
re.True(cst.Append(entry))
}

// use i as the store ID
for i := 0; i < N; i++ {
c.Assert(cst.stats[uint64(i)].CPU(), Equals, float64(20))
re.Equal(float64(20), cst.stats[uint64(i)].CPU())
}
}

func (s *testClusterStatSuite) TestStatEntriesCPU(c *C) {
func TestStatEntriesCPU(t *testing.T) {
re := require.New(t)
N := 10
cst := NewStatEntries(N)
c.Assert(cst, NotNil)
re.NotNil(cst)

// the average cpu usage is 20%
usages := cpu(20)
Expand All @@ -110,14 +110,15 @@ func (s *testClusterStatSuite) TestStatEntriesCPU(c *C) {
StoreId: uint64(i % N),
CpuUsages: usages,
}
c.Assert(cst.Append(entry), IsTrue)
re.True(cst.Append(entry))
}

c.Assert(cst.total, Equals, int64(2*N))
re.Equal(int64(2*N), cst.total)
// the cpu usage of the whole cluster is 20%
c.Assert(cst.CPU(), Equals, float64(20))
re.Equal(float64(20), cst.CPU())
}
func (s *testClusterStatSuite) TestStatEntriesCPUStale(c *C) {
func TestStatEntriesCPUStale(t *testing.T) {
re := require.New(t)
N := 10
cst := NewStatEntries(N)
// make all entries stale immediately
Expand All @@ -132,13 +133,14 @@ func (s *testClusterStatSuite) TestStatEntriesCPUStale(c *C) {
}
cst.Append(entry)
}
c.Assert(cst.CPU(), Equals, float64(0))
re.Equal(float64(0), cst.CPU())
}

func (s *testClusterStatSuite) TestStatEntriesState(c *C) {
func TestStatEntriesState(t *testing.T) {
re := require.New(t)
Load := func(usage int64) *State {
cst := NewStatEntries(10)
c.Assert(cst, NotNil)
re.NotNil(cst)

usages := cpu(usage)
ThreadsCollected = []string{"cpu:"}
Expand All @@ -152,8 +154,8 @@ func (s *testClusterStatSuite) TestStatEntriesState(c *C) {
}
return &State{cst}
}
c.Assert(Load(0).State(), Equals, LoadStateIdle)
c.Assert(Load(5).State(), Equals, LoadStateLow)
c.Assert(Load(10).State(), Equals, LoadStateNormal)
c.Assert(Load(30).State(), Equals, LoadStateHigh)
re.Equal(LoadStateIdle, Load(0).State())
re.Equal(LoadStateLow, Load(5).State())
re.Equal(LoadStateNormal, Load(10).State())
re.Equal(LoadStateHigh, Load(30).State())
}
Loading

0 comments on commit e4ca2e6

Please sign in to comment.