rdb/rdb_test.go

71 lines
1.4 KiB
Go
Raw Normal View History

2022-10-25 18:24:01 +03:00
package rdb_test
import (
"os"
"testing"
"git.bit5.ru/backend/colog"
"git.bit5.ru/backend/rdb"
2022-10-25 18:52:59 +03:00
"git.bit5.ru/backend/redigo/redis"
2022-10-25 18:24:01 +03:00
"github.com/stretchr/testify/assert"
)
func getPool() *redis.Pool {
pool := rdb.NewRedisPool(getLogger(), rdb.RdSettings{Host: "localhost", Port: 6379, Db: 10}, "test")
return pool
}
func getLogger() *colog.CoLog {
tlog := colog.NewCoLog(os.Stderr, "", 0)
ft := &colog.StdFormatter{Flag: 0}
ft.ColorSupported(true)
tlog.SetFormatter(ft)
return tlog
}
func TestBadConnection(t *testing.T) {
pool := rdb.NewRedisPool(getLogger(), rdb.RdSettings{Host: "dummy", Port: 80}, "test")
conn := pool.Get()
assert.NotNil(t, conn)
_, err := conn.Do("PING")
assert.NotNil(t, err)
}
func TestGetConn(t *testing.T) {
pool := getPool()
conn := pool.Get()
defer conn.Close()
_, err := conn.Do("PING")
assert.Nil(t, err)
}
func TestCloseConn(t *testing.T) {
pool := getPool()
conn := pool.Get()
assert.EqualValues(t, 0, pool.IdleCount())
_, err := conn.Do("PING")
assert.Nil(t, err)
conn.Close()
_, err = conn.Do("PING")
assert.NotNil(t, err)
assert.EqualValues(t, 1, pool.IdleCount())
}
func TestDoubleCloseConnIsOk(t *testing.T) {
pool := getPool()
conn := pool.Get()
_, err := conn.Do("PING")
assert.Nil(t, err)
assert.EqualValues(t, 0, pool.IdleCount())
conn.Close()
assert.EqualValues(t, 1, pool.IdleCount())
conn.Close()
assert.EqualValues(t, 1, pool.IdleCount())
}