Skip to content

Commit

Permalink
tests: refine test(to test the failure case)
Browse files Browse the repository at this point in the history
  • Loading branch information
YuJuncen committed Oct 8, 2021
1 parent 07a6a55 commit e9a882c
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions br/pkg/restore/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,6 @@ func (c *TestClient) SetStoresLabel(ctx context.Context, stores []uint64, labelK
return nil
}

func checkScatter(check *C, regions map[uint64]*restore.RegionInfo, scattered map[uint64]bool) {
for key := range regions {
if !scattered[key] {
check.Fatalf("region %d has not been scattered: %#v", key, regions[key])
}
}
}

type assertRetryNotThanBackoffer struct {
max int
already int
Expand Down Expand Up @@ -245,16 +237,15 @@ func (b *assertRetryNotThanBackoffer) Attempt() int {
}

func TestScatterFinishInTime(t *testing.T) {
t.Parallel()
client := initTestClient()
ranges := initRanges()
rewriteRules := initRewriteRules()
regionSplitter := restore.NewRegionSplitter(client)

ctx := context.Background()
err := regionSplitter.Split(ctx, ranges, rewriteRules, func(key [][]byte) {})
if err != nil {
require.Nil(t, err)
}
require.NoError(t, err)
regions := client.GetAllRegions()
if !validateRegions(regions) {
for _, region := range regions {
Expand Down Expand Up @@ -291,40 +282,47 @@ func TestScatterFinishInTime(t *testing.T) {
// expected regions after split:
// [, aay), [aay, bba), [bba, bbf), [bbf, bbh), [bbh, bbj),
// [bbj, cca), [cca, xxe), [xxe, xxz), [xxz, )
func (s *testRangeSuite) TestSplitAndScatter(c *C) {
func TestSplitAndScatter(t *testing.T) {
t.Parallel()
client := initTestClient()
ranges := initRanges()
rewriteRules := initRewriteRules()
regionSplitter := restore.NewRegionSplitter(client)

ctx := context.Background()
err := regionSplitter.Split(ctx, ranges, rewriteRules, func(key [][]byte) {})
if err != nil {
c.Assert(err, IsNil, Commentf("split regions failed: %v", err))
}
require.NoError(t, err)
regions := client.GetAllRegions()
if !validateRegions(regions) {
for _, region := range regions {
c.Logf("region: %v\n", region.Region)
t.Logf("region: %v\n", region.Region)
}
c.Log("get wrong result")
c.Fail()
t.Log("get wrong result")
t.Fail()
}
regionInfos := make([]*restore.RegionInfo, 0, len(regions))
for _, info := range regions {
regionInfos = append(regionInfos, info)
}
scattered := map[uint64]bool{}
const alwaysFailedRegionID = 1
client.injectInScatter = func(regionInfo *restore.RegionInfo) error {
if _, ok := scattered[regionInfo.Region.Id]; !ok {
if _, ok := scattered[regionInfo.Region.Id]; !ok || regionInfo.Region.Id == alwaysFailedRegionID {
scattered[regionInfo.Region.Id] = false
return status.Errorf(codes.Unknown, "region %d is not fully replicated", regionInfo.Region.Id)
}
scattered[regionInfo.Region.Id] = true
return nil
}
regionSplitter.ScatterRegions(ctx, regionInfos)
checkScatter(c, client.GetAllRegions(), scattered)
for key := range regions {
if key == alwaysFailedRegionID {
require.Falsef(t, scattered[key], "always failed region %d was scattered successfully", key)
} else if !scattered[key] {
t.Fatalf("region %d has not been scattered: %#v", key, regions[key])
}
}

}

// region: [, aay), [aay, bba), [bba, bbh), [bbh, cca), [cca, )
Expand Down

0 comments on commit e9a882c

Please sign in to comment.