github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/cyclic/replication_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 cyclic
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/pingcap/check"
    20  	"github.com/pingcap/ticdc/cdc/model"
    21  	"github.com/pingcap/ticdc/pkg/config"
    22  	"github.com/pingcap/ticdc/pkg/cyclic/mark"
    23  	"github.com/pingcap/ticdc/pkg/util/testleak"
    24  )
    25  
    26  type cyclicSuite struct{}
    27  
    28  var _ = check.Suite(&cyclicSuite{})
    29  
    30  func Test(t *testing.T) { check.TestingT(t) }
    31  
    32  func (s *cyclicSuite) TestCyclicConfig(c *check.C) {
    33  	defer testleak.AfterTest(c)()
    34  	cfg := &config.CyclicConfig{
    35  		Enable:          true,
    36  		ReplicaID:       1,
    37  		FilterReplicaID: []uint64{2, 3},
    38  	}
    39  	cyc := NewCyclic(cfg)
    40  	c.Assert(cyc, check.NotNil)
    41  	c.Assert(cyc.Enabled(), check.IsTrue)
    42  	c.Assert(cyc.ReplicaID(), check.Equals, uint64(1))
    43  	c.Assert(cyc.FilterReplicaID(), check.DeepEquals, []uint64{2, 3})
    44  
    45  	cyc = NewCyclic(nil)
    46  	c.Assert(cyc, check.IsNil)
    47  	cyc = NewCyclic(&config.CyclicConfig{ReplicaID: 0})
    48  	c.Assert(cyc, check.IsNil)
    49  }
    50  
    51  func (s *cyclicSuite) TestRelaxSQLMode(c *check.C) {
    52  	defer testleak.AfterTest(c)()
    53  	tests := []struct {
    54  		oldMode string
    55  		newMode string
    56  	}{
    57  		{"ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE", "ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE"},
    58  		{"ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,STRICT_TRANS_TABLES", "ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE"},
    59  		{"STRICT_TRANS_TABLES", ""},
    60  		{"ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE", "ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE"},
    61  	}
    62  
    63  	for _, test := range tests {
    64  		getNew := RelaxSQLMode(test.oldMode)
    65  		c.Assert(getNew, check.Equals, test.newMode)
    66  	}
    67  }
    68  
    69  func (s *cyclicSuite) TestIsTablePaired(c *check.C) {
    70  	defer testleak.AfterTest(c)()
    71  	tests := []struct {
    72  		tables   []model.TableName
    73  		isParied bool
    74  	}{
    75  		{[]model.TableName{}, true},
    76  		{
    77  			[]model.TableName{{Schema: mark.SchemaName, Table: "repl_mark_1"}},
    78  			true,
    79  		},
    80  		{
    81  			[]model.TableName{{Schema: "a", Table: "a"}},
    82  			false,
    83  		},
    84  		{
    85  			[]model.TableName{
    86  				{Schema: mark.SchemaName, Table: "repl_mark_a_a"},
    87  				{Schema: "a", Table: "a"},
    88  			},
    89  			true,
    90  		},
    91  		{
    92  			[]model.TableName{
    93  				{Schema: mark.SchemaName, Table: "repl_mark_a_a"},
    94  				{Schema: mark.SchemaName, Table: "repl_mark_a_b"},
    95  				{Schema: "a", Table: "a"},
    96  				{Schema: "a", Table: "b"},
    97  			},
    98  			true,
    99  		},
   100  	}
   101  
   102  	for _, test := range tests {
   103  		c.Assert(IsTablesPaired(test.tables), check.Equals, test.isParied,
   104  			check.Commentf("%v", test))
   105  	}
   106  }