1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 00:10:50 +00:00
Beego/client/cache/cache_test.go

194 lines
5.0 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 cache
import (
2020-10-04 14:11:28 +00:00
"context"
2020-07-22 14:50:08 +00:00
"os"
"sync"
"testing"
"time"
)
func TestCacheIncr(t *testing.T) {
bm, err := NewCache("memory", `{"interval":20}`)
if err != nil {
t.Error("init err")
}
2020-10-04 14:11:28 +00:00
// timeoutDuration := 10 * time.Second
2020-07-22 14:50:08 +00:00
2020-10-04 14:11:28 +00:00
bm.Put(context.Background(), "edwardhey", 0, time.Second*20)
2020-07-22 14:50:08 +00:00
wg := sync.WaitGroup{}
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
2020-10-04 14:11:28 +00:00
bm.Incr(context.Background(), "edwardhey")
2020-07-22 14:50:08 +00:00
}()
}
wg.Wait()
2020-10-04 14:11:28 +00:00
val, _ := bm.Get(context.Background(), "edwardhey")
if val.(int) != 10 {
2020-07-22 14:50:08 +00:00
t.Error("Incr err")
}
}
func TestCache(t *testing.T) {
bm, err := NewCache("memory", `{"interval":20}`)
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")
}
2020-10-04 14:11:28 +00:00
if v, _ := bm.Get(context.Background(), "astaxie"); v.(int) != 1 {
2020-07-22 14:50:08 +00:00
t.Error("get err")
}
time.Sleep(30 * 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
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
if v, _ := bm.Get(context.Background(), "astaxie"); v.(int) != 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
if v, _ := bm.Get(context.Background(), "astaxie"); v.(int) != 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-10-04 14:11:28 +00:00
// test GetMulti
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
if v, _ := bm.Get(context.Background(), "astaxie"); v.(string) != "author" {
2020-07-22 14:50:08 +00:00
t.Error("get err")
}
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 vv[0].(string) != "author" {
t.Error("GetMulti ERROR")
}
if vv[1].(string) != "author1" {
t.Error("GetMulti ERROR")
}
}
func TestFileCache(t *testing.T) {
bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`)
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")
}
2020-10-04 14:11:28 +00:00
if v, _ := bm.Get(context.Background(), "astaxie"); v.(int) != 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
if v, _ := bm.Get(context.Background(), "astaxie"); v.(int) != 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
if v, _ := bm.Get(context.Background(), "astaxie"); v.(int) != 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-10-04 14:11:28 +00:00
// test string
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
if v, _ := bm.Get(context.Background(), "astaxie"); v.(string) != "author" {
2020-07-22 14:50:08 +00:00
t.Error("get err")
}
2020-10-04 14:11:28 +00:00
// test GetMulti
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 vv[0].(string) != "author" {
t.Error("GetMulti ERROR")
}
if vv[1].(string) != "author1" {
t.Error("GetMulti ERROR")
}
os.RemoveAll("cache")
}