From dcfae2353bbdf771930d9891b06dd18152f9cc4c Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Tue, 30 Nov 2021 18:51:31 +0800 Subject: [PATCH] test(dao-channelX): add dao channelX test --- storage/dao/channelx_test.go | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 storage/dao/channelx_test.go diff --git a/storage/dao/channelx_test.go b/storage/dao/channelx_test.go new file mode 100644 index 0000000..384cf94 --- /dev/null +++ b/storage/dao/channelx_test.go @@ -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) +}