Skip to content

Commit

Permalink
tests(ticdc): Migrate several test cases to testify (#4199)
Browse files Browse the repository at this point in the history
close #2901
  • Loading branch information
zhaoxinyu authored Jan 26, 2022
1 parent 9d7ae48 commit 03c5232
Show file tree
Hide file tree
Showing 18 changed files with 946 additions and 963 deletions.
24 changes: 13 additions & 11 deletions cdc/entry/schema_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,43 @@
package entry

import (
"github.com/pingcap/check"
"testing"

ticonfig "github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
timeta "github.com/pingcap/tidb/meta"
timodel "github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
"github.com/tikv/client-go/v2/oracle"
)

// SchemaTestHelper is a test helper for schema which creates an internal tidb instance to generate DDL jobs with meta information
type SchemaTestHelper struct {
c *check.C
t *testing.T
tk *testkit.TestKit
storage kv.Storage
domain *domain.Domain
}

// NewSchemaTestHelper creates a SchemaTestHelper
func NewSchemaTestHelper(c *check.C) *SchemaTestHelper {
func NewSchemaTestHelper(t *testing.T) *SchemaTestHelper {
store, err := mockstore.NewMockStore()
c.Assert(err, check.IsNil)
require.Nil(t, err)
ticonfig.UpdateGlobal(func(conf *ticonfig.Config) {
conf.AlterPrimaryKey = true
})
session.SetSchemaLease(0)
session.DisableStats4Test()
domain, err := session.BootstrapSession(store)
c.Assert(err, check.IsNil)
require.Nil(t, err)
domain.SetStatsUpdating(true)
tk := testkit.NewTestKit(c, store)
tk := testkit.NewTestKit(t, store)
return &SchemaTestHelper{
c: c,
t: t,
tk: tk,
storage: store,
domain: domain,
Expand All @@ -59,8 +61,8 @@ func NewSchemaTestHelper(c *check.C) *SchemaTestHelper {
func (s *SchemaTestHelper) DDL2Job(ddl string) *timodel.Job {
s.tk.MustExec(ddl)
jobs, err := s.GetCurrentMeta().GetLastNHistoryDDLJobs(1)
s.c.Assert(err, check.IsNil)
s.c.Assert(jobs, check.HasLen, 1)
require.Nil(s.t, err)
require.Len(s.t, jobs, 1)
return jobs[0]
}

Expand All @@ -72,7 +74,7 @@ func (s *SchemaTestHelper) Storage() kv.Storage {
// GetCurrentMeta return the current meta snapshot
func (s *SchemaTestHelper) GetCurrentMeta() *timeta.Meta {
ver, err := s.storage.CurrentVersion(oracle.GlobalTxnScope)
s.c.Assert(err, check.IsNil)
require.Nil(s.t, err)
return timeta.NewSnapshotMeta(s.storage.GetSnapshot(ver))
}

Expand Down
39 changes: 15 additions & 24 deletions cdc/owner/barrier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,41 @@ import (
"math/rand"
"testing"

"github.com/pingcap/check"
"github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/pkg/util/testleak"
"github.com/stretchr/testify/require"
)

func Test(t *testing.T) { check.TestingT(t) }

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

type barrierSuite struct{}

func (s *barrierSuite) TestBarrier(c *check.C) {
defer testleak.AfterTest(c)()
func TestBarrier(t *testing.T) {
b := newBarriers()
b.Update(ddlJobBarrier, 2)
b.Update(syncPointBarrier, 3)
b.Update(finishBarrier, 1)
tp, ts := b.Min()
c.Assert(tp, check.Equals, finishBarrier)
c.Assert(ts, check.Equals, uint64(1))
require.Equal(t, tp, finishBarrier)
require.Equal(t, ts, uint64(1))

b.Update(finishBarrier, 4)
tp, ts = b.Min()
c.Assert(tp, check.Equals, ddlJobBarrier)
c.Assert(ts, check.Equals, uint64(2))
require.Equal(t, tp, ddlJobBarrier)
require.Equal(t, ts, uint64(2))

b.Remove(ddlJobBarrier)
tp, ts = b.Min()
c.Assert(tp, check.Equals, syncPointBarrier)
c.Assert(ts, check.Equals, uint64(3))
require.Equal(t, tp, syncPointBarrier)
require.Equal(t, ts, uint64(3))

b.Update(finishBarrier, 1)
tp, ts = b.Min()
c.Assert(tp, check.Equals, finishBarrier)
c.Assert(ts, check.Equals, uint64(1))
require.Equal(t, tp, finishBarrier)
require.Equal(t, ts, uint64(1))

b.Update(ddlJobBarrier, 5)
tp, ts = b.Min()
c.Assert(tp, check.Equals, finishBarrier)
c.Assert(ts, check.Equals, uint64(1))
require.Equal(t, tp, finishBarrier)
require.Equal(t, ts, uint64(1))
}

func (s *barrierSuite) TestBarrierRandom(c *check.C) {
defer testleak.AfterTest(c)()
func TestBarrierRandom(t *testing.T) {
maxBarrierType := 50
maxBarrierTs := 1000000
b := newBarriers()
Expand Down Expand Up @@ -90,7 +81,7 @@ func (s *barrierSuite) TestBarrierRandom(c *check.C) {
}
}
tp, ts := b.Min()
c.Assert(ts, check.Equals, expectedMinTs)
c.Assert(expectedBarriers[tp], check.Equals, expectedMinTs)
require.Equal(t, ts, expectedMinTs)
require.Equal(t, expectedBarriers[tp], expectedMinTs)
}
}
Loading

0 comments on commit 03c5232

Please sign in to comment.