Merge develop

This commit is contained in:
Ming Deng
2021-02-12 22:02:22 +08:00
39 changed files with 800 additions and 1923 deletions

View File

@@ -53,7 +53,7 @@ func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, er
keysErr := make([]string, 0)
for i, ki := range keys {
if _, ok := keyIdx[ki]; !ok {
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "the key isn't exist"))
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "key not exist"))
continue
}
values[i] = res[keyIdx[ki]+1]

View File

@@ -5,9 +5,12 @@ import (
"fmt"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/beego/beego/v2/client/cache"
)
@@ -19,114 +22,80 @@ func TestSsdbcacheCache(t *testing.T) {
}
ssdb, err := cache.NewCache("ssdb", fmt.Sprintf(`{"conn": "%s"}`, ssdbAddr))
if err != nil {
t.Error("init err")
}
assert.Nil(t, err)
// test put and exist
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); res {
t.Error("check err")
}
timeoutDuration := 10 * time.Second
res, _ := ssdb.IsExist(context.Background(), "ssdb")
assert.False(t, res)
timeoutDuration := 3 * time.Second
// timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
t.Error("check err")
}
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration))
res, _ = ssdb.IsExist(context.Background(), "ssdb")
assert.True(t, res)
// Get test done
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
t.Error("set Error", err)
}
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration))
if v, _ := ssdb.Get(context.Background(), "ssdb"); v != "ssdb" {
t.Error("get Error")
}
v, _ := ssdb.Get(context.Background(), "ssdb")
assert.Equal(t, "ssdb", v)
// inc/dec test done
if err = ssdb.Put(context.Background(), "ssdb", "2", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if err = ssdb.Incr(context.Background(), "ssdb"); err != nil {
t.Error("incr Error", err)
}
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "2", timeoutDuration))
assert.Nil(t, ssdb.Incr(context.Background(), "ssdb"))
val, _ := ssdb.Get(context.Background(), "ssdb")
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
t.Error("get err")
}
v, err = strconv.Atoi(val.(string))
assert.Nil(t, err)
assert.Equal(t, 3, v)
if err = ssdb.Decr(context.Background(), "ssdb"); err != nil {
t.Error("decr error")
}
assert.Nil(t, ssdb.Decr(context.Background(), "ssdb"))
// test del
if err = ssdb.Put(context.Background(), "ssdb", "3", timeoutDuration); err != nil {
t.Error("set Error", err)
}
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "3", timeoutDuration))
val, _ = ssdb.Get(context.Background(), "ssdb")
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
t.Error("get err")
}
if err := ssdb.Delete(context.Background(), "ssdb"); err == nil {
if e, _ := ssdb.IsExist(context.Background(), "ssdb"); e {
t.Error("delete err")
}
}
v, err = strconv.Atoi(val.(string))
assert.Equal(t, 3, v)
assert.Nil(t, err)
assert.Nil(t, ssdb.Delete(context.Background(), "ssdb"))
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "ssdb", -10*time.Second))
// test string
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", -10*time.Second); err != nil {
t.Error("set Error", err)
}
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
t.Error("check err")
}
if v, _ := ssdb.Get(context.Background(), "ssdb"); v.(string) != "ssdb" {
t.Error("get err")
}
res, _ = ssdb.IsExist(context.Background(), "ssdb")
assert.True(t, res)
v, _ = ssdb.Get(context.Background(), "ssdb")
assert.Equal(t, "ssdb", v.(string))
// test GetMulti done
if err = ssdb.Put(context.Background(), "ssdb1", "ssdb1", -10*time.Second); err != nil {
t.Error("set Error", err)
}
if res, _ := ssdb.IsExist(context.Background(), "ssdb1"); !res {
t.Error("check err")
}
assert.Nil(t, ssdb.Put(context.Background(), "ssdb1", "ssdb1", -10*time.Second))
res, _ = ssdb.IsExist(context.Background(), "ssdb1")
assert.True(t, res)
vv, _ := ssdb.GetMulti(context.Background(), []string{"ssdb", "ssdb1"})
if len(vv) != 2 {
t.Error("getmulti error")
}
if vv[0].(string) != "ssdb" {
t.Error("getmulti error")
}
if vv[1].(string) != "ssdb1" {
t.Error("getmulti error")
}
assert.Equal(t, 2, len(vv))
assert.Equal(t, "ssdb", vv[0])
assert.Equal(t, "ssdb1", vv[1])
vv, err = ssdb.GetMulti(context.Background(), []string{"ssdb", "ssdb11"})
if len(vv) != 2 {
t.Error("getmulti error")
}
if vv[0].(string) != "ssdb" {
t.Error("getmulti error")
}
if vv[1] != nil {
t.Error("getmulti error")
}
if err != nil && err.Error() != "key [ssdb11] error: the key isn't exist" {
t.Error("getmulti error")
}
assert.Equal(t, 2, len(vv))
assert.Equal(t, "ssdb", vv[0])
assert.Nil(t, vv[1])
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "key not exist"))
// test clear all done
if err = ssdb.ClearAll(context.Background()); err != nil {
t.Error("clear all err")
}
assert.Nil(t, ssdb.ClearAll(context.Background()))
e1, _ := ssdb.IsExist(context.Background(), "ssdb")
e2, _ := ssdb.IsExist(context.Background(), "ssdb1")
if e1 || e2 {
t.Error("check err")
}
assert.False(t, e1)
assert.False(t, e2)
}