1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-24 03:20:55 +00:00
Beego/client/cache/ssdb/ssdb_test.go

133 lines
3.3 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
package ssdb
import (
2020-10-04 14:11:28 +00:00
"context"
"fmt"
"os"
2020-07-22 14:50:08 +00:00
"strconv"
"testing"
"time"
"github.com/astaxie/beego/client/cache"
2020-07-22 14:50:08 +00:00
)
func TestSsdbcacheCache(t *testing.T) {
ssdbAddr := os.Getenv("SSDB_ADDR")
if ssdbAddr == "" {
ssdbAddr = "127.0.0.1:8888"
}
ssdb, err := cache.NewCache("ssdb", fmt.Sprintf(`{"conn": "%s"}`, ssdbAddr))
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error("init err")
}
// test put and exist
2020-10-04 14:11:28 +00:00
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); res {
2020-07-22 14:50:08 +00:00
t.Error("check err")
}
timeoutDuration := 10 * time.Second
2020-10-04 14:11:28 +00:00
// timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("set Error", err)
}
2020-10-04 14:11:28 +00:00
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
2020-07-22 14:50:08 +00:00
t.Error("check err")
}
// Get test done
2020-10-04 14:11:28 +00:00
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("set Error", err)
}
2020-10-04 14:11:28 +00:00
if v, _ := ssdb.Get(context.Background(), "ssdb"); v != "ssdb" {
2020-07-22 14:50:08 +00:00
t.Error("get Error")
}
2020-10-04 14:11:28 +00:00
// inc/dec test done
if err = ssdb.Put(context.Background(), "ssdb", "2", timeoutDuration); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("set Error", err)
}
2020-10-04 14:11:28 +00:00
if err = ssdb.Incr(context.Background(), "ssdb"); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("incr Error", err)
}
2020-10-04 14:11:28 +00:00
val, _ := ssdb.Get(context.Background(), "ssdb")
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
2020-07-22 14:50:08 +00:00
t.Error("get err")
}
2020-10-04 14:11:28 +00:00
if err = ssdb.Decr(context.Background(), "ssdb"); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("decr error")
}
// test del
2020-10-04 14:11:28 +00:00
if err = ssdb.Put(context.Background(), "ssdb", "3", timeoutDuration); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("set Error", err)
}
2020-10-04 14:11:28 +00:00
val, _ = ssdb.Get(context.Background(), "ssdb")
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
2020-07-22 14:50:08 +00:00
t.Error("get err")
}
2020-10-04 14:11:28 +00:00
if err := ssdb.Delete(context.Background(), "ssdb"); err == nil {
if e, _ := ssdb.IsExist(context.Background(), "ssdb"); e {
2020-07-22 14:50:08 +00:00
t.Error("delete err")
}
}
2020-10-04 14:11:28 +00:00
// test string
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", -10*time.Second); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("set Error", err)
}
2020-10-04 14:11:28 +00:00
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
2020-07-22 14:50:08 +00:00
t.Error("check err")
}
2020-10-04 14:11:28 +00:00
if v, _ := ssdb.Get(context.Background(), "ssdb"); v.(string) != "ssdb" {
2020-07-22 14:50:08 +00:00
t.Error("get err")
}
2020-10-04 14:11:28 +00:00
// test GetMulti done
if err = ssdb.Put(context.Background(), "ssdb1", "ssdb1", -10*time.Second); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("set Error", err)
}
2020-10-04 14:11:28 +00:00
if res, _ := ssdb.IsExist(context.Background(), "ssdb1"); !res {
2020-07-22 14:50:08 +00:00
t.Error("check err")
}
2020-10-04 14:11:28 +00:00
vv, _ := ssdb.GetMulti(context.Background(), []string{"ssdb", "ssdb1"})
2020-07-22 14:50:08 +00:00
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")
}
2020-11-17 12:53:33 +00:00
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")
}
2020-07-22 14:50:08 +00:00
// test clear all done
2020-10-04 14:11:28 +00:00
if err = ssdb.ClearAll(context.Background()); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("clear all err")
}
2020-10-04 14:11:28 +00:00
e1, _ := ssdb.IsExist(context.Background(), "ssdb")
e2, _ := ssdb.IsExist(context.Background(), "ssdb1")
if e1 || e2 {
2020-07-22 14:50:08 +00:00
t.Error("check err")
}
}