Convert bytes to string in Cmd.

This commit is contained in:
Vladimir Mihailenco 2016-04-12 19:41:56 +03:00
parent 889409de38
commit 818785577e
3 changed files with 16 additions and 7 deletions

View File

@ -165,7 +165,12 @@ func (cmd *Cmd) readReply(cn *pool.Conn) error {
cmd.err = err cmd.err = err
return cmd.err return cmd.err
} }
if b, ok := val.([]byte); ok {
// Bytes must be copied, because underlying memory is reused.
cmd.val = string(b)
} else {
cmd.val = val cmd.val = val
}
return nil return nil
} }

View File

@ -7,7 +7,7 @@ import (
"gopkg.in/redis.v4" "gopkg.in/redis.v4"
) )
var _ = Describe("Command", func() { var _ = Describe("Cmd", func() {
var client *redis.Client var client *redis.Client
BeforeEach(func() { BeforeEach(func() {
@ -19,7 +19,7 @@ var _ = Describe("Command", func() {
Expect(client.Close()).NotTo(HaveOccurred()) Expect(client.Close()).NotTo(HaveOccurred())
}) })
It("should implement Stringer", func() { It("implements Stringer", func() {
set := client.Set("foo", "bar", 0) set := client.Set("foo", "bar", 0)
Expect(set.String()).To(Equal("SET foo bar: OK")) Expect(set.String()).To(Equal("SET foo bar: OK"))
@ -27,7 +27,7 @@ var _ = Describe("Command", func() {
Expect(get.String()).To(Equal("GET foo: bar")) Expect(get.String()).To(Equal("GET foo: bar"))
}) })
It("should have correct val/err states", func() { It("has val/err", func() {
set := client.Set("key", "hello", 0) set := client.Set("key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred()) Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK")) Expect(set.Val()).To(Equal("OK"))
@ -40,7 +40,7 @@ var _ = Describe("Command", func() {
Expect(set.Val()).To(Equal("OK")) Expect(set.Val()).To(Equal("OK"))
}) })
It("should convert strings via helpers", func() { It("has helpers", func() {
set := client.Set("key", "10", 0) set := client.Set("key", "10", 0)
Expect(set.Err()).NotTo(HaveOccurred()) Expect(set.Err()).NotTo(HaveOccurred())

View File

@ -128,11 +128,15 @@ var _ = Describe("Client", func() {
Expect(db2.Close()).NotTo(HaveOccurred()) Expect(db2.Close()).NotTo(HaveOccurred())
}) })
It("should process custom commands", func() { It("processes custom commands", func() {
cmd := redis.NewCmd("PING") cmd := redis.NewCmd("PING")
client.Process(cmd) client.Process(cmd)
// Flush buffers.
Expect(client.Echo("hello").Err()).NotTo(HaveOccurred())
Expect(cmd.Err()).NotTo(HaveOccurred()) Expect(cmd.Err()).NotTo(HaveOccurred())
Expect(cmd.Val()).To(Equal([]byte("PONG"))) Expect(cmd.Val()).To(Equal("PONG"))
}) })
It("should retry command on network error", func() { It("should retry command on network error", func() {