Skip to content

Commit

Permalink
Merge pull request redis#742 from go-redis/fix/with-context-wrap-process
Browse files Browse the repository at this point in the history
Fix WithContext followed by WrapProcess
vmihailenco authored Mar 13, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 2fb336f + 20363d1 commit 17b6cf1
Showing 2 changed files with 50 additions and 22 deletions.
40 changes: 23 additions & 17 deletions redis.go
Original file line number Diff line number Diff line change
@@ -96,16 +96,7 @@ func (c *baseClient) initConn(cn *pool.Conn) error {
return nil
}

// Temp client to initialize connection.
conn := &Conn{
baseClient: baseClient{
opt: c.opt,
connPool: pool.NewSingleConnPool(cn),
},
}
conn.baseClient.init()
conn.statefulCmdable.setProcessor(conn.Process)

conn := newConn(c.opt, cn)
_, err := conn.Pipelined(func(pipe Pipeliner) error {
if c.opt.Password != "" {
pipe.Auth(c.opt.Password)
@@ -351,22 +342,24 @@ type Client struct {
ctx context.Context
}

func newClient(opt *Options, pool pool.Pooler) *Client {
// NewClient returns a client to the Redis Server specified by Options.
func NewClient(opt *Options) *Client {
opt.init()

c := Client{
baseClient: baseClient{
opt: opt,
connPool: pool,
connPool: newConnPool(opt),
},
}
c.baseClient.init()
c.cmdable.setProcessor(c.Process)
c.init()

return &c
}

// NewClient returns a client to the Redis Server specified by Options.
func NewClient(opt *Options) *Client {
opt.init()
return newClient(opt, newConnPool(opt))
func (c *Client) init() {
c.cmdable.setProcessor(c.Process)
}

func (c *Client) Context() context.Context {
@@ -387,6 +380,7 @@ func (c *Client) WithContext(ctx context.Context) *Client {

func (c *Client) copy() *Client {
cp := *c
cp.init()
return &cp
}

@@ -467,6 +461,18 @@ type Conn struct {
statefulCmdable
}

func newConn(opt *Options, cn *pool.Conn) *Conn {
c := Conn{
baseClient: baseClient{
opt: opt,
connPool: pool.NewSingleConnPool(cn),
},
}
c.baseClient.init()
c.statefulCmdable.setProcessor(c.Process)
return &c
}

func (c *Conn) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
return c.Pipeline().Pipelined(fn)
}
32 changes: 27 additions & 5 deletions redis_test.go
Original file line number Diff line number Diff line change
@@ -228,18 +228,40 @@ var _ = Describe("Client", func() {
})

It("should call WrapProcess", func() {
var wrapperFnCalled bool
var fnCalled bool

client.WrapProcess(func(oldProcess func(redis.Cmder) error) func(redis.Cmder) error {
client.WrapProcess(func(old func(redis.Cmder) error) func(redis.Cmder) error {
return func(cmd redis.Cmder) error {
wrapperFnCalled = true
return oldProcess(cmd)
fnCalled = true
return old(cmd)
}
})

Expect(client.Ping().Err()).NotTo(HaveOccurred())
Expect(fnCalled).To(BeTrue())
})

It("should call WrapProcess after WithContext", func() {
var fn1Called, fn2Called bool

client.WrapProcess(func(old func(cmd redis.Cmder) error) func(cmd redis.Cmder) error {
return func(cmd redis.Cmder) error {
fn1Called = true
return old(cmd)
}
})

client2 := client.WithContext(client.Context())
client2.WrapProcess(func(old func(cmd redis.Cmder) error) func(cmd redis.Cmder) error {
return func(cmd redis.Cmder) error {
fn2Called = true
return old(cmd)
}
})

Expect(wrapperFnCalled).To(BeTrue())
Expect(client2.Ping().Err()).NotTo(HaveOccurred())
Expect(fn2Called).To(BeTrue())
Expect(fn1Called).To(BeTrue())
})
})

0 comments on commit 17b6cf1

Please sign in to comment.