github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/etcd/client_test.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package etcd
    15  
    16  import (
    17  	"context"
    18  	"time"
    19  
    20  	"github.com/pingcap/check"
    21  	"github.com/pingcap/errors"
    22  	"github.com/pingcap/ticdc/pkg/util/testleak"
    23  	"go.etcd.io/etcd/clientv3"
    24  )
    25  
    26  type clientSuite struct {
    27  }
    28  
    29  var _ = check.Suite(&clientSuite{})
    30  
    31  type mockClient struct {
    32  	clientv3.KV
    33  	getOK bool
    34  }
    35  
    36  func (m *mockClient) Get(ctx context.Context, key string, opts ...clientv3.OpOption) (resp *clientv3.GetResponse, err error) {
    37  	if m.getOK {
    38  		m.getOK = true
    39  		return nil, errors.New("mock error")
    40  	}
    41  	return &clientv3.GetResponse{}, nil
    42  }
    43  
    44  func (m *mockClient) Put(ctx context.Context, key, val string, opts ...clientv3.OpOption) (resp *clientv3.PutResponse, err error) {
    45  	return nil, errors.New("mock error")
    46  }
    47  
    48  func (s *clientSuite) TestRetry(c *check.C) {
    49  	defer testleak.AfterTest(c)()
    50  	originValue := maxTries
    51  	// to speedup the test
    52  	maxTries = 2
    53  
    54  	cli := clientv3.NewCtxClient(context.TODO())
    55  	cli.KV = &mockClient{}
    56  	retrycli := Wrap(cli, nil)
    57  	get, err := retrycli.Get(context.TODO(), "")
    58  	c.Assert(err, check.IsNil)
    59  	c.Assert(get, check.NotNil)
    60  
    61  	_, err = retrycli.Put(context.TODO(), "", "")
    62  	c.Assert(err, check.NotNil)
    63  	c.Assert(errors.Cause(err), check.ErrorMatches, "mock error", check.Commentf("err:%v", err.Error()))
    64  	maxTries = originValue
    65  }
    66  
    67  func (s *etcdSuite) TestDelegateLease(c *check.C) {
    68  	defer testleak.AfterTest(c)()
    69  	defer s.TearDownTest(c)
    70  	ctx := context.Background()
    71  	cli, err := clientv3.New(clientv3.Config{
    72  		Endpoints:   []string{s.clientURL.String()},
    73  		DialTimeout: 3 * time.Second,
    74  	})
    75  	c.Assert(err, check.IsNil)
    76  	defer cli.Close()
    77  
    78  	ttl := int64(10)
    79  	lease, err := cli.Grant(ctx, ttl)
    80  	c.Assert(err, check.IsNil)
    81  
    82  	ttlResp, err := cli.TimeToLive(ctx, lease.ID)
    83  	c.Assert(err, check.IsNil)
    84  	c.Assert(ttlResp.GrantedTTL, check.Equals, ttl)
    85  	c.Assert(ttlResp.TTL, check.Less, ttl)
    86  	c.Assert(ttlResp.TTL, check.Greater, int64(0))
    87  
    88  	_, err = cli.Revoke(ctx, lease.ID)
    89  	c.Assert(err, check.IsNil)
    90  	ttlResp, err = cli.TimeToLive(ctx, lease.ID)
    91  	c.Assert(err, check.IsNil)
    92  	c.Assert(ttlResp.TTL, check.Equals, int64(-1))
    93  }