github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/syncer/job_test.go (about)

     1  // Copyright 2019 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 syncer
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/go-mysql-org/go-mysql/mysql"
    20  	. "github.com/pingcap/check"
    21  	"github.com/pingcap/tidb/pkg/util/filter"
    22  	cdcmodel "github.com/pingcap/tiflow/cdc/model"
    23  	"github.com/pingcap/tiflow/dm/pkg/binlog"
    24  	"github.com/pingcap/tiflow/pkg/sqlmodel"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  var _ = Suite(&testJobSuite{})
    29  
    30  type testJobSuite struct{}
    31  
    32  func (t *testJobSuite) TestJobTypeString(c *C) {
    33  	testCases := []struct {
    34  		tp  opType
    35  		str string
    36  	}{
    37  		{
    38  			null,
    39  			"",
    40  		}, {
    41  			dml,
    42  			"dml",
    43  		}, {
    44  			ddl,
    45  			"ddl",
    46  		}, {
    47  			xid,
    48  			"xid",
    49  		}, {
    50  			flush,
    51  			"flush",
    52  		}, {
    53  			skip,
    54  			"skip",
    55  		}, {
    56  			rotate,
    57  			"rotate",
    58  		},
    59  	}
    60  
    61  	for _, testCase := range testCases {
    62  		tpStr := testCase.tp.String()
    63  		c.Assert(tpStr, Equals, testCase.str)
    64  	}
    65  }
    66  
    67  func TestJob(t *testing.T) {
    68  	t.Parallel()
    69  
    70  	ddlInfo := &ddlInfo{
    71  		sourceTables: []*filter.Table{{Schema: "test1", Name: "t1"}},
    72  		targetTables: []*filter.Table{{Schema: "test2", Name: "t2"}},
    73  	}
    74  	table := &cdcmodel.TableName{Schema: "test", Table: "t1"}
    75  	location := binlog.MustZeroLocation(mysql.MySQLFlavor)
    76  	ec := &eventContext{startLocation: location, endLocation: location, lastLocation: location, safeMode: true}
    77  	qec := &queryEventContext{
    78  		eventContext:    ec,
    79  		originSQL:       "create database test",
    80  		needHandleDDLs:  []string{"create database test"},
    81  		shardingDDLInfo: ddlInfo,
    82  	}
    83  
    84  	schema := "create table test.tb(id int primary key, col1 int unique not null)"
    85  	ti := mockTableInfo(t, schema)
    86  
    87  	exampleChange := sqlmodel.NewRowChange(table, nil, nil, []interface{}{2, 2}, ti, nil, nil)
    88  
    89  	testCases := []struct {
    90  		job    *job
    91  		jobStr string
    92  	}{
    93  		{
    94  			newDMLJob(exampleChange, ec),
    95  			"tp: dml, flushSeq: 0, dml: [type: ChangeInsert, source table: test.t1, target table: test.t1, preValues: [], postValues: [2 2]], safemode: true, ddls: [], last_location: position: (, 4), gtid-set: , start_location: position: (, 4), gtid-set: , current_location: position: (, 4), gtid-set: ",
    96  		}, {
    97  			newDDLJob(qec),
    98  			"tp: ddl, flushSeq: 0, dml: [], safemode: false, ddls: [create database test], last_location: position: (, 4), gtid-set: , start_location: position: (, 4), gtid-set: , current_location: position: (, 4), gtid-set: ",
    99  		}, {
   100  			newXIDJob(binlog.MustZeroLocation(mysql.MySQLFlavor), binlog.MustZeroLocation(mysql.MySQLFlavor), binlog.MustZeroLocation(mysql.MySQLFlavor)),
   101  			"tp: xid, flushSeq: 0, dml: [], safemode: false, ddls: [], last_location: position: (, 4), gtid-set: , start_location: position: (, 4), gtid-set: , current_location: position: (, 4), gtid-set: ",
   102  		}, {
   103  			newFlushJob(16, 1),
   104  			"tp: flush, flushSeq: 1, dml: [], safemode: false, ddls: [], last_location: position: (, 0), gtid-set: , start_location: position: (, 0), gtid-set: , current_location: position: (, 0), gtid-set: ",
   105  		}, {
   106  			newSkipJob(ec),
   107  			"tp: skip, flushSeq: 0, dml: [], safemode: false, ddls: [], last_location: position: (, 4), gtid-set: , start_location: position: (, 0), gtid-set: , current_location: position: (, 0), gtid-set: ",
   108  		},
   109  	}
   110  
   111  	for _, testCase := range testCases {
   112  		require.Equal(t, testCase.jobStr, testCase.job.String())
   113  	}
   114  }
   115  
   116  func (t *testJobSuite) TestQueueBucketName(c *C) {
   117  	name := queueBucketName(0)
   118  	c.Assert(name, Equals, "q_0")
   119  
   120  	name = queueBucketName(8)
   121  	c.Assert(name, Equals, "q_0")
   122  
   123  	name = queueBucketName(9)
   124  	c.Assert(name, Equals, "q_1")
   125  }