package rdb_test import ( "log" "os" "testing" "git.bit5.ru/backend/rdb" "github.com/stretchr/testify/assert" "github.com/go-logr/logr" "github.com/go-logr/stdr" ) const verbosity = 0 func getPool() *rdb.Pool { //TODO: use ENV settings as well pool := rdb.OpenPool(rdb.RdSettings{Host: "localhost", Port: 6379, Db: 10, ClientName: "test", LogLevel: verbosity}, getLogger()) return pool } func getLogger() logr.Logger { //TODO: use ENV settings as well stdr.SetVerbosity(verbosity) return stdr.New(log.New(os.Stdout, "", log.Lshortfile)) } func TestBadConnection(t *testing.T) { pool := rdb.OpenPool(rdb.RdSettings{Host: "dummy", Port: 80, ClientName: "test"}, getLogger()) conn := pool.Get() assert.NotNil(t, conn) _, err := conn.Do("PING") assert.NotNil(t, err) } func TestGetConn(t *testing.T) { pool := getPool() defer pool.Close() conn := pool.Get() defer conn.Close() _, err := conn.Do("PING") assert.Nil(t, err) } func TestCloseConn(t *testing.T) { pool := getPool() assert.EqualValues(t, 0, pool.RP.IdleCount()) defer pool.Close() conn := pool.Get() assert.EqualValues(t, 0, pool.RP.IdleCount()) _, err := conn.Do("PING") assert.Nil(t, err) conn.Close() assert.EqualValues(t, 1, pool.RP.IdleCount()) _, err = conn.Do("PING") assert.NotNil(t, err) } func TestDoubleCloseConnIsOk(t *testing.T) { pool := getPool() defer pool.Close() conn := pool.Get() assert.EqualValues(t, 0, pool.RP.IdleCount()) _, err := conn.Do("PING") assert.Nil(t, err) assert.EqualValues(t, 0, pool.RP.IdleCount()) conn.Close() assert.EqualValues(t, 1, pool.RP.IdleCount()) conn.Close() assert.EqualValues(t, 1, pool.RP.IdleCount()) } func TestDoOnPool(t *testing.T) { pool := getPool() defer pool.Close() assert.EqualValues(t, 0, pool.RP.IdleCount()) _, err := pool.Do("PING") assert.EqualValues(t, 1, pool.RP.IdleCount()) assert.Nil(t, err) }