Fix Sonart PR1

This commit is contained in:
Ming Deng
2021-02-01 00:18:34 +08:00
parent 326fc5dd9c
commit 41d682d878
11 changed files with 142 additions and 104 deletions

View File

@@ -10,6 +10,14 @@ import (
"github.com/beego/beego/v2/adapter/cache"
)
const (
initError = "init err"
setError = "set Error"
checkError = "check err"
getError = "get err"
getMultiError = "GetMulti Error"
)
func TestSsdbcacheCache(t *testing.T) {
ssdbAddr := os.Getenv("SSDB_ADDR")
if ssdbAddr == "" {
@@ -18,25 +26,25 @@ func TestSsdbcacheCache(t *testing.T) {
ssdb, err := cache.NewCache("ssdb", fmt.Sprintf(`{"conn": "%s"}`, ssdbAddr))
if err != nil {
t.Error("init err")
t.Error(initError)
}
// test put and exist
if ssdb.IsExist("ssdb") {
t.Error("check err")
t.Error(checkError)
}
timeoutDuration := 10 * time.Second
// timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil {
t.Error("set Error", err)
t.Error(setError, err)
}
if !ssdb.IsExist("ssdb") {
t.Error("check err")
t.Error(checkError)
}
// Get test done
if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil {
t.Error("set Error", err)
t.Error(setError, err)
}
if v := ssdb.Get("ssdb"); v != "ssdb" {
@@ -45,14 +53,14 @@ func TestSsdbcacheCache(t *testing.T) {
// inc/dec test done
if err = ssdb.Put("ssdb", "2", timeoutDuration); err != nil {
t.Error("set Error", err)
t.Error(setError, err)
}
if err = ssdb.Incr("ssdb"); err != nil {
t.Error("incr Error", err)
}
if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 {
t.Error("get err")
t.Error(getError)
}
if err = ssdb.Decr("ssdb"); err != nil {
@@ -61,10 +69,10 @@ func TestSsdbcacheCache(t *testing.T) {
// test del
if err = ssdb.Put("ssdb", "3", timeoutDuration); err != nil {
t.Error("set Error", err)
t.Error(setError, err)
}
if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 {
t.Error("get err")
t.Error(getError)
}
if err := ssdb.Delete("ssdb"); err == nil {
if ssdb.IsExist("ssdb") {
@@ -74,31 +82,31 @@ func TestSsdbcacheCache(t *testing.T) {
// test string
if err = ssdb.Put("ssdb", "ssdb", -10*time.Second); err != nil {
t.Error("set Error", err)
t.Error(setError, err)
}
if !ssdb.IsExist("ssdb") {
t.Error("check err")
t.Error(checkError)
}
if v := ssdb.Get("ssdb").(string); v != "ssdb" {
t.Error("get err")
t.Error(getError)
}
// test GetMulti done
if err = ssdb.Put("ssdb1", "ssdb1", -10*time.Second); err != nil {
t.Error("set Error", err)
t.Error(setError, err)
}
if !ssdb.IsExist("ssdb1") {
t.Error("check err")
t.Error(checkError)
}
vv := ssdb.GetMulti([]string{"ssdb", "ssdb1"})
if len(vv) != 2 {
t.Error("getmulti error")
t.Error(getMultiError)
}
if vv[0].(string) != "ssdb" {
t.Error("getmulti error")
t.Error(getMultiError)
}
if vv[1].(string) != "ssdb1" {
t.Error("getmulti error")
t.Error(getMultiError)
}
// test clear all done
@@ -106,6 +114,6 @@ func TestSsdbcacheCache(t *testing.T) {
t.Error("clear all err")
}
if ssdb.IsExist("ssdb") || ssdb.IsExist("ssdb1") {
t.Error("check err")
t.Error(checkError)
}
}