Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
*: fix import cycle in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytm committed May 9, 2020
1 parent 3bc8924 commit e0ce147
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 32 deletions.
8 changes: 5 additions & 3 deletions pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ func (bc *Client) SaveBackupMeta(ctx context.Context, ddlJobs []*model.Job) erro
return bc.storage.Write(ctx, utils.MetaFile, backupMetaData)
}

func buildTableRanges(tbl *model.TableInfo) ([]kv.KeyRange, error) {
// BuildTableRanges returns the key ranges encompassing the entire table,
// and its partitions if exists.
func BuildTableRanges(tbl *model.TableInfo) ([]kv.KeyRange, error) {
pis := tbl.GetPartitionInfo()
if pis == nil {
// Short path, no partition.
Expand Down Expand Up @@ -285,7 +287,7 @@ func BuildBackupRangeAndSchema(
}
backupSchemas.pushPending(schema, dbInfo.Name.L, tableInfo.Name.L)

tableRanges, err := buildTableRanges(tableInfo)
tableRanges, err := BuildTableRanges(tableInfo)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -897,7 +899,7 @@ func (bc *Client) CollectChecksums() ([]Checksum, error) {

// CompleteMeta wait response of admin checksum from TiDB to complete backup meta.
func (bc *Client) CompleteMeta(backupSchemas *Schemas) error {
schemas, err := backupSchemas.finishTableChecksum()
schemas, err := backupSchemas.FinishTableChecksum()
if err != nil {
return err
}
Expand Down
26 changes: 14 additions & 12 deletions pkg/backup/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.

package backup
package backup_test

import (
"context"
Expand All @@ -10,20 +10,23 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/parser/model"
pd "github.com/pingcap/pd/v4/client"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util/codec"

"github.com/pingcap/br/pkg/backup"
"github.com/pingcap/br/pkg/conn"
)

type testBackup struct {
ctx context.Context
cancel context.CancelFunc

backupClient *Client
mockPDClient pd.Client
backupClient *backup.Client
}

var _ = Suite(&testBackup{})
Expand All @@ -33,15 +36,14 @@ func TestT(t *testing.T) {
}

func (r *testBackup) SetUpSuite(c *C) {
mockPDClient := mocktikv.NewPDClient(mocktikv.NewCluster())
r.mockPDClient = mocktikv.NewPDClient(mocktikv.NewCluster())
r.ctx, r.cancel = context.WithCancel(context.Background())
mockMgr := &conn.Mgr{}
mockMgr.SetPDClient(mockPDClient)
mockMgr.SetPDClient(r.mockPDClient)
mockMgr.SetPDHTTP([]string{"test"}, nil)
r.backupClient = &Client{
clusterID: mockPDClient.GetClusterID(r.ctx),
mgr: mockMgr,
}
var err error
r.backupClient, err = backup.NewBackupClient(r.ctx, mockMgr)
c.Assert(err, IsNil)
}

func (r *testBackup) TestGetTS(c *C) {
Expand Down Expand Up @@ -81,10 +83,10 @@ func (r *testBackup) TestGetTS(c *C) {
c.Assert(err, ErrorMatches, "backup ts overflow.*")

// timeago = "10h" exceed GCSafePoint
p, l, err := r.backupClient.mgr.GetPDClient().GetTS(r.ctx)
p, l, err := r.mockPDClient.GetTS(r.ctx)
c.Assert(err, IsNil)
now := oracle.ComposeTS(p, l)
_, err = r.backupClient.mgr.GetPDClient().UpdateGCSafePoint(r.ctx, now)
_, err = r.mockPDClient.UpdateGCSafePoint(r.ctx, now)
c.Assert(err, IsNil)
_, err = r.backupClient.GetTS(r.ctx, 10*time.Hour, 0)
c.Assert(err, ErrorMatches, "GC safepoint [0-9]+ exceed TS [0-9]+")
Expand Down Expand Up @@ -125,13 +127,13 @@ func (r *testBackup) TestBuildTableRange(c *C) {
tbl.Partition.Definitions = append(tbl.Partition.Definitions,
model.PartitionDefinition{ID: id})
}
ranges, err := buildTableRanges(tbl)
ranges, err := backup.BuildTableRanges(tbl)
c.Assert(err, IsNil)
c.Assert(ranges, DeepEquals, cs.trs)
}

tbl := &model.TableInfo{ID: 7}
ranges, err := buildTableRanges(tbl)
ranges, err := backup.BuildTableRanges(tbl)
c.Assert(err, IsNil)
c.Assert(ranges, DeepEquals, []kv.KeyRange{
{StartKey: tablecodec.EncodeRowKey(7, low), EndKey: tablecodec.EncodeRowKey(7, high)},
Expand Down
11 changes: 6 additions & 5 deletions pkg/backup/safe_point_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.

package backup
package backup_test

import (
"context"
Expand All @@ -10,6 +10,7 @@ import (
pd "github.com/pingcap/pd/v4/client"
"github.com/pingcap/tidb/util/testleak"

"github.com/pingcap/br/pkg/backup"
"github.com/pingcap/br/pkg/mock"
)

Expand All @@ -36,19 +37,19 @@ func (s *testSafePointSuite) TestCheckGCSafepoint(c *C) {
ctx := context.Background()
pdClient := &mockSafePoint{Client: s.mock.PDClient, safepoint: 2333}
{
err := CheckGCSafePoint(ctx, pdClient, 2333+1)
err := backup.CheckGCSafePoint(ctx, pdClient, 2333+1)
c.Assert(err, IsNil)
}
{
err := CheckGCSafePoint(ctx, pdClient, 2333)
err := backup.CheckGCSafePoint(ctx, pdClient, 2333)
c.Assert(err, NotNil)
}
{
err := CheckGCSafePoint(ctx, pdClient, 2333-1)
err := backup.CheckGCSafePoint(ctx, pdClient, 2333-1)
c.Assert(err, NotNil)
}
{
err := CheckGCSafePoint(ctx, pdClient, 0)
err := backup.CheckGCSafePoint(ctx, pdClient, 0)
c.Assert(err, ErrorMatches, "GC safepoint 2333 exceed TS 0")
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/backup/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ func (pending *Schemas) Start(
}()
}

func (pending *Schemas) finishTableChecksum() ([]*backup.Schema, error) {
// FinishTableChecksum waits until all schemas' checksums are verified.
func (pending *Schemas) FinishTableChecksum() ([]*backup.Schema, error) {
schemas := make([]*backup.Schema, 0, len(pending.schemas))
for {
select {
Expand Down
17 changes: 9 additions & 8 deletions pkg/backup/schema_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.

package backup
package backup_test

import (
"context"
Expand All @@ -12,6 +12,7 @@ import (
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"

"github.com/pingcap/br/pkg/backup"
"github.com/pingcap/br/pkg/mock"
)

Expand Down Expand Up @@ -60,7 +61,7 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) {
DoTables: []*filter.Table{{Schema: "test", Name: "t1"}},
})
c.Assert(err, IsNil)
_, backupSchemas, err := BuildBackupRangeAndSchema(
_, backupSchemas, err := backup.BuildBackupRangeAndSchema(
s.mock.Domain, s.mock.Storage, testFilter, math.MaxUint64)
c.Assert(err, IsNil)
c.Assert(backupSchemas, IsNil)
Expand All @@ -70,15 +71,15 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) {
DoTables: []*filter.Table{{Schema: "foo", Name: "t1"}},
})
c.Assert(err, IsNil)
_, backupSchemas, err = BuildBackupRangeAndSchema(
_, backupSchemas, err = backup.BuildBackupRangeAndSchema(
s.mock.Domain, s.mock.Storage, fooFilter, math.MaxUint64)
c.Assert(err, IsNil)
c.Assert(backupSchemas, IsNil)

// Empty database.
noFilter, err := filter.New(false, &filter.Rules{})
c.Assert(err, IsNil)
_, backupSchemas, err = BuildBackupRangeAndSchema(
_, backupSchemas, err = backup.BuildBackupRangeAndSchema(
s.mock.Domain, s.mock.Storage, noFilter, math.MaxUint64)
c.Assert(err, IsNil)
c.Assert(backupSchemas, IsNil)
Expand All @@ -88,13 +89,13 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) {
tk.MustExec("create table t1 (a int);")
tk.MustExec("insert into t1 values (10);")

_, backupSchemas, err = BuildBackupRangeAndSchema(
_, backupSchemas, err = backup.BuildBackupRangeAndSchema(
s.mock.Domain, s.mock.Storage, testFilter, math.MaxUint64)
c.Assert(err, IsNil)
c.Assert(backupSchemas.Len(), Equals, 1)
updateCh := new(simpleProgress)
backupSchemas.Start(context.Background(), s.mock.Storage, math.MaxUint64, 1, updateCh)
schemas, err := backupSchemas.finishTableChecksum()
schemas, err := backupSchemas.FinishTableChecksum()
c.Assert(updateCh.get(), Equals, int64(1))
c.Assert(err, IsNil)
c.Assert(len(schemas), Equals, 1)
Expand All @@ -108,13 +109,13 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) {
tk.MustExec("insert into t2 values (10);")
tk.MustExec("insert into t2 values (11);")

_, backupSchemas, err = BuildBackupRangeAndSchema(
_, backupSchemas, err = backup.BuildBackupRangeAndSchema(
s.mock.Domain, s.mock.Storage, noFilter, math.MaxUint64)
c.Assert(err, IsNil)
c.Assert(backupSchemas.Len(), Equals, 2)
updateCh.reset()
backupSchemas.Start(context.Background(), s.mock.Storage, math.MaxUint64, 2, updateCh)
schemas, err = backupSchemas.finishTableChecksum()
schemas, err = backupSchemas.FinishTableChecksum()
c.Assert(updateCh.get(), Equals, int64(2))
c.Assert(err, IsNil)
c.Assert(len(schemas), Equals, 2)
Expand Down
14 changes: 11 additions & 3 deletions pkg/mock/mock_cluster_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.

package mock
package mock_test

import (
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/util/testleak"

"github.com/pingcap/br/pkg/mock"
)

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

var _ = Suite(&testClusterSuite{})

type testClusterSuite struct {
mock *Cluster
mock *mock.Cluster
}

func (s *testClusterSuite) SetUpSuite(c *C) {
var err error
s.mock, err = NewCluster()
s.mock, err = mock.NewCluster()
c.Assert(err, IsNil)
}

Expand Down

0 comments on commit e0ce147

Please sign in to comment.