github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/ekv/union_store_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 package ekv 15 16 import ( 17 "context" 18 19 . "github.com/whtcorpsinc/check" 20 "github.com/whtcorpsinc/milevadb/soliton/testleak" 21 ) 22 23 var _ = Suite(&testUnionStoreSuite{}) 24 25 type testUnionStoreSuite struct { 26 causetstore MemBuffer 27 us UnionStore 28 } 29 30 func (s *testUnionStoreSuite) SetUpTest(c *C) { 31 s.causetstore = newMemDB() 32 s.us = NewUnionStore(&mockSnapshot{s.causetstore}) 33 } 34 35 func (s *testUnionStoreSuite) TestGetSet(c *C) { 36 defer testleak.AfterTest(c)() 37 err := s.causetstore.Set([]byte("1"), []byte("1")) 38 c.Assert(err, IsNil) 39 v, err := s.us.Get(context.TODO(), []byte("1")) 40 c.Assert(err, IsNil) 41 c.Assert(v, BytesEquals, []byte("1")) 42 err = s.us.GetMemBuffer().Set([]byte("1"), []byte("2")) 43 c.Assert(err, IsNil) 44 v, err = s.us.Get(context.TODO(), []byte("1")) 45 c.Assert(err, IsNil) 46 c.Assert(v, BytesEquals, []byte("2")) 47 c.Assert(s.us.GetMemBuffer().Size(), Equals, 2) 48 c.Assert(s.us.GetMemBuffer().Len(), Equals, 1) 49 } 50 51 func (s *testUnionStoreSuite) TestDelete(c *C) { 52 defer testleak.AfterTest(c)() 53 err := s.causetstore.Set([]byte("1"), []byte("1")) 54 c.Assert(err, IsNil) 55 err = s.us.GetMemBuffer().Delete([]byte("1")) 56 c.Assert(err, IsNil) 57 _, err = s.us.Get(context.TODO(), []byte("1")) 58 c.Assert(IsErrNotFound(err), IsTrue) 59 60 err = s.us.GetMemBuffer().Set([]byte("1"), []byte("2")) 61 c.Assert(err, IsNil) 62 v, err := s.us.Get(context.TODO(), []byte("1")) 63 c.Assert(err, IsNil) 64 c.Assert(v, BytesEquals, []byte("2")) 65 } 66 67 func (s *testUnionStoreSuite) TestSeek(c *C) { 68 defer testleak.AfterTest(c)() 69 err := s.causetstore.Set([]byte("1"), []byte("1")) 70 c.Assert(err, IsNil) 71 err = s.causetstore.Set([]byte("2"), []byte("2")) 72 c.Assert(err, IsNil) 73 err = s.causetstore.Set([]byte("3"), []byte("3")) 74 c.Assert(err, IsNil) 75 76 iter, err := s.us.Iter(nil, nil) 77 c.Assert(err, IsNil) 78 checkIterator(c, iter, [][]byte{[]byte("1"), []byte("2"), []byte("3")}, [][]byte{[]byte("1"), []byte("2"), []byte("3")}) 79 80 iter, err = s.us.Iter([]byte("2"), nil) 81 c.Assert(err, IsNil) 82 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("3")}, [][]byte{[]byte("2"), []byte("3")}) 83 84 err = s.us.GetMemBuffer().Set([]byte("4"), []byte("4")) 85 c.Assert(err, IsNil) 86 iter, err = s.us.Iter([]byte("2"), nil) 87 c.Assert(err, IsNil) 88 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("3"), []byte("4")}, [][]byte{[]byte("2"), []byte("3"), []byte("4")}) 89 90 err = s.us.GetMemBuffer().Delete([]byte("3")) 91 c.Assert(err, IsNil) 92 iter, err = s.us.Iter([]byte("2"), nil) 93 c.Assert(err, IsNil) 94 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("4")}, [][]byte{[]byte("2"), []byte("4")}) 95 } 96 97 func (s *testUnionStoreSuite) TestIterReverse(c *C) { 98 defer testleak.AfterTest(c)() 99 err := s.causetstore.Set([]byte("1"), []byte("1")) 100 c.Assert(err, IsNil) 101 err = s.causetstore.Set([]byte("2"), []byte("2")) 102 c.Assert(err, IsNil) 103 err = s.causetstore.Set([]byte("3"), []byte("3")) 104 c.Assert(err, IsNil) 105 106 iter, err := s.us.IterReverse(nil) 107 c.Assert(err, IsNil) 108 checkIterator(c, iter, [][]byte{[]byte("3"), []byte("2"), []byte("1")}, [][]byte{[]byte("3"), []byte("2"), []byte("1")}) 109 110 iter, err = s.us.IterReverse([]byte("3")) 111 c.Assert(err, IsNil) 112 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("1")}, [][]byte{[]byte("2"), []byte("1")}) 113 114 err = s.us.GetMemBuffer().Set([]byte("0"), []byte("0")) 115 c.Assert(err, IsNil) 116 iter, err = s.us.IterReverse([]byte("3")) 117 c.Assert(err, IsNil) 118 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("1"), []byte("0")}, [][]byte{[]byte("2"), []byte("1"), []byte("0")}) 119 120 err = s.us.GetMemBuffer().Delete([]byte("1")) 121 c.Assert(err, IsNil) 122 iter, err = s.us.IterReverse([]byte("3")) 123 c.Assert(err, IsNil) 124 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("0")}, [][]byte{[]byte("2"), []byte("0")}) 125 } 126 127 func checkIterator(c *C, iter Iterator, keys [][]byte, values [][]byte) { 128 defer iter.Close() 129 c.Assert(len(keys), Equals, len(values)) 130 for i, k := range keys { 131 v := values[i] 132 c.Assert(iter.Valid(), IsTrue) 133 c.Assert([]byte(iter.Key()), BytesEquals, k) 134 c.Assert(iter.Value(), BytesEquals, v) 135 c.Assert(iter.Next(), IsNil) 136 } 137 c.Assert(iter.Valid(), IsFalse) 138 }