2019-03-21 00:14:23 +08:00
|
|
|
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gredis_test
|
|
|
|
|
|
|
|
import (
|
2019-12-04 18:43:16 +08:00
|
|
|
"github.com/gogf/gf/frame/g"
|
2020-05-16 21:56:31 +08:00
|
|
|
"github.com/gogf/gf/util/guid"
|
2019-07-09 08:40:28 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-09-06 18:08:58 +08:00
|
|
|
"github.com/gogf/gf/os/gtime"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/database/gredis"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
2019-08-01 14:07:25 +08:00
|
|
|
redis2 "github.com/gomodule/redigo/redis"
|
2019-03-21 00:14:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-06-13 11:58:43 +08:00
|
|
|
config = gredis.Config{
|
|
|
|
Host: "127.0.0.1",
|
|
|
|
Port: 6379,
|
|
|
|
Db: 1,
|
|
|
|
}
|
2019-03-21 00:14:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_NewClose(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-13 11:58:43 +08:00
|
|
|
redis := gredis.New(config)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(redis, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
err := redis.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
})
|
2019-03-21 00:14:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Do(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-13 11:58:43 +08:00
|
|
|
redis := gredis.New(config)
|
|
|
|
defer redis.Close()
|
|
|
|
_, err := redis.Do("SET", "k", "v")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
|
|
|
|
r, err := redis.Do("GET", "k")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, []byte("v"))
|
2019-06-13 11:58:43 +08:00
|
|
|
|
|
|
|
_, err = redis.Do("DEL", "k")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
r, err = redis.Do("GET", "k")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
})
|
2019-03-21 00:14:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Stats(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-13 11:58:43 +08:00
|
|
|
redis := gredis.New(config)
|
|
|
|
defer redis.Close()
|
|
|
|
redis.SetMaxIdle(2)
|
|
|
|
redis.SetMaxActive(100)
|
|
|
|
redis.SetIdleTimeout(500 * time.Millisecond)
|
|
|
|
redis.SetMaxConnLifetime(500 * time.Millisecond)
|
|
|
|
|
|
|
|
array := make([]*gredis.Conn, 0)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
array = append(array, redis.Conn())
|
|
|
|
}
|
|
|
|
stats := redis.Stats()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(stats.ActiveCount, 10)
|
|
|
|
t.Assert(stats.IdleCount, 0)
|
2019-06-13 11:58:43 +08:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
array[i].Close()
|
|
|
|
}
|
|
|
|
stats = redis.Stats()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(stats.ActiveCount, 2)
|
|
|
|
t.Assert(stats.IdleCount, 2)
|
2019-06-13 11:58:43 +08:00
|
|
|
//time.Sleep(3000*time.Millisecond)
|
|
|
|
//stats = redis.Stats()
|
|
|
|
//fmt.Println(stats)
|
2020-03-19 22:56:12 +08:00
|
|
|
//t.Assert(stats.ActiveCount, 0)
|
|
|
|
//t.Assert(stats.IdleCount, 0)
|
2019-06-13 11:58:43 +08:00
|
|
|
})
|
2019-03-21 00:14:23 +08:00
|
|
|
}
|
2019-03-21 10:04:53 +08:00
|
|
|
|
|
|
|
func Test_Conn(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-13 11:58:43 +08:00
|
|
|
redis := gredis.New(config)
|
|
|
|
defer redis.Close()
|
|
|
|
conn := redis.Conn()
|
|
|
|
defer conn.Close()
|
|
|
|
|
2020-01-20 14:14:11 +08:00
|
|
|
key := gconv.String(gtime.TimestampNano())
|
2019-09-06 18:08:58 +08:00
|
|
|
value := []byte("v")
|
|
|
|
r, err := conn.Do("SET", key, value)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
|
2019-09-06 18:08:58 +08:00
|
|
|
r, err = conn.Do("GET", key)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, value)
|
2019-09-06 18:08:58 +08:00
|
|
|
|
|
|
|
_, err = conn.Do("DEL", key)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-09-06 18:08:58 +08:00
|
|
|
r, err = conn.Do("GET", key)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
})
|
2019-04-02 14:37:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Instance(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-13 11:58:43 +08:00
|
|
|
group := "my-test"
|
|
|
|
gredis.SetConfig(config, group)
|
|
|
|
defer gredis.RemoveConfig(group)
|
|
|
|
redis := gredis.Instance(group)
|
|
|
|
defer redis.Close()
|
|
|
|
|
|
|
|
conn := redis.Conn()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
_, err := conn.Do("SET", "k", "v")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
|
|
|
|
r, err := conn.Do("GET", "k")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, []byte("v"))
|
2019-06-13 11:58:43 +08:00
|
|
|
|
|
|
|
_, err = conn.Do("DEL", "k")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
r, err = conn.Do("GET", "k")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-29 09:49:29 +08:00
|
|
|
func Test_Error(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-13 11:58:43 +08:00
|
|
|
config1 := gredis.Config{
|
2019-10-29 09:49:29 +08:00
|
|
|
Host: "127.0.0.2",
|
|
|
|
Port: 6379,
|
|
|
|
Db: 1,
|
|
|
|
ConnectTimeout: time.Second,
|
2019-06-13 11:58:43 +08:00
|
|
|
}
|
|
|
|
redis := gredis.New(config1)
|
|
|
|
_, err := redis.Do("info")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
|
|
|
|
config1 = gredis.Config{
|
|
|
|
Host: "127.0.0.1",
|
|
|
|
Port: 6379,
|
|
|
|
Db: 1,
|
|
|
|
Pass: "666666",
|
|
|
|
}
|
|
|
|
redis = gredis.New(config1)
|
|
|
|
_, err = redis.Do("info")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
|
|
|
|
config1 = gredis.Config{
|
|
|
|
Host: "127.0.0.1",
|
|
|
|
Port: 6379,
|
|
|
|
Db: 100,
|
|
|
|
}
|
|
|
|
redis = gredis.New(config1)
|
|
|
|
_, err = redis.Do("info")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
|
|
|
|
redis = gredis.Instance("gf")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(redis == nil, true)
|
2019-06-13 11:58:43 +08:00
|
|
|
gredis.ClearConfig()
|
|
|
|
|
|
|
|
redis = gredis.New(config)
|
|
|
|
defer redis.Close()
|
|
|
|
_, err = redis.DoVar("SET", "k", "v")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
|
|
|
|
v, err := redis.DoVar("GET", "k")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(v.String(), "v")
|
2019-06-13 11:58:43 +08:00
|
|
|
|
|
|
|
conn := redis.GetConn()
|
|
|
|
_, err = conn.DoVar("SET", "k", "v")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-06-13 11:58:43 +08:00
|
|
|
|
|
|
|
//v, err = conn.ReceiveVar()
|
2020-03-19 22:56:12 +08:00
|
|
|
//t.Assert(err, nil)
|
|
|
|
//t.Assert(v.String(), "v")
|
2019-06-13 11:58:43 +08:00
|
|
|
|
|
|
|
psc := redis2.PubSubConn{Conn: conn}
|
|
|
|
psc.Subscribe("gf")
|
|
|
|
redis.DoVar("PUBLISH", "gf", "gf test")
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
v, _ := conn.ReceiveVar()
|
|
|
|
switch obj := v.Val().(type) {
|
|
|
|
case redis2.Message:
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(string(obj.Data), "gf test")
|
2019-06-13 11:58:43 +08:00
|
|
|
case redis2.Subscription:
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
})
|
2019-03-21 10:04:53 +08:00
|
|
|
}
|
2019-08-22 23:24:45 +08:00
|
|
|
|
|
|
|
func Test_Bool(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-22 23:24:45 +08:00
|
|
|
redis := gredis.New(config)
|
2020-03-08 23:16:06 +08:00
|
|
|
defer func() {
|
|
|
|
redis.Do("DEL", "key-true")
|
|
|
|
redis.Do("DEL", "key-false")
|
|
|
|
}()
|
|
|
|
|
2019-08-22 23:24:45 +08:00
|
|
|
_, err := redis.Do("SET", "key-true", true)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-08-22 23:24:45 +08:00
|
|
|
|
|
|
|
_, err = redis.Do("SET", "key-false", false)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-08-22 23:24:45 +08:00
|
|
|
|
|
|
|
r, err := redis.DoVar("GET", "key-true")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r.Bool(), true)
|
2019-08-22 23:24:45 +08:00
|
|
|
|
|
|
|
r, err = redis.DoVar("GET", "key-false")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r.Bool(), false)
|
2019-08-22 23:24:45 +08:00
|
|
|
})
|
|
|
|
}
|
2019-12-04 18:43:16 +08:00
|
|
|
|
|
|
|
func Test_Int(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-04 18:43:16 +08:00
|
|
|
redis := gredis.New(config)
|
2020-05-16 21:56:31 +08:00
|
|
|
key := guid.S()
|
2020-03-08 23:16:06 +08:00
|
|
|
defer redis.Do("DEL", key)
|
|
|
|
|
2019-12-04 18:43:16 +08:00
|
|
|
_, err := redis.Do("SET", key, 1)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-12-04 18:43:16 +08:00
|
|
|
|
|
|
|
r, err := redis.DoVar("GET", key)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r.Int(), 1)
|
2019-12-04 18:43:16 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_HSet(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-04 18:43:16 +08:00
|
|
|
redis := gredis.New(config)
|
2020-05-16 21:56:31 +08:00
|
|
|
key := guid.S()
|
2020-03-08 23:16:06 +08:00
|
|
|
defer redis.Do("DEL", key)
|
|
|
|
|
2019-12-04 18:43:16 +08:00
|
|
|
_, err := redis.Do("HSET", key, "name", "john")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-12-04 18:43:16 +08:00
|
|
|
|
|
|
|
r, err := redis.DoVar("HGETALL", key)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r.Strings(), g.ArrayStr{"name", "john"})
|
2019-12-04 18:43:16 +08:00
|
|
|
})
|
|
|
|
}
|
2020-03-08 23:16:06 +08:00
|
|
|
|
|
|
|
func Test_HGetAll(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-08 23:16:06 +08:00
|
|
|
var err error
|
|
|
|
redis := gredis.New(config)
|
2020-05-16 21:56:31 +08:00
|
|
|
key := guid.S()
|
2020-03-08 23:16:06 +08:00
|
|
|
defer redis.Do("DEL", key)
|
|
|
|
|
|
|
|
_, err = redis.Do("HSET", key, "id", "100")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-08 23:16:06 +08:00
|
|
|
_, err = redis.Do("HSET", key, "name", "john")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-08 23:16:06 +08:00
|
|
|
|
|
|
|
r, err := redis.DoVar("HGETALL", key)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r.Map(), g.MapStrAny{
|
2020-03-08 23:16:06 +08:00
|
|
|
"id": 100,
|
|
|
|
"name": "john",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2020-05-18 19:18:53 +08:00
|
|
|
|
|
|
|
func Test_Auto_Marshal(t *testing.T) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
redis = gredis.New(config)
|
|
|
|
key = guid.S()
|
|
|
|
)
|
|
|
|
defer redis.Do("DEL", key)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
user := &User{
|
|
|
|
Id: 10000,
|
|
|
|
Name: "john",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = redis.Do("SET", key, user)
|
|
|
|
t.Assert(err, nil)
|
|
|
|
|
|
|
|
r, err := redis.DoVar("GET", key)
|
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r.Map(), g.MapStrAny{
|
|
|
|
"Id": user.Id,
|
|
|
|
"Name": user.Name,
|
|
|
|
})
|
|
|
|
|
|
|
|
var user2 *User
|
|
|
|
t.Assert(r.Struct(&user2), nil)
|
|
|
|
t.Assert(user2.Id, user.Id)
|
|
|
|
t.Assert(user2.Name, user.Name)
|
|
|
|
})
|
|
|
|
}
|