2022-10-25 18:24:01 +03:00
|
|
|
package rdb_test
|
|
|
|
|
|
|
|
import (
|
2022-11-03 14:47:45 +03:00
|
|
|
"log"
|
2022-10-25 18:24:01 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.bit5.ru/backend/rdb"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-11-03 14:47:45 +03:00
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
"github.com/go-logr/stdr"
|
2022-10-25 18:24:01 +03:00
|
|
|
)
|
|
|
|
|
2022-11-03 14:47:45 +03:00
|
|
|
const verbosity = 0
|
|
|
|
|
2022-10-28 14:03:40 +03:00
|
|
|
func getPool() *rdb.Pool {
|
2022-11-03 14:47:45 +03:00
|
|
|
//TODO: use ENV settings as well
|
|
|
|
pool := rdb.OpenPool(rdb.RdSettings{Host: "localhost", Port: 6379, Db: 10, ClientName: "test", LogLevel: verbosity}, getLogger())
|
2022-10-25 18:24:01 +03:00
|
|
|
return pool
|
|
|
|
}
|
|
|
|
|
2022-11-03 14:47:45 +03:00
|
|
|
func getLogger() logr.Logger {
|
|
|
|
//TODO: use ENV settings as well
|
|
|
|
stdr.SetVerbosity(verbosity)
|
|
|
|
return stdr.New(log.New(os.Stdout, "", log.Lshortfile))
|
2022-10-25 18:24:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadConnection(t *testing.T) {
|
2022-10-28 17:04:35 +03:00
|
|
|
pool := rdb.OpenPool(rdb.RdSettings{Host: "dummy", Port: 80, ClientName: "test"}, getLogger())
|
2022-10-25 18:24:01 +03:00
|
|
|
conn := pool.Get()
|
|
|
|
assert.NotNil(t, conn)
|
|
|
|
_, err := conn.Do("PING")
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetConn(t *testing.T) {
|
|
|
|
pool := getPool()
|
2022-10-28 14:03:40 +03:00
|
|
|
defer pool.Close()
|
2022-10-25 18:24:01 +03:00
|
|
|
|
|
|
|
conn := pool.Get()
|
|
|
|
defer conn.Close()
|
2022-10-31 16:05:04 +03:00
|
|
|
|
2022-10-25 18:24:01 +03:00
|
|
|
_, err := conn.Do("PING")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseConn(t *testing.T) {
|
|
|
|
pool := getPool()
|
2022-10-31 16:05:04 +03:00
|
|
|
assert.EqualValues(t, 0, pool.RP.IdleCount())
|
2022-10-28 14:03:40 +03:00
|
|
|
defer pool.Close()
|
2022-10-25 18:24:01 +03:00
|
|
|
|
|
|
|
conn := pool.Get()
|
2022-10-28 14:03:40 +03:00
|
|
|
assert.EqualValues(t, 0, pool.RP.IdleCount())
|
2022-10-25 18:24:01 +03:00
|
|
|
|
|
|
|
_, err := conn.Do("PING")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
conn.Close()
|
2022-10-31 16:05:04 +03:00
|
|
|
assert.EqualValues(t, 1, pool.RP.IdleCount())
|
|
|
|
|
2022-10-25 18:24:01 +03:00
|
|
|
_, err = conn.Do("PING")
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDoubleCloseConnIsOk(t *testing.T) {
|
|
|
|
pool := getPool()
|
2022-10-28 14:03:40 +03:00
|
|
|
defer pool.Close()
|
2022-10-25 18:24:01 +03:00
|
|
|
|
|
|
|
conn := pool.Get()
|
2022-10-31 16:05:04 +03:00
|
|
|
assert.EqualValues(t, 0, pool.RP.IdleCount())
|
|
|
|
|
2022-10-25 18:24:01 +03:00
|
|
|
_, err := conn.Do("PING")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-10-28 14:03:40 +03:00
|
|
|
assert.EqualValues(t, 0, pool.RP.IdleCount())
|
2022-10-25 18:24:01 +03:00
|
|
|
conn.Close()
|
2022-10-28 14:03:40 +03:00
|
|
|
assert.EqualValues(t, 1, pool.RP.IdleCount())
|
2022-10-25 18:24:01 +03:00
|
|
|
conn.Close()
|
2022-10-28 14:03:40 +03:00
|
|
|
assert.EqualValues(t, 1, pool.RP.IdleCount())
|
2022-10-25 18:24:01 +03:00
|
|
|
}
|
2022-10-31 16:05:04 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|