From fbb4cb3b1ca7de2654f960fbd0f5841af0600c3f Mon Sep 17 00:00:00 2001 From: John Date: Thu, 21 Mar 2019 10:04:53 +0800 Subject: [PATCH] add more unit test cases for gredis --- g/database/gredis/gredis.go | 21 +++++++++++++-------- g/database/gredis/gredis_unit_test.go | 19 +++++++++++++++++++ g/frame/gins/gins_redis_test.go | 6 +++--- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/g/database/gredis/gredis.go b/g/database/gredis/gredis.go index c4e375dbd..8a255bd77 100644 --- a/g/database/gredis/gredis.go +++ b/g/database/gredis/gredis.go @@ -24,13 +24,13 @@ const ( // Redis客户端(管理连接池) type Redis struct { - pool *redis.Pool + pool *redis.Pool + config Config } // Redis连接对象(连接池中的单个连接) type Conn redis.Conn - // Redis服务端但节点连接配置信息 type Config struct { Host string // 地址 @@ -63,7 +63,8 @@ func New(config Config) *Redis { config.MaxConnLifetime = gDEFAULT_POOL_MAX_LIFE_TIME } return &Redis{ - pool : pools.GetOrSetFuncLock(fmt.Sprintf("%v", config), func() interface{} { + config : config, + pool : pools.GetOrSetFuncLock(fmt.Sprintf("%v", config), func() interface{} { return &redis.Pool { IdleTimeout : config.IdleTimeout, MaxConnLifetime : config.MaxConnLifetime, @@ -97,8 +98,9 @@ func New(config Config) *Redis { // Close closes the redis connection pool, // it will release all connections reserved by this pool. // -// 关闭redis管理对象,将会关闭底层的 +// 关闭redis管理对象,将会关闭底层的连接池。 func (r *Redis) Close() error { + pools.Remove(fmt.Sprintf("%v", r.config)) return r.pool.Close() } @@ -107,8 +109,9 @@ func (r *Redis) Conn() Conn { return r.GetConn() } -// GetConn returns a raw connection object, which expose more methods communication with server. -// You should call Close function manually if you do not use this connection any more. +// GetConn returns a raw connection object, +// which expose more methods communication with server. +// **You should call Close function manually if you do not use this connection any further.** // // 获得一个原生的redis连接对象,用于自定义连接操作, // 但是需要注意的是如果不再使用该连接对象时,需要手动Close连接,否则会造成连接数超限。 @@ -146,20 +149,22 @@ func (r *Redis) SetMaxConnLifetime(value time.Duration) { // Stats returns pool's statistics. // -// 获取当前连接池统计信息 +// 获取当前连接池统计信息。 func (r *Redis) Stats() *PoolStats { return &PoolStats{r.pool.Stats()} } // Do sends a command to the server and returns the received reply. +// Do automatically get a connection from pool, and close it when reply received. // -// 执行同步命令 - Do +// 执行同步命令,自动从连接池中获取连接,使用完毕后关闭连接(丢回连接池),开发者不用自行Close. func (r *Redis) Do(command string, args ...interface{}) (interface{}, error) { conn := r.pool.Get() defer conn.Close() return conn.Do(command, args...) } +// Deprecated, use Conn instead. // Send writes the command to the client's output buffer. // // 执行异步命令 - Send diff --git a/g/database/gredis/gredis_unit_test.go b/g/database/gredis/gredis_unit_test.go index 53e9589e2..d3a16c825 100644 --- a/g/database/gredis/gredis_unit_test.go +++ b/g/database/gredis/gredis_unit_test.go @@ -91,3 +91,22 @@ func Test_Stats(t *testing.T) { //gtest.Assert(stats.IdleCount, 0) }) } + +func Test_Conn(t *testing.T) { + gtest.Case(t, func() { + redis := gredis.New(config) + defer redis.Close() + conn := redis.Conn() + defer conn.Close() + + r, err := conn.Do("GET", "k") + gtest.Assert(err, nil) + gtest.Assert(r, []byte("v")) + + _, err = conn.Do("DEL", "k") + gtest.Assert(err, nil) + r, err = conn.Do("GET", "k") + gtest.Assert(err, nil) + gtest.Assert(r, nil) + }) +} diff --git a/g/frame/gins/gins_redis_test.go b/g/frame/gins/gins_redis_test.go index 8585af5b0..62df9f5db 100644 --- a/g/frame/gins/gins_redis_test.go +++ b/g/frame/gins/gins_redis_test.go @@ -45,9 +45,9 @@ test = "v=3" priority = "1" # Redis数据库配置 [redis] - default = "127.0.0.1:6379,0" - cache = "127.0.0.1:6379,1" - disk = "127.0.0.1:6379,1,?maxIdle=1&maxActive=10&idleTimeout=10&maxConnLifetime=10" + default = "127.0.0.1:6379,10" + cache = "127.0.0.1:6379,20" + disk = "127.0.0.1:6379,30,?maxIdle=1&maxActive=10&idleTimeout=10&maxConnLifetime=10" ` path := "config.toml" err := gfile.PutContents(path, config)