wheat-cache/pkg/lru/woker_test.go

33 lines
806 B
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/event"
"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
"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 := 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
}