clientv3: add nil checks in Close()

Added nil checks in Close() for Watcher and Lease fields
Added test case
This commit is contained in:
vimalk78 2019-08-21 22:30:02 +01:00 committed by Gyuho Lee
parent d23af41bca
commit 83bf125d93
2 changed files with 16 additions and 2 deletions

View File

@ -129,8 +129,12 @@ func NewFromURLs(urls []string) (*Client, error) {
// Close shuts down the client's etcd connections.
func (c *Client) Close() error {
c.cancel()
c.Watcher.Close()
c.Lease.Close()
if c.Watcher != nil {
c.Watcher.Close()
}
if c.Lease != nil {
c.Lease.Close()
}
if c.resolverGroup != nil {
c.resolverGroup.Close()
}

View File

@ -156,3 +156,13 @@ func TestIsHaltErr(t *testing.T) {
t.Errorf("cancel on context should be Halted")
}
}
func TestCloseCtxClient(t *testing.T) {
ctx := context.Background()
c := NewCtxClient(ctx)
err := c.Close()
// Close returns ctx.toErr, a nil error means an open Done channel
if err == nil {
t.Errorf("failed to Close the client. %v", err)
}
}