github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/allegrosql/memdb_norace_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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  //go:build !race
    15  // +build !race
    16  
    17  package ekv
    18  
    19  import (
    20  	"encoding/binary"
    21  	"math/rand"
    22  
    23  	. "github.com/whtcorpsinc/check"
    24  	"github.com/whtcorpsinc/goleveldb/leveldb/comparer"
    25  	leveldb "github.com/whtcorpsinc/goleveldb/leveldb/memdb"
    26  )
    27  
    28  // The test takes too long under the race detector.
    29  func (s testMemDBSuite) TestRandom(c *C) {
    30  	c.Parallel()
    31  	const cnt = 500000
    32  	keys := make([][]byte, cnt)
    33  	for i := range keys {
    34  		keys[i] = make([]byte, rand.Intn(19)+1)
    35  		rand.Read(keys[i])
    36  	}
    37  
    38  	p1 := newMemDB()
    39  	p2 := leveldb.New(comparer.DefaultComparer, 4*1024)
    40  	for _, k := range keys {
    41  		p1.Set(k, k)
    42  		_ = p2.Put(k, k)
    43  	}
    44  
    45  	c.Check(p1.Len(), Equals, p2.Len())
    46  	c.Check(p1.Size(), Equals, p2.Size())
    47  
    48  	rand.Shuffle(cnt, func(i, j int) { keys[i], keys[j] = keys[j], keys[i] })
    49  
    50  	for _, k := range keys {
    51  		op := rand.Float64()
    52  		if op < 0.35 {
    53  			p1.DeleteKey(k)
    54  			p2.Delete(k)
    55  		} else {
    56  			newValue := make([]byte, rand.Intn(19)+1)
    57  			rand.Read(newValue)
    58  			p1.Set(k, newValue)
    59  			_ = p2.Put(k, newValue)
    60  		}
    61  	}
    62  	s.checkConsist(c, p1, p2)
    63  }
    64  
    65  // The test takes too long under the race detector.
    66  func (s testMemDBSuite) TestRandomDerive(c *C) {
    67  	c.Parallel()
    68  	EDB := newMemDB()
    69  	golden := leveldb.New(comparer.DefaultComparer, 4*1024)
    70  	s.testRandomDeriveRecur(c, EDB, golden, 0)
    71  }
    72  
    73  func (s testMemDBSuite) testRandomDeriveRecur(c *C, EDB *memdb, golden *leveldb.EDB, depth int) [][2][]byte {
    74  	var keys [][]byte
    75  	if op := rand.Float64(); op < 0.33 {
    76  		start, end := rand.Intn(512), rand.Intn(512)+512
    77  		cnt := end - start
    78  		keys = make([][]byte, cnt)
    79  		for i := range keys {
    80  			keys[i] = make([]byte, 8)
    81  			binary.BigEndian.PutUint64(keys[i], uint64(start+i))
    82  		}
    83  	} else if op < 0.66 {
    84  		keys = make([][]byte, rand.Intn(512)+512)
    85  		for i := range keys {
    86  			keys[i] = make([]byte, rand.Intn(19)+1)
    87  			rand.Read(keys[i])
    88  		}
    89  	} else {
    90  		keys = make([][]byte, 512)
    91  		for i := range keys {
    92  			keys[i] = make([]byte, 8)
    93  			binary.BigEndian.PutUint64(keys[i], uint64(i))
    94  		}
    95  	}
    96  
    97  	vals := make([][]byte, len(keys))
    98  	for i := range vals {
    99  		vals[i] = make([]byte, rand.Intn(255)+1)
   100  		rand.Read(vals[i])
   101  	}
   102  
   103  	h := EDB.Staging()
   104  	opLog := make([][2][]byte, 0, len(keys))
   105  	for i := range keys {
   106  		EDB.Set(keys[i], vals[i])
   107  		old, err := golden.Get(keys[i])
   108  		if err != nil {
   109  			opLog = append(opLog, [2][]byte{keys[i], nil})
   110  		} else {
   111  			opLog = append(opLog, [2][]byte{keys[i], old})
   112  		}
   113  		golden.Put(keys[i], vals[i])
   114  	}
   115  
   116  	if depth < 2000 {
   117  		childOps := s.testRandomDeriveRecur(c, EDB, golden, depth+1)
   118  		opLog = append(opLog, childOps...)
   119  	}
   120  
   121  	if rand.Float64() < 0.3 && depth > 0 {
   122  		EDB.Cleanup(h)
   123  		for i := len(opLog) - 1; i >= 0; i-- {
   124  			if opLog[i][1] == nil {
   125  				golden.Delete(opLog[i][0])
   126  			} else {
   127  				golden.Put(opLog[i][0], opLog[i][1])
   128  			}
   129  		}
   130  		opLog = nil
   131  	} else {
   132  		EDB.Release(h)
   133  	}
   134  
   135  	if depth%200 == 0 {
   136  		s.checkConsist(c, EDB, golden)
   137  	}
   138  
   139  	return opLog
   140  }