github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/sink/dispatcher/switcher_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/config"
    20  	"github.com/pingcap/ticdc/pkg/util/testleak"
    21  )
    22  
    23  type SwitcherSuite struct{}
    24  
    25  var _ = check.Suite(&SwitcherSuite{})
    26  
    27  func (s SwitcherSuite) TestSwitcher(c *check.C) {
    28  	defer testleak.AfterTest(c)()
    29  	d, err := NewDispatcher(config.GetDefaultReplicaConfig(), 4)
    30  	c.Assert(err, check.IsNil)
    31  	c.Assert(d.(*dispatcherSwitcher).matchDispatcher(&model.RowChangedEvent{
    32  		Table: &model.TableName{
    33  			Schema: "test", Table: "test",
    34  		},
    35  	}), check.FitsTypeOf, &defaultDispatcher{})
    36  
    37  	d, err = NewDispatcher(&config.ReplicaConfig{
    38  		Sink: &config.SinkConfig{
    39  			DispatchRules: []*config.DispatchRule{
    40  				{Matcher: []string{"test_default.*"}, Dispatcher: "default"},
    41  				{Matcher: []string{"test_table.*"}, Dispatcher: "table"},
    42  				{Matcher: []string{"test_index_value.*"}, Dispatcher: "index-value"},
    43  				{Matcher: []string{"test.*"}, Dispatcher: "rowid"},
    44  				{Matcher: []string{"*.*", "!*.test"}, Dispatcher: "ts"},
    45  			},
    46  		},
    47  	}, 4)
    48  	c.Assert(err, check.IsNil)
    49  	c.Assert(d.(*dispatcherSwitcher).matchDispatcher(&model.RowChangedEvent{
    50  		Table: &model.TableName{
    51  			Schema: "test", Table: "table1",
    52  		},
    53  	}), check.FitsTypeOf, &indexValueDispatcher{})
    54  	c.Assert(d.(*dispatcherSwitcher).matchDispatcher(&model.RowChangedEvent{
    55  		Table: &model.TableName{
    56  			Schema: "sbs", Table: "table2",
    57  		},
    58  	}), check.FitsTypeOf, &tsDispatcher{})
    59  	c.Assert(d.(*dispatcherSwitcher).matchDispatcher(&model.RowChangedEvent{
    60  		Table: &model.TableName{
    61  			Schema: "sbs", Table: "test",
    62  		},
    63  	}), check.FitsTypeOf, &defaultDispatcher{})
    64  	c.Assert(d.(*dispatcherSwitcher).matchDispatcher(&model.RowChangedEvent{
    65  		Table: &model.TableName{
    66  			Schema: "test_default", Table: "test",
    67  		},
    68  	}), check.FitsTypeOf, &defaultDispatcher{})
    69  	c.Assert(d.(*dispatcherSwitcher).matchDispatcher(&model.RowChangedEvent{
    70  		Table: &model.TableName{
    71  			Schema: "test_table", Table: "test",
    72  		},
    73  	}), check.FitsTypeOf, &tableDispatcher{})
    74  	c.Assert(d.(*dispatcherSwitcher).matchDispatcher(&model.RowChangedEvent{
    75  		Table: &model.TableName{
    76  			Schema: "test_index_value", Table: "test",
    77  		},
    78  	}), check.FitsTypeOf, &indexValueDispatcher{})
    79  }