github.com/astaxie/beego@v1.12.3/cache/ssdb/ssdb_test.go (about) 1 package ssdb 2 3 import ( 4 "strconv" 5 "testing" 6 "time" 7 8 "github.com/astaxie/beego/cache" 9 ) 10 11 func TestSsdbcacheCache(t *testing.T) { 12 ssdb, err := cache.NewCache("ssdb", `{"conn": "127.0.0.1:8888"}`) 13 if err != nil { 14 t.Error("init err") 15 } 16 17 // test put and exist 18 if ssdb.IsExist("ssdb") { 19 t.Error("check err") 20 } 21 timeoutDuration := 10 * time.Second 22 //timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent 23 if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil { 24 t.Error("set Error", err) 25 } 26 if !ssdb.IsExist("ssdb") { 27 t.Error("check err") 28 } 29 30 // Get test done 31 if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil { 32 t.Error("set Error", err) 33 } 34 35 if v := ssdb.Get("ssdb"); v != "ssdb" { 36 t.Error("get Error") 37 } 38 39 //inc/dec test done 40 if err = ssdb.Put("ssdb", "2", timeoutDuration); err != nil { 41 t.Error("set Error", err) 42 } 43 if err = ssdb.Incr("ssdb"); err != nil { 44 t.Error("incr Error", err) 45 } 46 47 if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 { 48 t.Error("get err") 49 } 50 51 if err = ssdb.Decr("ssdb"); err != nil { 52 t.Error("decr error") 53 } 54 55 // test del 56 if err = ssdb.Put("ssdb", "3", timeoutDuration); err != nil { 57 t.Error("set Error", err) 58 } 59 if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 { 60 t.Error("get err") 61 } 62 if err := ssdb.Delete("ssdb"); err == nil { 63 if ssdb.IsExist("ssdb") { 64 t.Error("delete err") 65 } 66 } 67 68 //test string 69 if err = ssdb.Put("ssdb", "ssdb", -10*time.Second); err != nil { 70 t.Error("set Error", err) 71 } 72 if !ssdb.IsExist("ssdb") { 73 t.Error("check err") 74 } 75 if v := ssdb.Get("ssdb").(string); v != "ssdb" { 76 t.Error("get err") 77 } 78 79 //test GetMulti done 80 if err = ssdb.Put("ssdb1", "ssdb1", -10*time.Second); err != nil { 81 t.Error("set Error", err) 82 } 83 if !ssdb.IsExist("ssdb1") { 84 t.Error("check err") 85 } 86 vv := ssdb.GetMulti([]string{"ssdb", "ssdb1"}) 87 if len(vv) != 2 { 88 t.Error("getmulti error") 89 } 90 if vv[0].(string) != "ssdb" { 91 t.Error("getmulti error") 92 } 93 if vv[1].(string) != "ssdb1" { 94 t.Error("getmulti error") 95 } 96 97 // test clear all done 98 if err = ssdb.ClearAll(); err != nil { 99 t.Error("clear all err") 100 } 101 if ssdb.IsExist("ssdb") || ssdb.IsExist("ssdb1") { 102 t.Error("check err") 103 } 104 }