github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/kv/checksum_test.go (about)

     1  // Copyright 2019 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package kv_test
    15  
    16  import (
    17  	"encoding/json"
    18  	"testing"
    19  
    20  	. "github.com/pingcap/check"
    21  
    22  	"github.com/pingcap/br/pkg/kv"
    23  )
    24  
    25  type testKVChecksumSuite struct{}
    26  
    27  func (s *testKVChecksumSuite) SetUpSuite(c *C)    {}
    28  func (s *testKVChecksumSuite) TearDownSuite(c *C) {}
    29  
    30  var _ = Suite(&testKVChecksumSuite{})
    31  
    32  func TestKVChecksum(t *testing.T) {
    33  	TestingT(t)
    34  }
    35  
    36  func uint64NotEqual(a uint64, b uint64) bool { return a != b }
    37  
    38  func (s *testKVChecksumSuite) TestChecksum(c *C) {
    39  	checksum := kv.NewKVChecksum(0)
    40  	c.Assert(checksum.Sum(), Equals, uint64(0))
    41  
    42  	// checksum on nothing
    43  	checksum.Update([]kv.Pair{})
    44  	c.Assert(checksum.Sum(), Equals, uint64(0))
    45  
    46  	checksum.Update(nil)
    47  	c.Assert(checksum.Sum(), Equals, uint64(0))
    48  
    49  	// checksum on real data
    50  	expectChecksum := uint64(4850203904608948940)
    51  
    52  	kvs := []kv.Pair{
    53  		{
    54  			Key: []byte("Cop"),
    55  			Val: []byte("PingCAP"),
    56  		},
    57  		{
    58  			Key: []byte("Introduction"),
    59  			Val: []byte("Inspired by Google Spanner/F1, PingCAP develops TiDB."),
    60  		},
    61  	}
    62  
    63  	checksum.Update(kvs)
    64  
    65  	var kvBytes uint64
    66  	for _, kv := range kvs {
    67  		kvBytes += uint64(len(kv.Key) + len(kv.Val))
    68  	}
    69  	c.Assert(checksum.SumSize(), Equals, kvBytes)
    70  	c.Assert(checksum.SumKVS(), Equals, uint64(len(kvs)))
    71  	c.Assert(checksum.Sum(), Equals, expectChecksum)
    72  
    73  	// recompute on same key-value
    74  	checksum.Update(kvs)
    75  	c.Assert(checksum.SumSize(), Equals, kvBytes<<1)
    76  	c.Assert(checksum.SumKVS(), Equals, uint64(len(kvs))<<1)
    77  	c.Assert(uint64NotEqual(checksum.Sum(), expectChecksum), IsTrue)
    78  }
    79  
    80  func (s *testKVChecksumSuite) TestChecksumJSON(c *C) {
    81  	testStruct := &struct {
    82  		Checksum kv.Checksum
    83  	}{
    84  		Checksum: kv.MakeKVChecksum(123, 456, 7890),
    85  	}
    86  
    87  	res, err := json.Marshal(testStruct)
    88  
    89  	c.Assert(err, IsNil)
    90  	c.Assert(res, BytesEquals, []byte(`{"Checksum":{"checksum":7890,"size":123,"kvs":456}}`))
    91  }