1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-15 15:50:56 +00:00
Beego/pkg/client/cache/redis/redis_test.go

164 lines
4.1 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redis
import (
2020-10-04 14:11:28 +00:00
"context"
2020-07-22 14:50:08 +00:00
"fmt"
"os"
2020-07-22 14:50:08 +00:00
"testing"
"time"
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
2020-07-29 13:56:19 +00:00
"github.com/astaxie/beego/pkg/client/cache"
2020-07-22 14:50:08 +00:00
)
func TestRedisCache(t *testing.T) {
redisAddr := os.Getenv("REDIS_ADDR")
if redisAddr == "" {
redisAddr = "127.0.0.1:6379"
}
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, redisAddr))
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error("init err")
}
timeoutDuration := 10 * time.Second
2020-10-04 14:11:28 +00:00
if err = bm.Put(context.Background(), "astaxie", 1, 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, _ := bm.IsExist(context.Background(), "astaxie"); !res {
2020-07-22 14:50:08 +00:00
t.Error("check err")
}
time.Sleep(11 * time.Second)
2020-10-04 14:11:28 +00:00
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
2020-07-22 14:50:08 +00:00
t.Error("check err")
}
2020-10-04 14:11:28 +00:00
if err = bm.Put(context.Background(), "astaxie", 1, 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, _ := bm.Get(context.Background(), "astaxie")
if v, _ := redis.Int(val, err); v != 1 {
2020-07-22 14:50:08 +00:00
t.Error("get err")
}
2020-10-04 14:11:28 +00:00
if err = bm.Incr(context.Background(), "astaxie"); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("Incr Error", err)
}
2020-10-04 14:11:28 +00:00
val, _ = bm.Get(context.Background(), "astaxie")
if v, _ := redis.Int(val, err); v != 2 {
2020-07-22 14:50:08 +00:00
t.Error("get err")
}
2020-10-04 14:11:28 +00:00
if err = bm.Decr(context.Background(), "astaxie"); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("Decr Error", err)
}
2020-10-04 14:11:28 +00:00
val, _ = bm.Get(context.Background(), "astaxie")
if v, _ := redis.Int(val, err); v != 1 {
2020-07-22 14:50:08 +00:00
t.Error("get err")
}
2020-10-04 14:11:28 +00:00
bm.Delete(context.Background(), "astaxie")
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
2020-07-22 14:50:08 +00:00
t.Error("delete err")
}
2020-07-29 13:56:19 +00:00
// test string
2020-10-04 14:11:28 +00:00
if err = bm.Put(context.Background(), "astaxie", "author", 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, _ := bm.IsExist(context.Background(), "astaxie"); !res {
2020-07-22 14:50:08 +00:00
t.Error("check err")
}
2020-10-04 14:11:28 +00:00
val, _ = bm.Get(context.Background(), "astaxie")
if v, _ := redis.String(val, err); v != "author" {
2020-07-22 14:50:08 +00:00
t.Error("get err")
}
2020-07-29 13:56:19 +00:00
// test GetMulti
2020-10-04 14:11:28 +00:00
if err = bm.Put(context.Background(), "astaxie1", "author1", 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, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
2020-07-22 14:50:08 +00:00
t.Error("check err")
}
2020-10-04 14:11:28 +00:00
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
2020-07-22 14:50:08 +00:00
if len(vv) != 2 {
t.Error("GetMulti ERROR")
}
if v, _ := redis.String(vv[0], nil); v != "author" {
t.Error("GetMulti ERROR")
}
if v, _ := redis.String(vv[1], nil); v != "author1" {
t.Error("GetMulti ERROR")
}
// test clear all
2020-10-04 14:11:28 +00:00
if err = bm.ClearAll(context.Background()); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("clear all err")
}
}
func TestCache_Scan(t *testing.T) {
timeoutDuration := 10 * time.Second
addr := os.Getenv("REDIS_ADDR")
if addr == "" {
addr = "127.0.0.1:6379"
}
2020-07-22 14:50:08 +00:00
// init
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, addr))
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error("init err")
}
// insert all
2020-08-30 15:39:07 +00:00
for i := 0; i < 100; i++ {
2020-10-04 14:11:28 +00:00
if err = bm.Put(context.Background(), fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("set Error", err)
}
}
time.Sleep(time.Second)
2020-07-22 14:50:08 +00:00
// scan all for the first time
keys, err := bm.(*Cache).Scan(DefaultKey + ":*")
if err != nil {
t.Error("scan Error", err)
}
2020-08-30 15:39:07 +00:00
assert.Equal(t, 100, len(keys), "scan all error")
2020-07-22 14:50:08 +00:00
// clear all
2020-10-04 14:11:28 +00:00
if err = bm.ClearAll(context.Background()); err != nil {
2020-07-22 14:50:08 +00:00
t.Error("clear all err")
}
// scan all for the second time
keys, err = bm.(*Cache).Scan(DefaultKey + ":*")
if err != nil {
t.Error("scan Error", err)
}
if len(keys) != 0 {
t.Error("scan all err")
}
}