wheat-cache/pkg/lru/woker_test.go

86 lines
2.1 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"
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
"github.com/stretchr/testify/require"
2021-10-03 09:47:04 +08:00
"testing"
2021-10-04 16:25:52 +08:00
"time"
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 := "v1"
res, err := v1.Set(&proto.SetRequest{
Val: "123",
})
if err != nil {
return nil, err
}
lru.Add(key, v1)
return res.Result, nil
}))
workEvent.InitWaitEvent()
produce.Call(ctx,workEvent)
res, err := workEvent.StartWaitEvent(2 * time.Second)
require.NoError(t, err)
require.Equal(t, res, "123")
workEvent.InitWaitEvent()
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
v2, ok := lru.Get("v1")
if !ok{
return nil, errorx.New("no this key")
}
switch v2.(type) {
case structure.StringXInterface:
res, err := v2.(structure.StringXInterface).Get(&proto.GetRequest{
})
if err != nil {
return nil, err
}
return res.Result, nil
default:
return nil, errorx.New("no this type")
}
}))
produce.Call(ctx, workEvent)
res, err = workEvent.StartWaitEvent(2 * time.Second)
require.NoError(t, err)
require.Equal(t, res, "123")
workEvent.InitWaitEvent()
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
lru.Del()
v2, ok := lru.Get("v1")
if !ok{
return nil, nil
}
switch v2.(type) {
case structure.StringXInterface:
res, err := v2.(structure.StringXInterface).Get(&proto.GetRequest{
})
if err != nil {
return nil, err
}
return res.Result, nil
default:
return nil, errorx.New("no this type")
}
}))
produce.Call(ctx, workEvent)
res, err = workEvent.StartWaitEvent(2 * time.Second)
require.Equal(t, err, nil)
require.Equal(t, res, nil)
2021-10-03 09:47:04 +08:00
}