forked from p93542168/wheat-cache
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package lru
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitee.com/timedb/wheatCache/pkg/event"
|
|
"gitee.com/timedb/wheatCache/pkg/logx"
|
|
"gitee.com/timedb/wheatCache/pkg/proto"
|
|
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWorker(t *testing.T) {
|
|
ctx := context.Background()
|
|
lru := NewLRUCache()
|
|
produce := event.NewProduce(lru.GetDriver())
|
|
workEvent := event.NewEvent(OptionEventName)
|
|
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
|
|
v1 := stringx.NewStringSingle()
|
|
key := proto.BaseKey{
|
|
Key: "v1",
|
|
}
|
|
res, _ := v1.Set("123")
|
|
lru.Add(&key, v1)
|
|
return res, nil
|
|
}))
|
|
workEvent.InitWaitEvent()
|
|
produce.Call(ctx, workEvent)
|
|
res, err := workEvent.StartWaitEvent(2 * time.Second)
|
|
require.NoError(t, err)
|
|
require.Equal(t, res, "123")
|
|
}
|
|
|
|
func TestSingleCache_DelToClearSize(t *testing.T) {
|
|
ctx := context.Background()
|
|
lru := NewLRUCache()
|
|
produce := event.NewProduce(lru.GetDriver())
|
|
|
|
for i := int32(20000); i >= 1; i-- {
|
|
workEvent := event.NewEvent(OptionEventName)
|
|
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
|
|
v1 := stringx.NewStringSingle()
|
|
key := proto.BaseKey{
|
|
Key: string(i),
|
|
}
|
|
u := v1.Setbit(i, true)
|
|
lru.Add(&key, v1)
|
|
return u, nil
|
|
}))
|
|
workEvent.InitWaitEvent()
|
|
produce.Call(ctx, workEvent)
|
|
workEvent.StartWaitEvent(2 * time.Second)
|
|
}
|
|
|
|
time.Sleep(5 * time.Second)
|
|
logx.Info("end size is %d", lru.nowSize)
|
|
}
|