github.com/pingcap/tidb-lightning@v5.0.0-rc.0.20210428090220-84b649866577+incompatible/lightning/verification/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 verification_test 15 16 import ( 17 "encoding/json" 18 "testing" 19 20 . "github.com/pingcap/check" 21 22 "github.com/pingcap/tidb-lightning/lightning/common" 23 "github.com/pingcap/tidb-lightning/lightning/verification" 24 ) 25 26 type testKVChcksumSuite struct{} 27 28 func (s *testKVChcksumSuite) SetUpSuite(c *C) {} 29 func (s *testKVChcksumSuite) TearDownSuite(c *C) {} 30 31 var _ = Suite(&testKVChcksumSuite{}) 32 33 func TestKVChcksum(t *testing.T) { 34 TestingT(t) 35 } 36 37 func uint64NotEqual(a uint64, b uint64) bool { return a != b } 38 39 func (s *testKVChcksumSuite) TestChcksum(c *C) { 40 checksum := verification.NewKVChecksum(0) 41 c.Assert(checksum.Sum(), Equals, uint64(0)) 42 43 // checksum on nothing 44 checksum.Update([]common.KvPair{}) 45 c.Assert(checksum.Sum(), Equals, uint64(0)) 46 47 checksum.Update(nil) 48 c.Assert(checksum.Sum(), Equals, uint64(0)) 49 50 // checksum on real data 51 excpectChecksum := uint64(4850203904608948940) 52 53 kvs := []common.KvPair{ 54 { 55 Key: []byte("Cop"), 56 Val: []byte("PingCAP"), 57 }, 58 { 59 Key: []byte("Introduction"), 60 Val: []byte("Inspired by Google Spanner/F1, PingCAP develops TiDB."), 61 }, 62 } 63 64 checksum.Update(kvs) 65 66 var kvBytes uint64 67 for _, kv := range kvs { 68 kvBytes += uint64(len(kv.Key) + len(kv.Val)) 69 } 70 c.Assert(checksum.SumSize(), Equals, kvBytes) 71 c.Assert(checksum.SumKVS(), Equals, uint64(len(kvs))) 72 c.Assert(checksum.Sum(), Equals, excpectChecksum) 73 74 // recompute on same key-value 75 checksum.Update(kvs) 76 c.Assert(checksum.SumSize(), Equals, kvBytes<<1) 77 c.Assert(checksum.SumKVS(), Equals, uint64(len(kvs))<<1) 78 c.Assert(uint64NotEqual(checksum.Sum(), excpectChecksum), IsTrue) 79 } 80 81 func (s *testKVChcksumSuite) TestChecksumJSON(c *C) { 82 testStruct := &struct { 83 Checksum verification.KVChecksum 84 }{ 85 Checksum: verification.MakeKVChecksum(123, 456, 7890), 86 } 87 88 res, err := json.Marshal(testStruct) 89 90 c.Assert(err, IsNil) 91 c.Assert(res, BytesEquals, []byte(`{"Checksum":{"checksum":7890,"size":123,"kvs":456}}`)) 92 }