test(dao-channelX): add dao channelX test

This commit is contained in:
bandl 2021-11-30 18:51:31 +08:00
parent bf0c03a77f
commit dcfae2353b
1 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,70 @@
package dao
import (
"strconv"
"testing"
"gitee.com/wheat-os/wheatCache/pkg/event2"
"gitee.com/wheat-os/wheatCache/pkg/lru"
"gitee.com/wheat-os/wheatCache/pkg/proto"
"github.com/stretchr/testify/require"
)
func execWaitFunc(t *testing.T, work interface{}) (interface{}, error) {
wait, ok := work.(event2.EventAwaitFunc)
require.True(t, ok)
return wait()
}
func TestDao_CPush_CPop(t *testing.T) {
testBaseKey := proto.NewBaseKey("123")
l := lru.NewLRUCache()
dao := NewDao(l)
_, err := dao.CMake(testBaseKey, 200)
require.NoError(t, err)
wait, err := dao.CPush(testBaseKey, []string{"1", "2", "3"})
require.NoError(t, err)
waitFunc, ok := wait.(event2.EventAwaitFunc)
require.True(t, ok)
_, err = waitFunc()
require.NoError(t, err)
wait, err = dao.CPop(testBaseKey, 3)
require.NoError(t, err)
res, err := execWaitFunc(t, wait)
require.NoError(t, err)
require.Equal(t, res.(*proto.CPopResponse).Result, []string{"1", "2", "3"})
}
func TestDao_Async_Push(t *testing.T) {
testBaseKey := proto.NewBaseKey("123")
l := lru.NewLRUCache()
dao := NewDao(l)
_, err := dao.CMake(testBaseKey, 200)
require.NoError(t, err)
rq := make([]string, 0)
for i := 0; i < 300; i++ {
rq = append(rq, strconv.Itoa(i))
}
go func() {
wait, err := dao.CPush(testBaseKey, rq)
require.NoError(t, err)
_, err = execWaitFunc(t, wait)
require.NoError(t, err)
}()
wait, err := dao.CPop(testBaseKey, 300)
require.NoError(t, err)
resp, err := execWaitFunc(t, wait)
require.NoError(t, err)
require.Equal(t, resp.(*proto.CPopResponse).Result, rq)
}