Skip to content

Commit

Permalink
*: use require.Equal to replace reflect.DeepEqual (tikv#5109)
Browse files Browse the repository at this point in the history
close tikv#5104

Use require.Equal to replace reflect.DeepEqual.

Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato authored and CabinfeverB committed Jul 14, 2022
1 parent baf8335 commit d753d27
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 59 deletions.
7 changes: 3 additions & 4 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package pd

import (
"context"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -58,11 +57,11 @@ func TestUpdateURLs(t *testing.T) {
cli := &baseClient{option: newOption()}
cli.urls.Store([]string{})
cli.updateURLs(members[1:])
re.True(reflect.DeepEqual(getURLs([]*pdpb.Member{members[1], members[3], members[2]}), cli.GetURLs()))
re.Equal(getURLs([]*pdpb.Member{members[1], members[3], members[2]}), cli.GetURLs())
cli.updateURLs(members[1:])
re.True(reflect.DeepEqual(getURLs([]*pdpb.Member{members[1], members[3], members[2]}), cli.GetURLs()))
re.Equal(getURLs([]*pdpb.Member{members[1], members[3], members[2]}), cli.GetURLs())
cli.updateURLs(members)
re.True(reflect.DeepEqual(getURLs([]*pdpb.Member{members[1], members[3], members[2], members[0]}), cli.GetURLs()))
re.Equal(getURLs([]*pdpb.Member{members[1], members[3], members[2], members[0]}), cli.GetURLs())
}

const testClientURL = "tmp://test.url:5255"
Expand Down
17 changes: 8 additions & 9 deletions pkg/autoscaling/calculation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"fmt"
"math"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -70,7 +69,7 @@ func TestGetScaledTiKVGroups(t *testing.T) {
informer core.StoreSetInformer
healthyInstances []instance
expectedPlan []*Plan
noError bool
errorChecker func(err error, msgAndArgs ...interface{})
}{
{
name: "no scaled tikv group",
Expand All @@ -90,7 +89,7 @@ func TestGetScaledTiKVGroups(t *testing.T) {
},
},
expectedPlan: nil,
noError: true,
errorChecker: re.NoError,
},
{
name: "exist 1 scaled tikv group",
Expand Down Expand Up @@ -120,7 +119,7 @@ func TestGetScaledTiKVGroups(t *testing.T) {
},
},
},
noError: true,
errorChecker: re.NoError,
},
{
name: "exist 1 tikv scaled group with inconsistency healthy instances",
Expand All @@ -140,7 +139,7 @@ func TestGetScaledTiKVGroups(t *testing.T) {
},
},
expectedPlan: nil,
noError: false,
errorChecker: re.Error,
},
{
name: "exist 1 tikv scaled group with less healthy instances",
Expand All @@ -166,7 +165,7 @@ func TestGetScaledTiKVGroups(t *testing.T) {
},
},
},
noError: true,
errorChecker: re.NoError,
},
{
name: "existed other tikv group",
Expand All @@ -186,7 +185,7 @@ func TestGetScaledTiKVGroups(t *testing.T) {
},
},
expectedPlan: nil,
noError: true,
errorChecker: re.NoError,
},
}

Expand All @@ -195,9 +194,9 @@ func TestGetScaledTiKVGroups(t *testing.T) {
plans, err := getScaledTiKVGroups(testCase.informer, testCase.healthyInstances)
if testCase.expectedPlan == nil {
re.Len(plans, 0)
re.Equal(testCase.noError, err == nil)
testCase.errorChecker(err)
} else {
re.True(reflect.DeepEqual(testCase.expectedPlan, plans))
re.Equal(testCase.expectedPlan, plans)
}
}
}
Expand Down
55 changes: 27 additions & 28 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cache

import (
"context"
"reflect"
"sort"
"testing"
"time"
Expand Down Expand Up @@ -76,7 +75,7 @@ func TestExpireRegionCache(t *testing.T) {

re.Equal(3, cache.Len())

re.True(reflect.DeepEqual(sortIDs(cache.GetAllID()), []uint64{1, 2, 3}))
re.Equal(sortIDs(cache.GetAllID()), []uint64{1, 2, 3})

time.Sleep(2 * time.Second)

Expand All @@ -93,7 +92,7 @@ func TestExpireRegionCache(t *testing.T) {
re.Equal(3.0, value)

re.Equal(2, cache.Len())
re.True(reflect.DeepEqual(sortIDs(cache.GetAllID()), []uint64{2, 3}))
re.Equal(sortIDs(cache.GetAllID()), []uint64{2, 3})

cache.Remove(2)

Expand All @@ -106,7 +105,7 @@ func TestExpireRegionCache(t *testing.T) {
re.Equal(3.0, value)

re.Equal(1, cache.Len())
re.True(reflect.DeepEqual(sortIDs(cache.GetAllID()), []uint64{3}))
re.Equal(sortIDs(cache.GetAllID()), []uint64{3})
}

func sortIDs(ids []uint64) []uint64 {
Expand All @@ -125,15 +124,15 @@ func TestLRUCache(t *testing.T) {

val, ok := cache.Get(3)
re.True(ok)
re.True(reflect.DeepEqual(val, "3"))
re.Equal(val, "3")

val, ok = cache.Get(2)
re.True(ok)
re.True(reflect.DeepEqual(val, "2"))
re.Equal(val, "2")

val, ok = cache.Get(1)
re.True(ok)
re.True(reflect.DeepEqual(val, "1"))
re.Equal(val, "1")

re.Equal(3, cache.Len())

Expand All @@ -147,27 +146,27 @@ func TestLRUCache(t *testing.T) {

val, ok = cache.Get(1)
re.True(ok)
re.True(reflect.DeepEqual(val, "1"))
re.Equal(val, "1")

val, ok = cache.Get(2)
re.True(ok)
re.True(reflect.DeepEqual(val, "2"))
re.Equal(val, "2")

val, ok = cache.Get(4)
re.True(ok)
re.True(reflect.DeepEqual(val, "4"))
re.Equal(val, "4")

re.Equal(3, cache.Len())

val, ok = cache.Peek(1)
re.True(ok)
re.True(reflect.DeepEqual(val, "1"))
re.Equal(val, "1")

elems := cache.Elems()
re.Len(elems, 3)
re.True(reflect.DeepEqual(elems[0].Value, "4"))
re.True(reflect.DeepEqual(elems[1].Value, "2"))
re.True(reflect.DeepEqual(elems[2].Value, "1"))
re.Equal(elems[0].Value, "4")
re.Equal(elems[1].Value, "2")
re.Equal(elems[2].Value, "1")

cache.Remove(1)
cache.Remove(2)
Expand Down Expand Up @@ -205,13 +204,13 @@ func TestFifoCache(t *testing.T) {

elems := cache.Elems()
re.Len(elems, 3)
re.True(reflect.DeepEqual(elems[0].Value, "2"))
re.True(reflect.DeepEqual(elems[1].Value, "3"))
re.True(reflect.DeepEqual(elems[2].Value, "4"))
re.Equal(elems[0].Value, "2")
re.Equal(elems[1].Value, "3")
re.Equal(elems[2].Value, "4")

elems = cache.FromElems(3)
re.Len(elems, 1)
re.True(reflect.DeepEqual(elems[0].Value, "4"))
re.Equal(elems[0].Value, "4")

cache.Remove()
cache.Remove()
Expand All @@ -228,15 +227,15 @@ func TestTwoQueueCache(t *testing.T) {

val, ok := cache.Get(3)
re.True(ok)
re.True(reflect.DeepEqual(val, "3"))
re.Equal(val, "3")

val, ok = cache.Get(2)
re.True(ok)
re.True(reflect.DeepEqual(val, "2"))
re.Equal(val, "2")

val, ok = cache.Get(1)
re.True(ok)
re.True(reflect.DeepEqual(val, "1"))
re.Equal(val, "1")

re.Equal(3, cache.Len())

Expand All @@ -250,27 +249,27 @@ func TestTwoQueueCache(t *testing.T) {

val, ok = cache.Get(1)
re.True(ok)
re.True(reflect.DeepEqual(val, "1"))
re.Equal(val, "1")

val, ok = cache.Get(2)
re.True(ok)
re.True(reflect.DeepEqual(val, "2"))
re.Equal(val, "2")

val, ok = cache.Get(4)
re.True(ok)
re.True(reflect.DeepEqual(val, "4"))
re.Equal(val, "4")

re.Equal(3, cache.Len())

val, ok = cache.Peek(1)
re.True(ok)
re.True(reflect.DeepEqual(val, "1"))
re.Equal(val, "1")

elems := cache.Elems()
re.Len(elems, 3)
re.True(reflect.DeepEqual(elems[0].Value, "4"))
re.True(reflect.DeepEqual(elems[1].Value, "2"))
re.True(reflect.DeepEqual(elems[2].Value, "1"))
re.Equal(elems[0].Value, "4")
re.Equal(elems[1].Value, "2")
re.Equal(elems[2].Value, "1")

cache.Remove(1)
cache.Remove(2)
Expand Down
7 changes: 3 additions & 4 deletions pkg/logutil/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package logutil

import (
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -73,11 +72,11 @@ func TestRedactLog(t *testing.T) {
SetRedactLog(testCase.enableRedactLog)
switch r := testCase.arg.(type) {
case []byte:
re.True(reflect.DeepEqual(testCase.expect, RedactBytes(r)))
re.Equal(testCase.expect, RedactBytes(r))
case string:
re.True(reflect.DeepEqual(testCase.expect, RedactString(r)))
re.Equal(testCase.expect, RedactString(r))
case fmt.Stringer:
re.True(reflect.DeepEqual(testCase.expect, RedactStringer(r)))
re.Equal(testCase.expect, RedactStringer(r))
default:
panic("unmatched case")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/typeutil/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestUint64ToBytes(t *testing.T) {
var a uint64 = 1000
b := Uint64ToBytes(a)
str := "\x00\x00\x00\x00\x00\x00\x03\xe8"
re.True(reflect.DeepEqual([]byte(str), b))
re.Equal([]byte(str), b)
}

func TestJSONToUint64Slice(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/typeutil/string_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package typeutil

import (
"encoding/json"
"reflect"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -32,7 +31,7 @@ func TestStringSliceJSON(t *testing.T) {
var nb StringSlice
err = json.Unmarshal(o, &nb)
re.NoError(err)
re.True(reflect.DeepEqual(b, nb))
re.Equal(b, nb)
}

func TestEmpty(t *testing.T) {
Expand All @@ -44,5 +43,5 @@ func TestEmpty(t *testing.T) {

var ss2 StringSlice
re.NoError(ss2.UnmarshalJSON(b))
re.True(reflect.DeepEqual(ss, ss2))
re.Equal(ss, ss2)
}
20 changes: 10 additions & 10 deletions tests/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestClientLeaderChange(t *testing.T) {
urls := cli.(client).GetURLs()
sort.Strings(urls)
sort.Strings(endpoints)
re.True(reflect.DeepEqual(endpoints, urls))
re.Equal(endpoints, urls)
}

func TestLeaderTransfer(t *testing.T) {
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestTSOAllocatorLeader(t *testing.T) {
urls := cli.(client).GetURLs()
sort.Strings(urls)
sort.Strings(endpoints)
re.True(reflect.DeepEqual(endpoints, urls))
re.Equal(endpoints, urls)
continue
}
pdName, exist := allocatorLeaderMap[dcLocation]
Expand Down Expand Up @@ -974,20 +974,20 @@ func (suite *clientTestSuite) TestScanRegions() {
t.Log("scanRegions", scanRegions)
t.Log("expect", expect)
for i := range expect {
suite.True(reflect.DeepEqual(expect[i], scanRegions[i].Meta))
suite.Equal(expect[i], scanRegions[i].Meta)

if scanRegions[i].Meta.GetId() == region3.GetID() {
suite.True(reflect.DeepEqual(&metapb.Peer{}, scanRegions[i].Leader))
suite.Equal(&metapb.Peer{}, scanRegions[i].Leader)
} else {
suite.True(reflect.DeepEqual(expect[i].Peers[0], scanRegions[i].Leader))
suite.Equal(expect[i].Peers[0], scanRegions[i].Leader)
}

if scanRegions[i].Meta.GetId() == region4.GetID() {
suite.True(reflect.DeepEqual([]*metapb.Peer{expect[i].Peers[1]}, scanRegions[i].DownPeers))
suite.Equal([]*metapb.Peer{expect[i].Peers[1]}, scanRegions[i].DownPeers)
}

if scanRegions[i].Meta.GetId() == region5.GetID() {
suite.True(reflect.DeepEqual([]*metapb.Peer{expect[i].Peers[1], expect[i].Peers[2]}, scanRegions[i].PendingPeers))
suite.Equal([]*metapb.Peer{expect[i].Peers[1], expect[i].Peers[2]}, scanRegions[i].PendingPeers)
}
}
}
Expand Down Expand Up @@ -1036,7 +1036,7 @@ func (suite *clientTestSuite) TestGetStore() {
// Get an up store should be OK.
n, err := suite.client.GetStore(context.Background(), store.GetId())
suite.NoError(err)
suite.True(reflect.DeepEqual(store, n))
suite.Equal(store, n)

actualStores, err := suite.client.GetAllStores(context.Background())
suite.NoError(err)
Expand All @@ -1053,7 +1053,7 @@ func (suite *clientTestSuite) TestGetStore() {
// Get an offline store should be OK.
n, err = suite.client.GetStore(context.Background(), store.GetId())
suite.NoError(err)
suite.True(reflect.DeepEqual(offlineStore, n))
suite.Equal(offlineStore, n)

// Should return offline stores.
contains := false
Expand All @@ -1062,7 +1062,7 @@ func (suite *clientTestSuite) TestGetStore() {
for _, store := range stores {
if store.GetId() == offlineStore.GetId() {
contains = true
suite.True(reflect.DeepEqual(offlineStore, store))
suite.Equal(offlineStore, store)
}
}
suite.True(contains)
Expand Down

0 comments on commit d753d27

Please sign in to comment.