test(lru): add lru test process

This commit is contained in:
bandl 2021-10-12 21:13:42 +08:00
parent 85e9a3b5f8
commit c8f23d1357
1 changed files with 46 additions and 2 deletions

View File

@ -2,10 +2,12 @@ package lru
import ( import (
"fmt" "fmt"
"testing"
"time"
"gitee.com/timedb/wheatCache/pkg/proto" "gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure/stringx" "gitee.com/timedb/wheatCache/pkg/structure/stringx"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"testing"
) )
func TestNewLRUCache(t *testing.T) { func TestNewLRUCache(t *testing.T) {
@ -55,4 +57,46 @@ func TestNewLRUCache2(t *testing.T) {
_, ok := cache.Get(&key1) _, ok := cache.Get(&key1)
require.Equal(t, ok, false) require.Equal(t, ok, false)
require.Error(t, cache.DelByKey(&key1)) 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))
}