forked from p93542168/wheat-cache
test(lru): add lru test process
This commit is contained in:
parent
85e9a3b5f8
commit
c8f23d1357
|
@ -2,10 +2,12 @@ package lru
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewLRUCache(t *testing.T) {
|
||||
|
@ -55,4 +57,46 @@ func TestNewLRUCache2(t *testing.T) {
|
|||
_, ok := cache.Get(&key1)
|
||||
require.Equal(t, ok, false)
|
||||
require.Error(t, cache.DelByKey(&key1))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLruProcess(t *testing.T) {
|
||||
lru := NewLRUCache()
|
||||
lru.clearSize = 1000
|
||||
|
||||
for i := 100; i < 200; i++ {
|
||||
lru.Add(&proto.BaseKey{
|
||||
Key: fmt.Sprint(i),
|
||||
Ttl: 20 << 2,
|
||||
}, stringx.NewStringSingle())
|
||||
}
|
||||
|
||||
// mock LruKey
|
||||
for i := 0; i < 100; i++ {
|
||||
lru.Add(&proto.BaseKey{
|
||||
Key: fmt.Sprint(i),
|
||||
Ttl: 4,
|
||||
}, stringx.NewStringSingle())
|
||||
}
|
||||
|
||||
require.Equal(t, lru.nowSize, int64(200*8))
|
||||
|
||||
// 自动清理测试
|
||||
time.Sleep(3 * time.Second)
|
||||
fmt.Println(lru.nowSize)
|
||||
require.Less(t, lru.nowSize, lru.clearSize+1)
|
||||
|
||||
// TTL 测试
|
||||
time.Sleep(2 * time.Second)
|
||||
require.Equal(t, lru.li.Len(), 25)
|
||||
|
||||
// 过期全部的 Key
|
||||
for i := 100; i < 200; i++ {
|
||||
lru.UpdateTTl(&proto.BaseKey{
|
||||
Key: fmt.Sprint(i),
|
||||
Ttl: -1,
|
||||
})
|
||||
}
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
require.Equal(t, lru.nowSize, int64(0))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue