From 537971dca135946da69e1ab31cf74e3db819b2e9 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 8 Jul 2014 11:24:19 +0300 Subject: [PATCH] Fix db selection. Fixes #31. --- redis.go | 2 +- redis_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/redis.go b/redis.go index c124bf7..1c5f6c3 100644 --- a/redis.go +++ b/redis.go @@ -40,7 +40,7 @@ func (c *baseClient) conn() (*conn, error) { } func (c *baseClient) initConn(cn *conn) error { - if c.opt.Password == "" || c.opt.DB == 0 { + if c.opt.Password == "" && c.opt.DB == 0 { return nil } diff --git a/redis_test.go b/redis_test.go index 4d3e003..3588aad 100644 --- a/redis_test.go +++ b/redis_test.go @@ -150,6 +150,20 @@ func (t *RedisConnectorTest) TestIdleTimeout(c *C) { } } +func (t *RedisConnectorTest) TestSelectDb(c *C) { + client1 := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + DB: 1, + }) + c.Assert(client1.Set("key", "db1").Err(), IsNil) + + client2 := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + DB: 2, + }) + c.Assert(client2.Get("key").Err(), Equals, redis.Nil) +} + //------------------------------------------------------------------------------ type RedisConnPoolTest struct {