github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/kv/regionlock/utils_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 regionlock 15 16 import ( 17 "testing" 18 19 "github.com/pingcap/kvproto/pkg/metapb" 20 "github.com/pingcap/tiflow/cdc/processor/tablepb" 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestCheckRegionsLeftCover(t *testing.T) { 25 t.Parallel() 26 27 cases := []struct { 28 regions []*metapb.Region 29 span tablepb.Span 30 cover bool 31 }{ 32 { 33 regions: []*metapb.Region{}, 34 span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}}, cover: false, 35 }, 36 {regions: []*metapb.Region{ 37 {StartKey: nil, EndKey: nil}, 38 }, span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}}, cover: true}, 39 {regions: []*metapb.Region{ 40 {StartKey: []byte{1}, EndKey: []byte{2}}, 41 }, span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}}, cover: true}, 42 {regions: []*metapb.Region{ 43 {StartKey: []byte{0}, EndKey: []byte{4}}, 44 }, span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}}, cover: true}, 45 {regions: []*metapb.Region{ 46 {StartKey: []byte{1}, EndKey: []byte{2}}, 47 {StartKey: []byte{2}, EndKey: []byte{3}}, 48 }, span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{3}}, cover: true}, 49 {regions: []*metapb.Region{ 50 {StartKey: []byte{1}, EndKey: []byte{2}}, 51 {StartKey: []byte{3}, EndKey: []byte{4}}, 52 }, span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{4}}, cover: false}, 53 {regions: []*metapb.Region{ 54 {StartKey: []byte{1}, EndKey: []byte{2}}, 55 {StartKey: []byte{2}, EndKey: []byte{3}}, 56 }, span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{4}}, cover: true}, 57 {regions: []*metapb.Region{ 58 {StartKey: []byte{2}, EndKey: []byte{3}}, 59 }, span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{3}}, cover: false}, 60 } 61 62 for _, tc := range cases { 63 require.Equal(t, tc.cover, CheckRegionsLeftCover(tc.regions, tc.span)) 64 } 65 } 66 67 func TestCutRegionsLeftCoverSpan(t *testing.T) { 68 t.Parallel() 69 70 cases := []struct { 71 regions []*metapb.Region 72 span tablepb.Span 73 covered []*metapb.Region 74 }{ 75 { 76 regions: []*metapb.Region{}, 77 span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}}, 78 covered: nil, 79 }, 80 { 81 regions: []*metapb.Region{{StartKey: nil, EndKey: nil}}, 82 span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}}, 83 covered: []*metapb.Region{{StartKey: nil, EndKey: nil}}, 84 }, 85 { 86 regions: []*metapb.Region{ 87 {StartKey: []byte{1}, EndKey: []byte{2}}, 88 }, 89 span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}}, 90 covered: []*metapb.Region{ 91 {StartKey: []byte{1}, EndKey: []byte{2}}, 92 }, 93 }, 94 { 95 regions: []*metapb.Region{ 96 {StartKey: []byte{0}, EndKey: []byte{4}}, 97 }, 98 span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}}, 99 covered: []*metapb.Region{ 100 {StartKey: []byte{0}, EndKey: []byte{4}}, 101 }, 102 }, 103 { 104 regions: []*metapb.Region{ 105 {StartKey: []byte{1}, EndKey: []byte{2}}, 106 {StartKey: []byte{2}, EndKey: []byte{3}}, 107 }, 108 span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{3}}, 109 covered: []*metapb.Region{ 110 {StartKey: []byte{1}, EndKey: []byte{2}}, 111 {StartKey: []byte{2}, EndKey: []byte{3}}, 112 }, 113 }, 114 { 115 regions: []*metapb.Region{ 116 {StartKey: []byte{1}, EndKey: []byte{2}}, 117 {StartKey: []byte{3}, EndKey: []byte{4}}, 118 }, 119 span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{4}}, 120 covered: []*metapb.Region{ 121 {StartKey: []byte{1}, EndKey: []byte{2}}, 122 }, 123 }, 124 { 125 regions: []*metapb.Region{ 126 {StartKey: []byte{1}, EndKey: []byte{2}}, 127 {StartKey: []byte{2}, EndKey: []byte{3}}, 128 }, 129 span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{4}}, 130 covered: []*metapb.Region{ 131 {StartKey: []byte{1}, EndKey: []byte{2}}, 132 {StartKey: []byte{2}, EndKey: []byte{3}}, 133 }, 134 }, 135 { 136 regions: []*metapb.Region{ 137 {StartKey: []byte{2}, EndKey: []byte{3}}, 138 }, 139 span: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{3}}, 140 covered: nil, 141 }, 142 } 143 144 for _, tc := range cases { 145 require.Equal(t, tc.covered, CutRegionsLeftCoverSpan(tc.regions, tc.span)) 146 } 147 }