github.com/astaxie/beego@v1.12.3/cache/cache_test.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cache
    16  
    17  import (
    18  	"os"
    19  	"sync"
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  func TestCacheIncr(t *testing.T) {
    25  	bm, err := NewCache("memory", `{"interval":20}`)
    26  	if err != nil {
    27  		t.Error("init err")
    28  	}
    29  	//timeoutDuration := 10 * time.Second
    30  
    31  	bm.Put("edwardhey", 0, time.Second*20)
    32  	wg := sync.WaitGroup{}
    33  	wg.Add(10)
    34  	for i := 0; i < 10; i++ {
    35  		go func() {
    36  			defer wg.Done()
    37  			bm.Incr("edwardhey")
    38  		}()
    39  	}
    40  	wg.Wait()
    41  	if bm.Get("edwardhey").(int) != 10 {
    42  		t.Error("Incr err")
    43  	}
    44  }
    45  
    46  func TestCache(t *testing.T) {
    47  	bm, err := NewCache("memory", `{"interval":20}`)
    48  	if err != nil {
    49  		t.Error("init err")
    50  	}
    51  	timeoutDuration := 10 * time.Second
    52  	if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
    53  		t.Error("set Error", err)
    54  	}
    55  	if !bm.IsExist("astaxie") {
    56  		t.Error("check err")
    57  	}
    58  
    59  	if v := bm.Get("astaxie"); v.(int) != 1 {
    60  		t.Error("get err")
    61  	}
    62  
    63  	time.Sleep(30 * time.Second)
    64  
    65  	if bm.IsExist("astaxie") {
    66  		t.Error("check err")
    67  	}
    68  
    69  	if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
    70  		t.Error("set Error", err)
    71  	}
    72  
    73  	if err = bm.Incr("astaxie"); err != nil {
    74  		t.Error("Incr Error", err)
    75  	}
    76  
    77  	if v := bm.Get("astaxie"); v.(int) != 2 {
    78  		t.Error("get err")
    79  	}
    80  
    81  	if err = bm.Decr("astaxie"); err != nil {
    82  		t.Error("Decr Error", err)
    83  	}
    84  
    85  	if v := bm.Get("astaxie"); v.(int) != 1 {
    86  		t.Error("get err")
    87  	}
    88  	bm.Delete("astaxie")
    89  	if bm.IsExist("astaxie") {
    90  		t.Error("delete err")
    91  	}
    92  
    93  	//test GetMulti
    94  	if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
    95  		t.Error("set Error", err)
    96  	}
    97  	if !bm.IsExist("astaxie") {
    98  		t.Error("check err")
    99  	}
   100  	if v := bm.Get("astaxie"); v.(string) != "author" {
   101  		t.Error("get err")
   102  	}
   103  
   104  	if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
   105  		t.Error("set Error", err)
   106  	}
   107  	if !bm.IsExist("astaxie1") {
   108  		t.Error("check err")
   109  	}
   110  
   111  	vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
   112  	if len(vv) != 2 {
   113  		t.Error("GetMulti ERROR")
   114  	}
   115  	if vv[0].(string) != "author" {
   116  		t.Error("GetMulti ERROR")
   117  	}
   118  	if vv[1].(string) != "author1" {
   119  		t.Error("GetMulti ERROR")
   120  	}
   121  }
   122  
   123  func TestFileCache(t *testing.T) {
   124  	bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`)
   125  	if err != nil {
   126  		t.Error("init err")
   127  	}
   128  	timeoutDuration := 10 * time.Second
   129  	if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
   130  		t.Error("set Error", err)
   131  	}
   132  	if !bm.IsExist("astaxie") {
   133  		t.Error("check err")
   134  	}
   135  
   136  	if v := bm.Get("astaxie"); v.(int) != 1 {
   137  		t.Error("get err")
   138  	}
   139  
   140  	if err = bm.Incr("astaxie"); err != nil {
   141  		t.Error("Incr Error", err)
   142  	}
   143  
   144  	if v := bm.Get("astaxie"); v.(int) != 2 {
   145  		t.Error("get err")
   146  	}
   147  
   148  	if err = bm.Decr("astaxie"); err != nil {
   149  		t.Error("Decr Error", err)
   150  	}
   151  
   152  	if v := bm.Get("astaxie"); v.(int) != 1 {
   153  		t.Error("get err")
   154  	}
   155  	bm.Delete("astaxie")
   156  	if bm.IsExist("astaxie") {
   157  		t.Error("delete err")
   158  	}
   159  
   160  	//test string
   161  	if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
   162  		t.Error("set Error", err)
   163  	}
   164  	if !bm.IsExist("astaxie") {
   165  		t.Error("check err")
   166  	}
   167  	if v := bm.Get("astaxie"); v.(string) != "author" {
   168  		t.Error("get err")
   169  	}
   170  
   171  	//test GetMulti
   172  	if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
   173  		t.Error("set Error", err)
   174  	}
   175  	if !bm.IsExist("astaxie1") {
   176  		t.Error("check err")
   177  	}
   178  
   179  	vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
   180  	if len(vv) != 2 {
   181  		t.Error("GetMulti ERROR")
   182  	}
   183  	if vv[0].(string) != "author" {
   184  		t.Error("GetMulti ERROR")
   185  	}
   186  	if vv[1].(string) != "author1" {
   187  		t.Error("GetMulti ERROR")
   188  	}
   189  
   190  	os.RemoveAll("cache")
   191  }