wheat-cache/pkg/lru/woker_test.go

60 lines
1.5 KiB
Go
Raw Normal View History

2021-10-03 09:47:04 +08:00
package lru
import (
2021-10-04 16:25:52 +08:00
"context"
2021-10-18 23:58:46 +08:00
"testing"
"time"
2021-10-04 16:25:52 +08:00
"gitee.com/timedb/wheatCache/pkg/event"
2021-10-10 20:53:34 +08:00
"gitee.com/timedb/wheatCache/pkg/logx"
"gitee.com/timedb/wheatCache/pkg/proto"
2021-10-04 16:25:52 +08:00
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
"github.com/stretchr/testify/require"
2021-10-03 09:47:04 +08:00
)
2021-10-04 16:25:52 +08:00
func TestWorker(t *testing.T) {
ctx := context.Background()
lru := NewLRUCache()
produce := event.NewProduce(lru.GetDriver())
2021-10-04 20:33:38 +08:00
workEvent := event.NewEvent(OptionEventName)
2021-10-04 16:25:52 +08:00
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
v1 := stringx.NewStringSingle()
key := proto.BaseKey{
Key: "v1",
}
2021-10-05 16:53:28 +08:00
res, _ := v1.Set("123")
lru.Add(&key, v1)
2021-10-05 16:53:28 +08:00
return res, nil
2021-10-04 16:25:52 +08:00
}))
workEvent.InitWaitEvent()
produce.Call(ctx, workEvent)
2021-10-05 16:53:28 +08:00
res, err := workEvent.StartWaitEvent(2 * time.Second)
2021-10-04 16:25:52 +08:00
require.NoError(t, err)
require.Equal(t, res, "123")
2021-10-03 09:47:04 +08:00
}
2021-10-10 20:53:34 +08:00
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)
}
2021-10-18 23:58:46 +08:00
time.Sleep(5 * time.Second)
2021-10-10 20:53:34 +08:00
logx.Info("end size is %d", lru.nowSize)
}