github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/sink/dispatcher/default_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 dispatcher
    15  
    16  import (
    17  	"github.com/pingcap/check"
    18  	"github.com/pingcap/ticdc/cdc/model"
    19  	"github.com/pingcap/ticdc/pkg/util/testleak"
    20  )
    21  
    22  type DefaultDispatcherSuite struct{}
    23  
    24  var _ = check.Suite(&DefaultDispatcherSuite{})
    25  
    26  func (s DefaultDispatcherSuite) TestDefaultDispatcher(c *check.C) {
    27  	defer testleak.AfterTest(c)()
    28  	testCases := []struct {
    29  		row             *model.RowChangedEvent
    30  		exceptPartition int32
    31  	}{
    32  		{row: &model.RowChangedEvent{
    33  			Table: &model.TableName{
    34  				Schema: "test",
    35  				Table:  "t1",
    36  			},
    37  			Columns: []*model.Column{
    38  				{
    39  					Name:  "id",
    40  					Value: 1,
    41  					Flag:  model.HandleKeyFlag | model.PrimaryKeyFlag,
    42  				},
    43  			},
    44  			IndexColumns: [][]int{{0}},
    45  		}, exceptPartition: 11},
    46  		{row: &model.RowChangedEvent{
    47  			Table: &model.TableName{
    48  				Schema: "test",
    49  				Table:  "t1",
    50  			},
    51  			Columns: []*model.Column{
    52  				{
    53  					Name:  "id",
    54  					Value: 2,
    55  					Flag:  model.HandleKeyFlag | model.PrimaryKeyFlag,
    56  				},
    57  			},
    58  			IndexColumns: [][]int{{0}},
    59  		}, exceptPartition: 1},
    60  		{row: &model.RowChangedEvent{
    61  			Table: &model.TableName{
    62  				Schema: "test",
    63  				Table:  "t1",
    64  			},
    65  			Columns: []*model.Column{
    66  				{
    67  					Name:  "id",
    68  					Value: 3,
    69  					Flag:  model.HandleKeyFlag | model.PrimaryKeyFlag,
    70  				},
    71  			},
    72  			IndexColumns: [][]int{{0}},
    73  		}, exceptPartition: 7},
    74  		{row: &model.RowChangedEvent{
    75  			Table: &model.TableName{
    76  				Schema: "test",
    77  				Table:  "t2",
    78  			},
    79  			Columns: []*model.Column{
    80  				{
    81  					Name:  "id",
    82  					Value: 1,
    83  					Flag:  model.HandleKeyFlag | model.PrimaryKeyFlag,
    84  				}, {
    85  					Name:  "a",
    86  					Value: 1,
    87  				},
    88  			},
    89  			IndexColumns: [][]int{{0}},
    90  		}, exceptPartition: 1},
    91  		{row: &model.RowChangedEvent{
    92  			Table: &model.TableName{
    93  				Schema: "test",
    94  				Table:  "t2",
    95  			},
    96  			Columns: []*model.Column{
    97  				{
    98  					Name:  "id",
    99  					Value: 2,
   100  					Flag:  model.HandleKeyFlag | model.PrimaryKeyFlag,
   101  				}, {
   102  					Name:  "a",
   103  					Value: 2,
   104  				},
   105  			},
   106  			IndexColumns: [][]int{{0}},
   107  		}, exceptPartition: 11},
   108  		{row: &model.RowChangedEvent{
   109  			Table: &model.TableName{
   110  				Schema: "test",
   111  				Table:  "t2",
   112  			},
   113  			Columns: []*model.Column{
   114  				{
   115  					Name:  "id",
   116  					Value: 3,
   117  					Flag:  model.HandleKeyFlag | model.PrimaryKeyFlag,
   118  				}, {
   119  					Name:  "a",
   120  					Value: 3,
   121  				},
   122  			},
   123  			IndexColumns: [][]int{{0}},
   124  		}, exceptPartition: 13},
   125  		{row: &model.RowChangedEvent{
   126  			Table: &model.TableName{
   127  				Schema: "test",
   128  				Table:  "t2",
   129  			},
   130  			Columns: []*model.Column{
   131  				{
   132  					Name:  "id",
   133  					Value: 3,
   134  					Flag:  model.HandleKeyFlag | model.PrimaryKeyFlag,
   135  				}, {
   136  					Name:  "a",
   137  					Value: 4,
   138  				},
   139  			},
   140  			IndexColumns: [][]int{{0}},
   141  		}, exceptPartition: 13},
   142  		{row: &model.RowChangedEvent{
   143  			Table: &model.TableName{
   144  				Schema: "test",
   145  				Table:  "t3",
   146  			},
   147  			Columns: []*model.Column{
   148  				{
   149  					Name:  "id",
   150  					Value: 1,
   151  					Flag:  model.HandleKeyFlag | model.PrimaryKeyFlag,
   152  				},
   153  				{
   154  					Name:  "a",
   155  					Value: 2,
   156  					Flag:  model.UniqueKeyFlag,
   157  				},
   158  			},
   159  			IndexColumns: [][]int{{0}, {1}},
   160  		}, exceptPartition: 3},
   161  		{row: &model.RowChangedEvent{
   162  			Table: &model.TableName{
   163  				Schema: "test",
   164  				Table:  "t3",
   165  			},
   166  			Columns: []*model.Column{
   167  				{
   168  					Name:  "id",
   169  					Value: 2,
   170  					Flag:  model.HandleKeyFlag | model.PrimaryKeyFlag,
   171  				}, {
   172  					Name:  "a",
   173  					Value: 3,
   174  					Flag:  model.UniqueKeyFlag,
   175  				},
   176  			},
   177  			IndexColumns: [][]int{{0}, {1}},
   178  		}, exceptPartition: 3},
   179  		{row: &model.RowChangedEvent{
   180  			Table: &model.TableName{
   181  				Schema: "test",
   182  				Table:  "t3",
   183  			},
   184  			Columns: []*model.Column{
   185  				{
   186  					Name:  "id",
   187  					Value: 3,
   188  					Flag:  model.HandleKeyFlag | model.PrimaryKeyFlag,
   189  				}, {
   190  					Name:  "a",
   191  					Value: 4,
   192  					Flag:  model.UniqueKeyFlag,
   193  				},
   194  			},
   195  			IndexColumns: [][]int{{0}, {1}},
   196  		}, exceptPartition: 3},
   197  	}
   198  	p := newDefaultDispatcher(16, false)
   199  	for _, tc := range testCases {
   200  		c.Assert(p.Dispatch(tc.row), check.Equals, tc.exceptPartition)
   201  	}
   202  }