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

@@ -25,6 +25,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 TestRedisCache(t *testing.T) {
redisAddr := os.Getenv("REDIS_ADDR")
if redisAddr == "" {
@@ -33,27 +41,27 @@ func TestRedisCache(t *testing.T) {
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, redisAddr))
if err != nil {
t.Error("init err")
t.Error(initError)
}
timeoutDuration := 10 * time.Second
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
t.Error("set Error", err)
t.Error(setError, err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
t.Error(checkError)
}
time.Sleep(11 * time.Second)
if bm.IsExist("astaxie") {
t.Error("check err")
t.Error(checkError)
}
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
t.Error("set Error", err)
t.Error(setError, err)
}
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
t.Error("get err")
t.Error(getError)
}
if err = bm.Incr("astaxie"); err != nil {
@@ -61,7 +69,7 @@ func TestRedisCache(t *testing.T) {
}
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 {
t.Error("get err")
t.Error(getError)
}
if err = bm.Decr("astaxie"); err != nil {
@@ -69,7 +77,7 @@ func TestRedisCache(t *testing.T) {
}
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
t.Error("get err")
t.Error(getError)
}
bm.Delete("astaxie")
if bm.IsExist("astaxie") {
@@ -78,33 +86,33 @@ func TestRedisCache(t *testing.T) {
// test string
if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
t.Error("set Error", err)
t.Error(setError, err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
t.Error(checkError)
}
if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" {
t.Error("get err")
t.Error(getError)
}
// test GetMulti
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
t.Error("set Error", err)
t.Error(setError, err)
}
if !bm.IsExist("astaxie1") {
t.Error("check err")
t.Error(checkError)
}
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
if len(vv) != 2 {
t.Error("GetMulti ERROR")
t.Error(getMultiError)
}
if v, _ := redis.String(vv[0], nil); v != "author" {
t.Error("GetMulti ERROR")
t.Error(getMultiError)
}
if v, _ := redis.String(vv[1], nil); v != "author1" {
t.Error("GetMulti ERROR")
t.Error(getMultiError)
}
// test clear all
@@ -118,12 +126,12 @@ func TestCache_Scan(t *testing.T) {
// init
bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`)
if err != nil {
t.Error("init err")
t.Error(initError)
}
// insert all
for i := 0; i < 10000; i++ {
if err = bm.Put(fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration); err != nil {
t.Error("set Error", err)
t.Error(setError, err)
}
}