Commit 2342ba7f authored by Randy's avatar Randy

code block format and modify comment

parent fabd3330
...@@ -46,17 +46,10 @@ var ( ...@@ -46,17 +46,10 @@ var (
// NewConfigClient create new Client // NewConfigClient create new Client
func NewConfigClient(opts ...Option) *Client { func NewConfigClient(opts ...Option) *Client {
options := &Options{ newClient, err := NewConfigClientWithErr(opts...)
Heartbeat: 1, // default Heartbeat
}
for _, opt := range opts {
opt(options)
}
newClient, err := NewClient(options.Name, options.Endpoints, options.Timeout, options.Heartbeat)
if err != nil { if err != nil {
log.Printf("new etcd client (Name{%s}, etcd addresses{%v}, Timeout{%d}) = error{%v}", log.Printf("new etcd client = error{%v}", err)
options.Name, options.Endpoints, options.Timeout, err)
} }
return newClient return newClient
} }
...@@ -237,7 +230,6 @@ func (c *Client) GetEndPoints() []string { ...@@ -237,7 +230,6 @@ func (c *Client) GetEndPoints() []string {
// if k not exist will put k/v in etcd, otherwise return ErrCompareFail // if k not exist will put k/v in etcd, otherwise return ErrCompareFail
func (c *Client) create(k string, v string, opts ...clientv3.OpOption) error { func (c *Client) create(k string, v string, opts ...clientv3.OpOption) error {
rawClient := c.GetRawClient() rawClient := c.GetRawClient()
if rawClient == nil { if rawClient == nil {
return ErrNilETCDV3Client return ErrNilETCDV3Client
} }
...@@ -259,7 +251,6 @@ func (c *Client) create(k string, v string, opts ...clientv3.OpOption) error { ...@@ -259,7 +251,6 @@ func (c *Client) create(k string, v string, opts ...clientv3.OpOption) error {
// if k in bulk insertion not exist all, then put all k/v in etcd, otherwise return error // if k in bulk insertion not exist all, then put all k/v in etcd, otherwise return error
func (c *Client) batchCreate(kList []string, vList []string, opts ...clientv3.OpOption) error { func (c *Client) batchCreate(kList []string, vList []string, opts ...clientv3.OpOption) error {
rawClient := c.GetRawClient() rawClient := c.GetRawClient()
if rawClient == nil { if rawClient == nil {
return ErrNilETCDV3Client return ErrNilETCDV3Client
} }
...@@ -293,10 +284,9 @@ func (c *Client) batchCreate(kList []string, vList []string, opts ...clientv3.Op ...@@ -293,10 +284,9 @@ func (c *Client) batchCreate(kList []string, vList []string, opts ...clientv3.Op
return nil return nil
} }
// put k v into etcd // put k/v in etcd, if fail return error
func (c *Client) put(k string, v string, opts ...clientv3.OpOption) error { func (c *Client) put(k string, v string, opts ...clientv3.OpOption) error {
rawClient := c.GetRawClient() rawClient := c.GetRawClient()
if rawClient == nil { if rawClient == nil {
return ErrNilETCDV3Client return ErrNilETCDV3Client
} }
...@@ -305,11 +295,9 @@ func (c *Client) put(k string, v string, opts ...clientv3.OpOption) error { ...@@ -305,11 +295,9 @@ func (c *Client) put(k string, v string, opts ...clientv3.OpOption) error {
return err return err
} }
// if k not exist will put k/v in etcd // put k/v in etcd when ModRevision equal with rev, if not return ErrCompareFail or other err
// if k is already exist in etcd and revision = revision, replace it, otherwise return ErrCompareFail
func (c *Client) updateWithRev(k string, v string, rev int64, opts ...clientv3.OpOption) error { func (c *Client) updateWithRev(k string, v string, rev int64, opts ...clientv3.OpOption) error {
rawClient := c.GetRawClient() rawClient := c.GetRawClient()
if rawClient == nil { if rawClient == nil {
return ErrNilETCDV3Client return ErrNilETCDV3Client
} }
...@@ -330,7 +318,6 @@ func (c *Client) updateWithRev(k string, v string, rev int64, opts ...clientv3.O ...@@ -330,7 +318,6 @@ func (c *Client) updateWithRev(k string, v string, rev int64, opts ...clientv3.O
func (c *Client) delete(k string) error { func (c *Client) delete(k string) error {
rawClient := c.GetRawClient() rawClient := c.GetRawClient()
if rawClient == nil { if rawClient == nil {
return ErrNilETCDV3Client return ErrNilETCDV3Client
} }
...@@ -341,7 +328,6 @@ func (c *Client) delete(k string) error { ...@@ -341,7 +328,6 @@ func (c *Client) delete(k string) error {
func (c *Client) get(k string) (string, error) { func (c *Client) get(k string) (string, error) {
rawClient := c.GetRawClient() rawClient := c.GetRawClient()
if rawClient == nil { if rawClient == nil {
return "", ErrNilETCDV3Client return "", ErrNilETCDV3Client
} }
...@@ -361,7 +347,6 @@ func (c *Client) get(k string) (string, error) { ...@@ -361,7 +347,6 @@ func (c *Client) get(k string) (string, error) {
// getValAndRev get value and revision // getValAndRev get value and revision
func (c *Client) getValAndRev(k string) (string, int64, error) { func (c *Client) getValAndRev(k string) (string, int64, error) {
rawClient := c.GetRawClient() rawClient := c.GetRawClient()
if rawClient == nil { if rawClient == nil {
return "", ErrRevision, ErrNilETCDV3Client return "", ErrRevision, ErrNilETCDV3Client
} }
...@@ -381,7 +366,6 @@ func (c *Client) getValAndRev(k string) (string, int64, error) { ...@@ -381,7 +366,6 @@ func (c *Client) getValAndRev(k string) (string, int64, error) {
// CleanKV delete all key and value // CleanKV delete all key and value
func (c *Client) CleanKV() error { func (c *Client) CleanKV() error {
rawClient := c.GetRawClient() rawClient := c.GetRawClient()
if rawClient == nil { if rawClient == nil {
return ErrNilETCDV3Client return ErrNilETCDV3Client
} }
...@@ -393,7 +377,6 @@ func (c *Client) CleanKV() error { ...@@ -393,7 +377,6 @@ func (c *Client) CleanKV() error {
// GetChildren return node children // GetChildren return node children
func (c *Client) GetChildren(k string) ([]string, []string, error) { func (c *Client) GetChildren(k string) ([]string, []string, error) {
rawClient := c.GetRawClient() rawClient := c.GetRawClient()
if rawClient == nil { if rawClient == nil {
return nil, nil, ErrNilETCDV3Client return nil, nil, ErrNilETCDV3Client
} }
...@@ -419,7 +402,6 @@ func (c *Client) GetChildren(k string) ([]string, []string, error) { ...@@ -419,7 +402,6 @@ func (c *Client) GetChildren(k string) ([]string, []string, error) {
// watchWithOption watch // watchWithOption watch
func (c *Client) watchWithOption(k string, opts ...clientv3.OpOption) (clientv3.WatchChan, error) { func (c *Client) watchWithOption(k string, opts ...clientv3.OpOption) (clientv3.WatchChan, error) {
rawClient := c.GetRawClient() rawClient := c.GetRawClient()
if rawClient == nil { if rawClient == nil {
return nil, ErrNilETCDV3Client return nil, ErrNilETCDV3Client
} }
...@@ -429,7 +411,6 @@ func (c *Client) watchWithOption(k string, opts ...clientv3.OpOption) (clientv3. ...@@ -429,7 +411,6 @@ func (c *Client) watchWithOption(k string, opts ...clientv3.OpOption) (clientv3.
func (c *Client) keepAliveKV(k string, v string) error { func (c *Client) keepAliveKV(k string, v string) error {
rawClient := c.GetRawClient() rawClient := c.GetRawClient()
if rawClient == nil { if rawClient == nil {
return ErrNilETCDV3Client return ErrNilETCDV3Client
} }
......
...@@ -118,10 +118,10 @@ func (suite *ClientTestSuite) TearDownSuite() { ...@@ -118,10 +118,10 @@ func (suite *ClientTestSuite) TearDownSuite() {
} }
func (suite *ClientTestSuite) setUpClient() *Client { func (suite *ClientTestSuite) setUpClient() *Client {
c, err := NewClient(suite.etcdConfig.name, c, err := NewConfigClientWithErr(WithName(suite.etcdConfig.name),
suite.etcdConfig.endpoints, WithEndpoints(suite.etcdConfig.endpoints...),
suite.etcdConfig.timeout, WithTimeout(suite.etcdConfig.timeout),
suite.etcdConfig.heartbeat) WithHeartbeat(suite.etcdConfig.heartbeat))
if err != nil { if err != nil {
suite.T().Fatal(err) suite.T().Fatal(err)
} }
...@@ -269,6 +269,11 @@ func (suite *ClientTestSuite) TestBatchClientGetValAndRevKV() { ...@@ -269,6 +269,11 @@ func (suite *ClientTestSuite) TestBatchClientGetValAndRevKV() {
t.Fatal(err) t.Fatal(err)
} }
err = c.Update(k, k)
if err != nil {
t.Fatal(err)
}
if value != expect { if value != expect {
t.Fatalf("expect %v but get %v", expect, value) t.Fatalf("expect %v but get %v", expect, value)
} }
...@@ -368,7 +373,7 @@ func (suite *ClientTestSuite) TestClientWatch() { ...@@ -368,7 +373,7 @@ func (suite *ClientTestSuite) TestClientWatch() {
c.Close() c.Close()
}() }()
wc, err := c.watchWithOption(prefix) wc, err := c.WatchWithOption(prefix)
if err != nil { if err != nil {
assert.Error(t, err) assert.Error(t, err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment