Skip to content

Commit

Permalink
util/resourcegrouptag: migrate test-infra to testify (#26504)
Browse files Browse the repository at this point in the history
  • Loading branch information
tisonkun committed Jul 27, 2021
1 parent 57d704b commit b073d6b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 44 deletions.
26 changes: 26 additions & 0 deletions util/resourcegrouptag/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package resourcegrouptag

import (
"testing"

"github.com/pingcap/tidb/util/testbridge"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
goleak.VerifyTestMain(m)
}
88 changes: 44 additions & 44 deletions util/resourcegrouptag/resource_group_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,48 @@ import (
"math/rand"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/parser"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tipb/go-tipb"
"github.com/stretchr/testify/require"
)

type testUtilsSuite struct{}
func TestResourceGroupTagEncoding(t *testing.T) {
t.Parallel()

var _ = Suite(&testUtilsSuite{})

func TestT(t *testing.T) {
TestingT(t)
}

func (s *testUtilsSuite) TestResourceGroupTagEncoding(c *C) {
sqlDigest := parser.NewDigest(nil)
tag := EncodeResourceGroupTag(sqlDigest, nil)
c.Assert(len(tag), Equals, 0)
require.Len(t, tag, 0)

decodedSQLDigest, err := DecodeResourceGroupTag(tag)
c.Assert(err, IsNil)
c.Assert(len(decodedSQLDigest), Equals, 0)
require.NoError(t, err)
require.Len(t, decodedSQLDigest, 0)

sqlDigest = parser.NewDigest([]byte{'a', 'a'})
tag = EncodeResourceGroupTag(sqlDigest, nil)
// version(1) + prefix(1) + length(1) + content(2hex -> 1byte)
c.Assert(len(tag), Equals, 4)
require.Len(t, tag, 4)

decodedSQLDigest, err = DecodeResourceGroupTag(tag)
c.Assert(err, IsNil)
c.Assert(decodedSQLDigest, DeepEquals, sqlDigest.Bytes())
require.NoError(t, err)
require.Equal(t, sqlDigest.Bytes(), decodedSQLDigest)

sqlDigest = parser.NewDigest(genRandHex(64))
tag = EncodeResourceGroupTag(sqlDigest, nil)
decodedSQLDigest, err = DecodeResourceGroupTag(tag)
c.Assert(err, IsNil)
c.Assert(decodedSQLDigest, DeepEquals, sqlDigest.Bytes())
require.NoError(t, err)
require.Equal(t, sqlDigest.Bytes(), decodedSQLDigest)

sqlDigest = parser.NewDigest(genRandHex(510))
tag = EncodeResourceGroupTag(sqlDigest, nil)
decodedSQLDigest, err = DecodeResourceGroupTag(tag)
c.Assert(err, IsNil)
c.Assert(decodedSQLDigest, DeepEquals, sqlDigest.Bytes())
}

func genRandHex(length int) []byte {
const chars = "0123456789abcdef"
res := make([]byte, length)
for i := 0; i < length; i++ {
res[i] = chars[rand.Intn(len(chars))]
}
return res
require.NoError(t, err)
require.Equal(t, sqlDigest.Bytes(), decodedSQLDigest)
}

func genDigest(str string) []byte {
hasher := sha256.New()
hasher.Write(hack.Slice(str))
return hasher.Sum(nil)
}
func TestResourceGroupTagEncodingPB(t *testing.T) {
t.Parallel()

func (s *testUtilsSuite) TestResourceGroupTagEncodingPB(c *C) {
digest1 := genDigest("abc")
digest2 := genDigest("abcdefg")
// Test for protobuf
Expand All @@ -85,24 +68,41 @@ func (s *testUtilsSuite) TestResourceGroupTagEncodingPB(c *C) {
PlanDigest: digest2,
}
buf, err := resourceTag.Marshal()
c.Assert(err, IsNil)
c.Assert(len(buf), Equals, 68)
require.NoError(t, err)
require.Len(t, buf, 68)

tag := &tipb.ResourceGroupTag{}
err = tag.Unmarshal(buf)
c.Assert(err, IsNil)
c.Assert(tag.SqlDigest, DeepEquals, digest1)
c.Assert(tag.PlanDigest, DeepEquals, digest2)
require.NoError(t, err)
require.Equal(t, digest1, tag.SqlDigest)
require.Equal(t, digest2, tag.PlanDigest)

// Test for protobuf sql_digest only
resourceTag = &tipb.ResourceGroupTag{
SqlDigest: digest1,
}
buf, err = resourceTag.Marshal()
c.Assert(err, IsNil)
c.Assert(len(buf), Equals, 34)
require.NoError(t, err)
require.Len(t, buf, 34)

tag = &tipb.ResourceGroupTag{}
err = tag.Unmarshal(buf)
c.Assert(err, IsNil)
c.Assert(tag.SqlDigest, DeepEquals, digest1)
c.Assert(tag.PlanDigest, IsNil)
require.NoError(t, err)
require.Equal(t, digest1, tag.SqlDigest)
require.Nil(t, tag.PlanDigest)
}

func genRandHex(length int) []byte {
const chars = "0123456789abcdef"
res := make([]byte, length)
for i := 0; i < length; i++ {
res[i] = chars[rand.Intn(len(chars))]
}
return res
}

func genDigest(str string) []byte {
hasher := sha256.New()
hasher.Write(hack.Slice(str))
return hasher.Sum(nil)
}

0 comments on commit b073d6b

Please sign in to comment.