Browse Source

Merge pull request #11066 from vimalk78/clientv3-fixes

clientv3: add nil checks in Client.Close()
Xiang Li 6 years ago
parent
commit
dd2b88d0ed
2 changed files with 16 additions and 2 deletions
  1. 6 2
      clientv3/client.go
  2. 10 0
      clientv3/client_test.go

+ 6 - 2
clientv3/client.go

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

+ 10 - 0
clientv3/client_test.go

@@ -156,3 +156,13 @@ func TestIsHaltErr(t *testing.T) {
 		t.Errorf("cancel on context should be Halted")
 		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)
+	}
+}