gf/os/gcache/gcache_z_unit_1_test.go

317 lines
7.4 KiB
Go
Raw Normal View History

// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
2018-12-30 14:53:16 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2018-12-30 14:53:16 +08:00
// go test *.go -bench=".*" -benchmem
package gcache_test
import (
"testing"
"time"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/container/gset"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gcache"
"github.com/gogf/gf/os/grpool"
"github.com/gogf/gf/test/gtest"
2018-12-30 14:53:16 +08:00
)
2020-05-17 18:16:26 +08:00
func TestCache_GCache_Set(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
gcache.Set(1, 11, 0)
defer gcache.Removes(g.Slice{1, 2, 3})
t.Assert(gcache.Get(1), 11)
t.Assert(gcache.Contains(1), true)
})
2019-06-10 19:58:00 +08:00
}
2018-12-30 14:53:16 +08:00
func TestCache_Set(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2020-05-17 18:16:26 +08:00
c := gcache.New()
defer c.Close()
c.Set(1, 11, 0)
t.Assert(c.Get(1), 11)
t.Assert(c.Contains(1), true)
})
}
2019-06-10 19:58:00 +08:00
2020-05-17 18:16:26 +08:00
func TestCache_GetVar(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
c := gcache.New()
defer c.Close()
c.Set(1, 11, 0)
t.Assert(c.Get(1), 11)
t.Assert(c.Contains(1), true)
t.Assert(c.GetVar(1).Int(), 11)
t.Assert(c.GetVar(2).Int(), 0)
t.Assert(c.GetVar(2).IsNil(), true)
t.Assert(c.GetVar(2).IsEmpty(), true)
2019-06-10 19:58:00 +08:00
})
2018-12-30 14:53:16 +08:00
}
func TestCache_Set_Expire(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
cache := gcache.New()
cache.Set(2, 22, 100*time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(2), 22)
2019-06-10 19:58:00 +08:00
time.Sleep(200 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(2), nil)
2019-06-10 19:58:00 +08:00
time.Sleep(3 * time.Second)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Size(), 0)
2019-06-10 19:58:00 +08:00
cache.Close()
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
cache := gcache.New()
cache.Set(1, 11, 100*time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(1), 11)
time.Sleep(200 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(1), nil)
})
2018-12-30 14:53:16 +08:00
}
func TestCache_Expire_SetVar(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
cache := gcache.New()
cache.Set(1, 11, 3*time.Second)
expireBefore, _ := cache.GetExpire(1)
t.Assert(cache.Get(1), 11)
cache.SetVar(1, 12)
t.Assert(cache.Get(1), 12)
time.Sleep(1 * time.Second)
cache.SetExpire(1, 5*time.Second)
expireAfter, okAfter := cache.GetExpire(1)
if okAfter {
t.Assert(expireAfter-expireBefore, 3000)
}
time.Sleep(4 * time.Second)
t.Assert(cache.Get(1), 12)
time.Sleep(2 * time.Second)
t.Assert(cache.Get(1), nil)
})
}
2018-12-30 14:53:16 +08:00
func TestCache_Keys_Values(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
cache := gcache.New()
for i := 0; i < 10; i++ {
cache.Set(i, i*10, 0)
}
2020-03-19 22:56:12 +08:00
t.Assert(len(cache.Keys()), 10)
t.Assert(len(cache.Values()), 10)
t.AssertIN(0, cache.Keys())
t.AssertIN(90, cache.Values())
2019-06-10 19:58:00 +08:00
})
2018-12-30 14:53:16 +08:00
}
func TestCache_LRU(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
cache := gcache.New(2)
for i := 0; i < 10; i++ {
cache.Set(i, i, 0)
}
2020-03-19 22:56:12 +08:00
t.Assert(cache.Size(), 10)
t.Assert(cache.Get(6), 6)
2019-06-10 19:58:00 +08:00
time.Sleep(4 * time.Second)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Size(), 2)
t.Assert(cache.Get(6), 6)
t.Assert(cache.Get(1), nil)
2019-06-10 19:58:00 +08:00
cache.Close()
})
}
func TestCache_LRU_expire(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
cache := gcache.New(2)
cache.Set(1, nil, 1000)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Size(), 1)
t.Assert(cache.Get(1), nil)
2019-06-10 19:58:00 +08:00
})
}
func TestCache_SetIfNotExist(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
cache := gcache.New()
cache.SetIfNotExist(1, 11, 0)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(1), 11)
2019-06-10 19:58:00 +08:00
cache.SetIfNotExist(1, 22, 0)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(1), 11)
2019-06-10 19:58:00 +08:00
cache.SetIfNotExist(2, 22, 0)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(2), 22)
2019-06-10 19:58:00 +08:00
2020-05-17 18:16:26 +08:00
gcache.Removes(g.Slice{1, 2, 3})
2019-06-10 19:58:00 +08:00
gcache.SetIfNotExist(1, 11, 0)
2020-03-19 22:56:12 +08:00
t.Assert(gcache.Get(1), 11)
2019-06-10 19:58:00 +08:00
gcache.SetIfNotExist(1, 22, 0)
2020-03-19 22:56:12 +08:00
t.Assert(gcache.Get(1), 11)
2019-06-10 19:58:00 +08:00
})
}
func TestCache_Sets(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
cache := gcache.New()
cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(1), 11)
2019-06-10 19:58:00 +08:00
2020-05-17 18:16:26 +08:00
gcache.Removes(g.Slice{1, 2, 3})
2019-06-10 19:58:00 +08:00
gcache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(gcache.Get(1), 11)
2019-06-10 19:58:00 +08:00
})
}
func TestCache_GetOrSet(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
cache := gcache.New()
cache.GetOrSet(1, 11, 0)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(1), 11)
2019-06-10 19:58:00 +08:00
cache.GetOrSet(1, 111, 0)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(1), 11)
2019-06-10 19:58:00 +08:00
2020-05-17 18:16:26 +08:00
gcache.Removes(g.Slice{1, 2, 3})
2019-06-10 19:58:00 +08:00
gcache.GetOrSet(1, 11, 0)
2020-03-19 22:56:12 +08:00
t.Assert(gcache.Get(1), 11)
2019-06-10 19:58:00 +08:00
gcache.GetOrSet(1, 111, 0)
2020-03-19 22:56:12 +08:00
t.Assert(gcache.Get(1), 11)
2019-06-10 19:58:00 +08:00
})
}
func TestCache_GetOrSetFunc(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
cache := gcache.New()
cache.GetOrSetFunc(1, func() interface{} {
return 11
}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(1), 11)
2019-06-10 19:58:00 +08:00
cache.GetOrSetFunc(1, func() interface{} {
return 111
}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(1), 11)
2019-06-10 19:58:00 +08:00
2020-05-17 18:16:26 +08:00
gcache.Removes(g.Slice{1, 2, 3})
2019-06-10 19:58:00 +08:00
gcache.GetOrSetFunc(1, func() interface{} {
return 11
}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(gcache.Get(1), 11)
2019-06-10 19:58:00 +08:00
gcache.GetOrSetFunc(1, func() interface{} {
return 111
}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(gcache.Get(1), 11)
2019-06-10 19:58:00 +08:00
})
}
func TestCache_GetOrSetFuncLock(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
cache := gcache.New()
cache.GetOrSetFuncLock(1, func() interface{} {
return 11
}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(1), 11)
2019-06-10 19:58:00 +08:00
cache.GetOrSetFuncLock(1, func() interface{} {
return 111
}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Get(1), 11)
2019-06-10 19:58:00 +08:00
2020-05-17 18:16:26 +08:00
gcache.Removes(g.Slice{1, 2, 3})
2019-06-10 19:58:00 +08:00
gcache.GetOrSetFuncLock(1, func() interface{} {
return 11
}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(gcache.Get(1), 11)
2019-06-10 19:58:00 +08:00
gcache.GetOrSetFuncLock(1, func() interface{} {
return 111
}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(gcache.Get(1), 11)
2019-06-10 19:58:00 +08:00
})
}
func TestCache_Clear(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
cache := gcache.New()
cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0)
cache.Clear()
2020-03-19 22:56:12 +08:00
t.Assert(cache.Size(), 0)
2019-06-10 19:58:00 +08:00
})
}
func TestCache_SetConcurrency(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
cache := gcache.New()
pool := grpool.New(4)
go func() {
for {
pool.Add(func() {
cache.SetIfNotExist(1, 11, 10)
})
}
}()
select {
case <-time.After(2 * time.Second):
//t.Log("first part end")
2019-06-10 19:58:00 +08:00
}
go func() {
for {
pool.Add(func() {
cache.SetIfNotExist(1, nil, 10)
})
}
}()
select {
case <-time.After(2 * time.Second):
//t.Log("second part end")
2019-06-10 19:58:00 +08:00
}
})
}
func TestCache_Basic(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 19:58:00 +08:00
{
cache := gcache.New()
cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(cache.Contains(1), true)
t.Assert(cache.Get(1), 11)
2019-06-10 19:58:00 +08:00
data := cache.Data()
2020-03-19 22:56:12 +08:00
t.Assert(data[1], 11)
t.Assert(data[2], 22)
t.Assert(data[3], nil)
t.Assert(cache.Size(), 2)
2019-06-10 19:58:00 +08:00
keys := cache.Keys()
2020-03-19 22:56:12 +08:00
t.Assert(gset.NewFrom(g.Slice{1, 2}).Equal(gset.NewFrom(keys)), true)
2019-06-10 19:58:00 +08:00
keyStrs := cache.KeyStrings()
2020-03-19 22:56:12 +08:00
t.Assert(gset.NewFrom(g.Slice{"1", "2"}).Equal(gset.NewFrom(keyStrs)), true)
2019-06-10 19:58:00 +08:00
values := cache.Values()
2020-03-19 22:56:12 +08:00
t.Assert(gset.NewFrom(g.Slice{11, 22}).Equal(gset.NewFrom(values)), true)
2019-06-10 19:58:00 +08:00
removeData1 := cache.Remove(1)
2020-03-19 22:56:12 +08:00
t.Assert(removeData1, 11)
t.Assert(cache.Size(), 1)
2019-06-10 19:58:00 +08:00
cache.Removes(g.Slice{2})
2020-03-19 22:56:12 +08:00
t.Assert(cache.Size(), 0)
2019-06-10 19:58:00 +08:00
}
2020-05-17 18:16:26 +08:00
gcache.Removes(g.Slice{1, 2, 3})
2019-06-10 19:58:00 +08:00
{
gcache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0)
2020-03-19 22:56:12 +08:00
t.Assert(gcache.Contains(1), true)
t.Assert(gcache.Get(1), 11)
2019-06-10 19:58:00 +08:00
data := gcache.Data()
2020-03-19 22:56:12 +08:00
t.Assert(data[1], 11)
t.Assert(data[2], 22)
t.Assert(data[3], nil)
t.Assert(gcache.Size(), 2)
2019-06-10 19:58:00 +08:00
keys := gcache.Keys()
2020-03-19 22:56:12 +08:00
t.Assert(gset.NewFrom(g.Slice{1, 2}).Equal(gset.NewFrom(keys)), true)
2019-06-10 19:58:00 +08:00
keyStrs := gcache.KeyStrings()
2020-03-19 22:56:12 +08:00
t.Assert(gset.NewFrom(g.Slice{"1", "2"}).Equal(gset.NewFrom(keyStrs)), true)
2019-06-10 19:58:00 +08:00
values := gcache.Values()
2020-03-19 22:56:12 +08:00
t.Assert(gset.NewFrom(g.Slice{11, 22}).Equal(gset.NewFrom(values)), true)
2019-06-10 19:58:00 +08:00
removeData1 := gcache.Remove(1)
2020-03-19 22:56:12 +08:00
t.Assert(removeData1, 11)
t.Assert(gcache.Size(), 1)
2019-06-10 19:58:00 +08:00
gcache.Removes(g.Slice{2})
2020-03-19 22:56:12 +08:00
t.Assert(gcache.Size(), 0)
2019-06-10 19:58:00 +08:00
}
})
}