github.com/bhojpur/cache@v0.0.4/pkg/engine/ristretto/store_test.go (about) 1 package ristretto 2 3 // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved. 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 import ( 24 "strconv" 25 "testing" 26 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestStoreSetGet(t *testing.T) { 31 s := newStore() 32 key, conflict := defaultStringHash("1") 33 i := Item{ 34 Key: key, 35 Conflict: conflict, 36 Value: 2, 37 } 38 s.Set(&i) 39 val, ok := s.Get(key, conflict) 40 require.True(t, ok) 41 require.Equal(t, 2, val.(int)) 42 43 i.Value = 3 44 s.Set(&i) 45 val, ok = s.Get(key, conflict) 46 require.True(t, ok) 47 require.Equal(t, 3, val.(int)) 48 49 key, conflict = defaultStringHash("2") 50 i = Item{ 51 Key: key, 52 Conflict: conflict, 53 Value: 2, 54 } 55 s.Set(&i) 56 val, ok = s.Get(key, conflict) 57 require.True(t, ok) 58 require.Equal(t, 2, val.(int)) 59 } 60 61 func TestStoreDel(t *testing.T) { 62 s := newStore() 63 key, conflict := defaultStringHash("1") 64 i := Item{ 65 Key: key, 66 Conflict: conflict, 67 Value: 1, 68 } 69 s.Set(&i) 70 s.Del(key, conflict) 71 val, ok := s.Get(key, conflict) 72 require.False(t, ok) 73 require.Nil(t, val) 74 75 s.Del(2, 0) 76 } 77 78 func TestStoreClear(t *testing.T) { 79 s := newStore() 80 for i := 0; i < 1000; i++ { 81 key, conflict := defaultStringHash(strconv.Itoa(i)) 82 it := Item{ 83 Key: key, 84 Conflict: conflict, 85 Value: i, 86 } 87 s.Set(&it) 88 } 89 s.Clear(nil) 90 for i := 0; i < 1000; i++ { 91 key, conflict := defaultStringHash(strconv.Itoa(i)) 92 val, ok := s.Get(key, conflict) 93 require.False(t, ok) 94 require.Nil(t, val) 95 } 96 } 97 98 func TestStoreUpdate(t *testing.T) { 99 s := newStore() 100 key, conflict := defaultStringHash("1") 101 i := Item{ 102 Key: key, 103 Conflict: conflict, 104 Value: 1, 105 } 106 s.Set(&i) 107 i.Value = 2 108 _, ok := s.Update(&i) 109 require.True(t, ok) 110 111 val, ok := s.Get(key, conflict) 112 require.True(t, ok) 113 require.NotNil(t, val) 114 115 val, ok = s.Get(key, conflict) 116 require.True(t, ok) 117 require.Equal(t, 2, val.(int)) 118 119 i.Value = 3 120 _, ok = s.Update(&i) 121 require.True(t, ok) 122 123 val, ok = s.Get(key, conflict) 124 require.True(t, ok) 125 require.Equal(t, 3, val.(int)) 126 127 key, conflict = defaultStringHash("2") 128 i = Item{ 129 Key: key, 130 Conflict: conflict, 131 Value: 2, 132 } 133 _, ok = s.Update(&i) 134 require.False(t, ok) 135 val, ok = s.Get(key, conflict) 136 require.False(t, ok) 137 require.Nil(t, val) 138 } 139 140 func TestStoreCollision(t *testing.T) { 141 s := newShardedMap() 142 s.shards[1].Lock() 143 s.shards[1].data[1] = storeItem{ 144 key: 1, 145 conflict: 0, 146 value: 1, 147 } 148 s.shards[1].Unlock() 149 val, ok := s.Get(1, 1) 150 require.False(t, ok) 151 require.Nil(t, val) 152 153 i := Item{ 154 Key: 1, 155 Conflict: 1, 156 Value: 2, 157 } 158 s.Set(&i) 159 val, ok = s.Get(1, 0) 160 require.True(t, ok) 161 require.NotEqual(t, 2, val.(int)) 162 163 _, ok = s.Update(&i) 164 require.False(t, ok) 165 val, ok = s.Get(1, 0) 166 require.True(t, ok) 167 require.NotEqual(t, 2, val.(int)) 168 169 s.Del(1, 1) 170 val, ok = s.Get(1, 0) 171 require.True(t, ok) 172 require.NotNil(t, val) 173 } 174 175 func BenchmarkStoreGet(b *testing.B) { 176 s := newStore() 177 key, conflict := defaultStringHash("1") 178 i := Item{ 179 Key: key, 180 Conflict: conflict, 181 Value: 1, 182 } 183 s.Set(&i) 184 b.SetBytes(1) 185 b.RunParallel(func(pb *testing.PB) { 186 for pb.Next() { 187 s.Get(key, conflict) 188 } 189 }) 190 } 191 192 func BenchmarkStoreSet(b *testing.B) { 193 s := newStore() 194 key, conflict := defaultStringHash("1") 195 b.SetBytes(1) 196 b.RunParallel(func(pb *testing.PB) { 197 for pb.Next() { 198 i := Item{ 199 Key: key, 200 Conflict: conflict, 201 Value: 1, 202 } 203 s.Set(&i) 204 } 205 }) 206 } 207 208 func BenchmarkStoreUpdate(b *testing.B) { 209 s := newStore() 210 key, conflict := defaultStringHash("1") 211 i := Item{ 212 Key: key, 213 Conflict: conflict, 214 Value: 1, 215 } 216 s.Set(&i) 217 b.SetBytes(1) 218 b.RunParallel(func(pb *testing.PB) { 219 for pb.Next() { 220 s.Update(&Item{ 221 Key: key, 222 Conflict: conflict, 223 Value: 2, 224 }) 225 } 226 }) 227 }