test(lru): add the test of worker

This commit is contained in:
HuangJiaLuo 2021-10-04 16:25:52 +08:00
parent 71ddeb4772
commit ca04ba1686
2 changed files with 82 additions and 6 deletions

View File

@ -1,9 +1,85 @@
package lru
import (
"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"
"testing"
"time"
)
func TestSingleCache_RetDriver(t *testing.T) {
func TestWorker(t *testing.T) {
ctx := context.Background()
lru := NewLRUCache()
produce := event.NewProduce(lru.GetDriver())
workEvent := event.NewEvent(OpEventName)
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)
}

View File

@ -13,16 +13,16 @@ func lruCleanWork() {
func (lru *singleCache) lruSingleWork() interface{} {
ctx := context.Background()
for {
eventKon := lru.lruConsumer.Receive(ctx)
workEvent := lru.lruConsumer.Receive(ctx)
worFunc, ok := eventKon.GetValue(WorkFuncEventCtxKey)
workFunc, ok := workEvent.GetValue(WorkFuncEventKey)
if !ok {
continue
}
switch worFunc.(type) {
switch workFunc.(type) {
case event.EventWorkFunc:
eventKon.ExecWorkAndSendResult(worFunc.(event.EventWorkFunc))
workEvent.ExecWorkAndSendResult(workFunc.(event.EventWorkFunc))
default:
log.Print("this is debug ")
}