From ee844aaf1d289a6dd16b4293ccc2e678787881b4 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sat, 25 Aug 2012 21:54:35 +0300 Subject: [PATCH] multi: Properly reset client in case of empty transaction. --- multi.go | 8 +++++--- redis_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/multi.go b/multi.go index 8a875286..091989ed 100644 --- a/multi.go +++ b/multi.go @@ -53,12 +53,14 @@ func (c *MultiClient) Exec(do func()) ([]Req, error) { c.mtx.Lock() c.reqs = append(c.reqs, NewIfaceSliceReq("EXEC")) - if len(c.reqs) == 2 { + + reqs := c.reqs + c.reqs = nil + + if len(reqs) == 2 { c.mtx.Unlock() return []Req{}, nil } - reqs := c.reqs - c.reqs = nil c.mtx.Unlock() conn, err := c.conn() diff --git a/redis_test.go b/redis_test.go index 048adfdc..682b8723 100644 --- a/redis_test.go +++ b/redis_test.go @@ -2395,6 +2395,22 @@ func (t *RedisTest) TestMultiExecDiscard(c *C) { c.Assert(get.Val(), Equals, "hello2") } +func (t *RedisTest) TestMultiExecEmpty(c *C) { + multi, err := t.client.MultiClient() + c.Assert(err, IsNil) + defer func() { + c.Assert(multi.Close(), IsNil) + }() + + reqs, err := multi.Exec(func() {}) + c.Assert(err, IsNil) + c.Assert(reqs, HasLen, 0) + + ping := multi.Ping() + c.Check(ping.Err(), IsNil) + c.Check(ping.Val(), Equals, "PONG") +} + func (t *RedisTest) TestMultiExecOnEmptyQueue(c *C) { multi, err := t.client.MultiClient() c.Assert(err, IsNil)