github.com/zhongdalu/gf@v1.0.0/g/database/gredis/gredis_unit_test.go (about) 1 // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package gredis_test 8 9 import ( 10 "testing" 11 "time" 12 13 "github.com/zhongdalu/gf/g/database/gredis" 14 "github.com/zhongdalu/gf/g/test/gtest" 15 redis2 "github.com/zhongdalu/gf/third/github.com/gomodule/redigo/redis" 16 ) 17 18 var ( 19 config = gredis.Config{ 20 Host: "127.0.0.1", 21 Port: 6379, 22 Db: 1, 23 } 24 ) 25 26 func Test_NewClose(t *testing.T) { 27 gtest.Case(t, func() { 28 redis := gredis.New(config) 29 gtest.AssertNE(redis, nil) 30 err := redis.Close() 31 gtest.Assert(err, nil) 32 }) 33 } 34 35 func Test_Do(t *testing.T) { 36 gtest.Case(t, func() { 37 redis := gredis.New(config) 38 defer redis.Close() 39 _, err := redis.Do("SET", "k", "v") 40 gtest.Assert(err, nil) 41 42 r, err := redis.Do("GET", "k") 43 gtest.Assert(err, nil) 44 gtest.Assert(r, []byte("v")) 45 46 _, err = redis.Do("DEL", "k") 47 gtest.Assert(err, nil) 48 r, err = redis.Do("GET", "k") 49 gtest.Assert(err, nil) 50 gtest.Assert(r, nil) 51 }) 52 } 53 54 func Test_Send(t *testing.T) { 55 gtest.Case(t, func() { 56 redis := gredis.New(config) 57 defer redis.Close() 58 err := redis.Send("SET", "k", "v") 59 gtest.Assert(err, nil) 60 61 r, err := redis.Do("GET", "k") 62 gtest.Assert(err, nil) 63 gtest.Assert(r, []byte("v")) 64 }) 65 } 66 67 func Test_Stats(t *testing.T) { 68 gtest.Case(t, func() { 69 redis := gredis.New(config) 70 defer redis.Close() 71 redis.SetMaxIdle(2) 72 redis.SetMaxActive(100) 73 redis.SetIdleTimeout(500 * time.Millisecond) 74 redis.SetMaxConnLifetime(500 * time.Millisecond) 75 76 array := make([]*gredis.Conn, 0) 77 for i := 0; i < 10; i++ { 78 array = append(array, redis.Conn()) 79 } 80 stats := redis.Stats() 81 gtest.Assert(stats.ActiveCount, 10) 82 gtest.Assert(stats.IdleCount, 0) 83 for i := 0; i < 10; i++ { 84 array[i].Close() 85 } 86 stats = redis.Stats() 87 gtest.Assert(stats.ActiveCount, 2) 88 gtest.Assert(stats.IdleCount, 2) 89 //time.Sleep(3000*time.Millisecond) 90 //stats = redis.Stats() 91 //fmt.Println(stats) 92 //gtest.Assert(stats.ActiveCount, 0) 93 //gtest.Assert(stats.IdleCount, 0) 94 }) 95 } 96 97 func Test_Conn(t *testing.T) { 98 gtest.Case(t, func() { 99 redis := gredis.New(config) 100 defer redis.Close() 101 conn := redis.Conn() 102 defer conn.Close() 103 104 r, err := conn.Do("GET", "k") 105 gtest.Assert(err, nil) 106 gtest.Assert(r, []byte("v")) 107 108 _, err = conn.Do("DEL", "k") 109 gtest.Assert(err, nil) 110 r, err = conn.Do("GET", "k") 111 gtest.Assert(err, nil) 112 gtest.Assert(r, nil) 113 }) 114 } 115 116 func Test_Instance(t *testing.T) { 117 gtest.Case(t, func() { 118 group := "my-test" 119 gredis.SetConfig(config, group) 120 defer gredis.RemoveConfig(group) 121 redis := gredis.Instance(group) 122 defer redis.Close() 123 124 conn := redis.Conn() 125 defer conn.Close() 126 127 _, err := conn.Do("SET", "k", "v") 128 gtest.Assert(err, nil) 129 130 r, err := conn.Do("GET", "k") 131 gtest.Assert(err, nil) 132 gtest.Assert(r, []byte("v")) 133 134 _, err = conn.Do("DEL", "k") 135 gtest.Assert(err, nil) 136 r, err = conn.Do("GET", "k") 137 gtest.Assert(err, nil) 138 gtest.Assert(r, nil) 139 }) 140 } 141 142 func Test_Basic(t *testing.T) { 143 gtest.Case(t, func() { 144 config1 := gredis.Config{ 145 Host: "127.0.0.2", 146 Port: 6379, 147 Db: 1, 148 } 149 redis := gredis.New(config1) 150 _, err := redis.Do("info") 151 gtest.AssertNE(err, nil) 152 153 config1 = gredis.Config{ 154 Host: "127.0.0.1", 155 Port: 6379, 156 Db: 1, 157 Pass: "666666", 158 } 159 redis = gredis.New(config1) 160 _, err = redis.Do("info") 161 gtest.AssertNE(err, nil) 162 163 config1 = gredis.Config{ 164 Host: "127.0.0.1", 165 Port: 6379, 166 Db: 100, 167 } 168 redis = gredis.New(config1) 169 _, err = redis.Do("info") 170 gtest.AssertNE(err, nil) 171 172 redis = gredis.Instance("gf") 173 gtest.Assert(redis == nil, true) 174 gredis.ClearConfig() 175 176 redis = gredis.New(config) 177 defer redis.Close() 178 _, err = redis.DoVar("SET", "k", "v") 179 gtest.Assert(err, nil) 180 181 v, err := redis.DoVar("GET", "k") 182 gtest.Assert(err, nil) 183 gtest.Assert(v.String(), "v") 184 185 conn := redis.GetConn() 186 _, err = conn.DoVar("SET", "k", "v") 187 gtest.Assert(err, nil) 188 189 //v, err = conn.ReceiveVar() 190 //gtest.Assert(err, nil) 191 //gtest.Assert(v.String(), "v") 192 193 psc := redis2.PubSubConn{Conn: conn} 194 psc.Subscribe("gf") 195 redis.DoVar("PUBLISH", "gf", "gf test") 196 go func() { 197 for { 198 v, _ := conn.ReceiveVar() 199 switch obj := v.Val().(type) { 200 case redis2.Message: 201 gtest.Assert(string(obj.Data), "gf test") 202 case redis2.Subscription: 203 204 } 205 } 206 }() 207 208 time.Sleep(time.Second) 209 }) 210 }