github.com/zhongdalu/gf@v1.0.0/g/os/gcache/gcache_z_unit_1_test.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 // go test *.go -bench=".*" -benchmem 8 9 package gcache_test 10 11 import ( 12 "testing" 13 "time" 14 15 "github.com/zhongdalu/gf/g" 16 "github.com/zhongdalu/gf/g/container/gset" 17 "github.com/zhongdalu/gf/g/os/gcache" 18 "github.com/zhongdalu/gf/g/os/grpool" 19 "github.com/zhongdalu/gf/g/test/gtest" 20 ) 21 22 //clear 用于清除全局缓存,因gcache api 暂未暴露 Clear 方法 23 //暂定所有测试用例key的集合为1,2,3,避免不同测试用例间因全局cache共享带来的问题,每个测试用例在测试gcache.XXX之前,先调用clear() 24 func clear() { 25 gcache.Removes(g.Slice{1, 2, 3}) 26 } 27 28 func TestCache_Set(t *testing.T) { 29 gtest.Case(t, func() { 30 cache := gcache.New() 31 cache.Set(1, 11, 0) 32 gtest.Assert(cache.Get(1), 11) 33 gtest.Assert(cache.Contains(1), true) 34 35 clear() 36 gcache.Set(1, 11, 0) 37 gtest.Assert(gcache.Get(1), 11) 38 gtest.Assert(gcache.Contains(1), true) 39 }) 40 } 41 42 func TestCache_Set_Expire(t *testing.T) { 43 gtest.Case(t, func() { 44 cache := gcache.New() 45 cache.Set(2, 22, 100) 46 gtest.Assert(cache.Get(2), 22) 47 time.Sleep(200 * time.Millisecond) 48 gtest.Assert(cache.Get(2), nil) 49 time.Sleep(3 * time.Second) 50 gtest.Assert(cache.Size(), 0) 51 cache.Close() 52 }) 53 54 gtest.Case(t, func() { 55 cache := gcache.New() 56 cache.Set(1, 11, 100*time.Millisecond) 57 gtest.Assert(cache.Get(1), 11) 58 time.Sleep(200 * time.Millisecond) 59 gtest.Assert(cache.Get(1), nil) 60 }) 61 } 62 63 func TestCache_Keys_Values(t *testing.T) { 64 gtest.Case(t, func() { 65 cache := gcache.New() 66 for i := 0; i < 10; i++ { 67 cache.Set(i, i*10, 0) 68 } 69 gtest.Assert(len(cache.Keys()), 10) 70 gtest.Assert(len(cache.Values()), 10) 71 gtest.AssertIN(0, cache.Keys()) 72 gtest.AssertIN(90, cache.Values()) 73 }) 74 } 75 76 func TestCache_LRU(t *testing.T) { 77 gtest.Case(t, func() { 78 cache := gcache.New(2) 79 for i := 0; i < 10; i++ { 80 cache.Set(i, i, 0) 81 } 82 gtest.Assert(cache.Size(), 10) 83 gtest.Assert(cache.Get(6), 6) 84 time.Sleep(4 * time.Second) 85 gtest.Assert(cache.Size(), 2) 86 gtest.Assert(cache.Get(6), 6) 87 gtest.Assert(cache.Get(1), nil) 88 cache.Close() 89 }) 90 } 91 92 func TestCache_LRU_expire(t *testing.T) { 93 gtest.Case(t, func() { 94 cache := gcache.New(2) 95 cache.Set(1, nil, 1000) 96 gtest.Assert(cache.Size(), 1) 97 gtest.Assert(cache.Get(1), nil) 98 }) 99 } 100 101 func TestCache_SetIfNotExist(t *testing.T) { 102 gtest.Case(t, func() { 103 cache := gcache.New() 104 cache.SetIfNotExist(1, 11, 0) 105 gtest.Assert(cache.Get(1), 11) 106 cache.SetIfNotExist(1, 22, 0) 107 gtest.Assert(cache.Get(1), 11) 108 cache.SetIfNotExist(2, 22, 0) 109 gtest.Assert(cache.Get(2), 22) 110 111 clear() 112 gcache.SetIfNotExist(1, 11, 0) 113 gtest.Assert(gcache.Get(1), 11) 114 gcache.SetIfNotExist(1, 22, 0) 115 gtest.Assert(gcache.Get(1), 11) 116 }) 117 } 118 119 func TestCache_Sets(t *testing.T) { 120 gtest.Case(t, func() { 121 cache := gcache.New() 122 cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) 123 gtest.Assert(cache.Get(1), 11) 124 125 clear() 126 gcache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) 127 gtest.Assert(gcache.Get(1), 11) 128 }) 129 } 130 131 func TestCache_GetOrSet(t *testing.T) { 132 gtest.Case(t, func() { 133 cache := gcache.New() 134 cache.GetOrSet(1, 11, 0) 135 gtest.Assert(cache.Get(1), 11) 136 cache.GetOrSet(1, 111, 0) 137 gtest.Assert(cache.Get(1), 11) 138 139 clear() 140 gcache.GetOrSet(1, 11, 0) 141 gtest.Assert(gcache.Get(1), 11) 142 gcache.GetOrSet(1, 111, 0) 143 gtest.Assert(gcache.Get(1), 11) 144 }) 145 } 146 147 func TestCache_GetOrSetFunc(t *testing.T) { 148 gtest.Case(t, func() { 149 cache := gcache.New() 150 cache.GetOrSetFunc(1, func() interface{} { 151 return 11 152 }, 0) 153 gtest.Assert(cache.Get(1), 11) 154 cache.GetOrSetFunc(1, func() interface{} { 155 return 111 156 }, 0) 157 gtest.Assert(cache.Get(1), 11) 158 159 clear() 160 gcache.GetOrSetFunc(1, func() interface{} { 161 return 11 162 }, 0) 163 gtest.Assert(gcache.Get(1), 11) 164 gcache.GetOrSetFunc(1, func() interface{} { 165 return 111 166 }, 0) 167 gtest.Assert(gcache.Get(1), 11) 168 }) 169 } 170 171 func TestCache_GetOrSetFuncLock(t *testing.T) { 172 gtest.Case(t, func() { 173 cache := gcache.New() 174 cache.GetOrSetFuncLock(1, func() interface{} { 175 return 11 176 }, 0) 177 gtest.Assert(cache.Get(1), 11) 178 cache.GetOrSetFuncLock(1, func() interface{} { 179 return 111 180 }, 0) 181 gtest.Assert(cache.Get(1), 11) 182 183 clear() 184 gcache.GetOrSetFuncLock(1, func() interface{} { 185 return 11 186 }, 0) 187 gtest.Assert(gcache.Get(1), 11) 188 gcache.GetOrSetFuncLock(1, func() interface{} { 189 return 111 190 }, 0) 191 gtest.Assert(gcache.Get(1), 11) 192 }) 193 } 194 195 func TestCache_Clear(t *testing.T) { 196 gtest.Case(t, func() { 197 cache := gcache.New() 198 cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) 199 cache.Clear() 200 gtest.Assert(cache.Size(), 0) 201 }) 202 } 203 204 func TestCache_SetConcurrency(t *testing.T) { 205 gtest.Case(t, func() { 206 cache := gcache.New() 207 pool := grpool.New(4) 208 go func() { 209 for { 210 pool.Add(func() { 211 cache.SetIfNotExist(1, 11, 10) 212 }) 213 } 214 }() 215 select { 216 case <-time.After(2 * time.Second): 217 //t.Log("first part end") 218 } 219 220 go func() { 221 for { 222 pool.Add(func() { 223 cache.SetIfNotExist(1, nil, 10) 224 }) 225 } 226 }() 227 select { 228 case <-time.After(2 * time.Second): 229 //t.Log("second part end") 230 } 231 }) 232 } 233 234 func TestCache_Basic(t *testing.T) { 235 gtest.Case(t, func() { 236 { 237 cache := gcache.New() 238 cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) 239 gtest.Assert(cache.Contains(1), true) 240 gtest.Assert(cache.Get(1), 11) 241 data := cache.Data() 242 gtest.Assert(data[1], 11) 243 gtest.Assert(data[2], 22) 244 gtest.Assert(data[3], nil) 245 gtest.Assert(cache.Size(), 2) 246 keys := cache.Keys() 247 gtest.Assert(gset.NewFrom(g.Slice{1, 2}).Equal(gset.NewFrom(keys)), true) 248 keyStrs := cache.KeyStrings() 249 gtest.Assert(gset.NewFrom(g.Slice{"1", "2"}).Equal(gset.NewFrom(keyStrs)), true) 250 values := cache.Values() 251 gtest.Assert(gset.NewFrom(g.Slice{11, 22}).Equal(gset.NewFrom(values)), true) 252 removeData1 := cache.Remove(1) 253 gtest.Assert(removeData1, 11) 254 gtest.Assert(cache.Size(), 1) 255 cache.Removes(g.Slice{2}) 256 gtest.Assert(cache.Size(), 0) 257 } 258 259 clear() 260 { 261 gcache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) 262 gtest.Assert(gcache.Contains(1), true) 263 gtest.Assert(gcache.Get(1), 11) 264 data := gcache.Data() 265 gtest.Assert(data[1], 11) 266 gtest.Assert(data[2], 22) 267 gtest.Assert(data[3], nil) 268 gtest.Assert(gcache.Size(), 2) 269 keys := gcache.Keys() 270 gtest.Assert(gset.NewFrom(g.Slice{1, 2}).Equal(gset.NewFrom(keys)), true) 271 keyStrs := gcache.KeyStrings() 272 gtest.Assert(gset.NewFrom(g.Slice{"1", "2"}).Equal(gset.NewFrom(keyStrs)), true) 273 values := gcache.Values() 274 gtest.Assert(gset.NewFrom(g.Slice{11, 22}).Equal(gset.NewFrom(values)), true) 275 removeData1 := gcache.Remove(1) 276 gtest.Assert(removeData1, 11) 277 gtest.Assert(gcache.Size(), 1) 278 gcache.Removes(g.Slice{2}) 279 gtest.Assert(gcache.Size(), 0) 280 } 281 }) 282 }