Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(ticdc): Migrate test cases to testify for pkg/etcd #4219

Merged
merged 5 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions pkg/etcd/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ package etcd

import (
"context"
"io/ioutil"
"os"
"testing"
"time"

"github.com/benbjohnson/clock"
"github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/tiflow/pkg/util/testleak"
"github.com/stretchr/testify/require"
"go.etcd.io/etcd/clientv3"
)

type clientSuite struct{}

var _ = check.Suite(&clientSuite{})

type mockClient struct {
clientv3.KV
getOK bool
Expand Down Expand Up @@ -68,8 +66,7 @@ func (m mockWatcher) RequestProgress(ctx context.Context) error {
return nil
}

func (s *clientSuite) TestRetry(c *check.C) {
defer testleak.AfterTest(c)()
func TestRetry(t *testing.T) {
originValue := maxTries
// to speedup the test
maxTries = 2
Expand All @@ -78,46 +75,52 @@ func (s *clientSuite) TestRetry(c *check.C) {
cli.KV = &mockClient{}
retrycli := Wrap(cli, nil)
get, err := retrycli.Get(context.TODO(), "")
c.Assert(err, check.IsNil)
c.Assert(get, check.NotNil)

require.Nil(t, err)
require.NotNil(t, get)

_, err = retrycli.Put(context.TODO(), "", "")
c.Assert(err, check.NotNil)
c.Assert(errors.Cause(err), check.ErrorMatches, "mock error", check.Commentf("err:%v", err.Error()))
require.NotNil(t, err)
require.Containsf(t, errors.Cause(err).Error(), "mock error", "err:%v", err.Error())
maxTries = originValue
}

func (s *etcdSuite) TestDelegateLease(c *check.C) {
defer testleak.AfterTest(c)()
defer s.TearDownTest(c)
func TestDelegateLease(t *testing.T) {
ctx := context.Background()
dir, err := ioutil.TempDir("", "delegate-lease-test")
require.Nil(t, err)
url, server, err := SetupEmbedEtcd(dir)
defer func() {
server.Close()
os.RemoveAll(dir)
}()
require.Nil(t, err)
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{s.clientURL.String()},
Endpoints: []string{url.String()},
DialTimeout: 3 * time.Second,
})
c.Assert(err, check.IsNil)
require.Nil(t, err)
defer cli.Close()

ttl := int64(10)
lease, err := cli.Grant(ctx, ttl)
c.Assert(err, check.IsNil)
require.Nil(t, err)

ttlResp, err := cli.TimeToLive(ctx, lease.ID)
c.Assert(err, check.IsNil)
c.Assert(ttlResp.GrantedTTL, check.Equals, ttl)
c.Assert(ttlResp.TTL, check.Less, ttl)
c.Assert(ttlResp.TTL, check.Greater, int64(0))
require.Nil(t, err)
require.Equal(t, ttlResp.GrantedTTL, ttl)
require.Less(t, ttlResp.TTL, ttl)
require.Greater(t, ttlResp.TTL, int64(0))

_, err = cli.Revoke(ctx, lease.ID)
c.Assert(err, check.IsNil)
require.Nil(t, err)
ttlResp, err = cli.TimeToLive(ctx, lease.ID)
c.Assert(err, check.IsNil)
c.Assert(ttlResp.TTL, check.Equals, int64(-1))
require.Nil(t, err)
require.Equal(t, ttlResp.TTL, int64(-1))
}

// test no data lost when WatchCh blocked
func (s *clientSuite) TestWatchChBlocked(c *check.C) {
defer testleak.AfterTest(c)()
func TestWatchChBlocked(t *testing.T) {
cli := clientv3.NewCtxClient(context.TODO())
resetCount := 0
requestCount := 0
Expand Down Expand Up @@ -168,19 +171,17 @@ func (s *clientSuite) TestWatchChBlocked(c *check.C) {
}
}

c.Check(sentRes, check.DeepEquals, receivedRes)
require.Equal(t, sentRes, receivedRes)
// make sure watchCh has been reset since timeout
c.Assert(*watcher.resetCount > 1, check.IsTrue)
require.True(t, *watcher.resetCount > 1)
// make sure RequestProgress has been call since timeout
c.Assert(*watcher.requestCount > 1, check.IsTrue)
require.True(t, *watcher.requestCount > 1)
// make sure etcdRequestProgressDuration is less than etcdWatchChTimeoutDuration
c.Assert(etcdRequestProgressDuration, check.Less, etcdWatchChTimeoutDuration)
require.Less(t, etcdRequestProgressDuration, etcdWatchChTimeoutDuration)
}

// test no data lost when OutCh blocked
func (s *clientSuite) TestOutChBlocked(c *check.C) {
defer testleak.AfterTest(c)()

func TestOutChBlocked(t *testing.T) {
cli := clientv3.NewCtxClient(context.TODO())
resetCount := 0
requestCount := 0
Expand Down Expand Up @@ -228,11 +229,10 @@ func (s *clientSuite) TestOutChBlocked(c *check.C) {
}
}

c.Check(sentRes, check.DeepEquals, receivedRes)
require.Equal(t, sentRes, receivedRes)
}

func (s *clientSuite) TestRevisionNotFallBack(c *check.C) {
defer testleak.AfterTest(c)()
func TestRevisionNotFallBack(t *testing.T) {
cli := clientv3.NewCtxClient(context.TODO())

resetCount := 0
Expand Down Expand Up @@ -270,9 +270,9 @@ func (s *clientSuite) TestRevisionNotFallBack(c *check.C) {
// move time forward
mockClock.Add(time.Second * 30)
// make sure watchCh has been reset since timeout
c.Assert(*watcher.resetCount > 1, check.IsTrue)
require.True(t, *watcher.resetCount > 1)
// make suer revision in WatchWitchChan does not fall back
// even if there has not any response been received from WatchCh
// while WatchCh was reset
c.Assert(*watcher.rev, check.Equals, revision)
require.Equal(t, *watcher.rev, revision)
}
Loading