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

@@ -21,10 +21,18 @@ import (
"time"
)
const (
initError = "init err"
setError = "set Error"
checkError = "check err"
getError = "get err"
getMultiError = "GetMulti Error"
)
func TestCacheIncr(t *testing.T) {
bm, err := NewCache("memory", `{"interval":20}`)
if err != nil {
t.Error("init err")
t.Error(initError)
}
// timeoutDuration := 10 * time.Second
@@ -46,28 +54,28 @@ func TestCacheIncr(t *testing.T) {
func TestCache(t *testing.T) {
bm, err := NewCache("memory", `{"interval":20}`)
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)
}
if v := bm.Get("astaxie"); v.(int) != 1 {
t.Error("get err")
t.Error(getError)
}
time.Sleep(30 * 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 err = bm.Incr("astaxie"); err != nil {
@@ -75,7 +83,7 @@ func TestCache(t *testing.T) {
}
if v := bm.Get("astaxie"); v.(int) != 2 {
t.Error("get err")
t.Error(getError)
}
if err = bm.Decr("astaxie"); err != nil {
@@ -83,7 +91,7 @@ func TestCache(t *testing.T) {
}
if v := bm.Get("astaxie"); v.(int) != 1 {
t.Error("get err")
t.Error(getError)
}
bm.Delete("astaxie")
if bm.IsExist("astaxie") {
@@ -92,49 +100,49 @@ func TestCache(t *testing.T) {
// test GetMulti
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 := bm.Get("astaxie"); v.(string) != "author" {
t.Error("get err")
t.Error(getError)
}
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 vv[0].(string) != "author" {
t.Error("GetMulti ERROR")
t.Error(getMultiError)
}
if vv[1].(string) != "author1" {
t.Error("GetMulti ERROR")
t.Error(getMultiError)
}
}
func TestFileCache(t *testing.T) {
bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`)
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)
}
if v := bm.Get("astaxie"); v.(int) != 1 {
t.Error("get err")
t.Error(getError)
}
if err = bm.Incr("astaxie"); err != nil {
@@ -142,7 +150,7 @@ func TestFileCache(t *testing.T) {
}
if v := bm.Get("astaxie"); v.(int) != 2 {
t.Error("get err")
t.Error(getError)
}
if err = bm.Decr("astaxie"); err != nil {
@@ -150,7 +158,7 @@ func TestFileCache(t *testing.T) {
}
if v := bm.Get("astaxie"); v.(int) != 1 {
t.Error("get err")
t.Error(getError)
}
bm.Delete("astaxie")
if bm.IsExist("astaxie") {
@@ -159,32 +167,32 @@ func TestFileCache(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 := bm.Get("astaxie"); v.(string) != "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 vv[0].(string) != "author" {
t.Error("GetMulti ERROR")
t.Error(getMultiError)
}
if vv[1].(string) != "author1" {
t.Error("GetMulti ERROR")
t.Error(getMultiError)
}
os.RemoveAll("cache")

View File

@@ -24,6 +24,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 TestMemcacheCache(t *testing.T) {
addr := os.Getenv("MEMCACHE_ADDR")
@@ -33,27 +41,27 @@ func TestMemcacheCache(t *testing.T) {
bm, err := cache.NewCache("memcache", fmt.Sprintf(`{"conn": "%s"}`, addr))
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, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 1 {
t.Error("get err")
t.Error(getError)
}
if err = bm.Incr("astaxie"); err != nil {
@@ -61,7 +69,7 @@ func TestMemcacheCache(t *testing.T) {
}
if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 2 {
t.Error("get err")
t.Error(getError)
}
if err = bm.Decr("astaxie"); err != nil {
@@ -69,7 +77,7 @@ func TestMemcacheCache(t *testing.T) {
}
if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 1 {
t.Error("get err")
t.Error(getError)
}
bm.Delete("astaxie")
if bm.IsExist("astaxie") {
@@ -78,33 +86,33 @@ func TestMemcacheCache(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 := bm.Get("astaxie").([]byte); string(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 string(vv[0].([]byte)) != "author" && string(vv[0].([]byte)) != "author1" {
t.Error("GetMulti ERROR")
t.Error(getMultiError)
}
if string(vv[1].([]byte)) != "author1" && string(vv[1].([]byte)) != "author" {
t.Error("GetMulti ERROR")
t.Error(getMultiError)
}
// test clear all

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)
}
}

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)
}
}