github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/sink/codec/maxwell_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 codec
    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 maxwellbatchSuite struct {
    23  	rowCases [][]*model.RowChangedEvent
    24  	ddlCases [][]*model.DDLEvent
    25  }
    26  
    27  var _ = check.Suite(&maxwellbatchSuite{
    28  	rowCases: [][]*model.RowChangedEvent{{{
    29  		CommitTs: 1,
    30  		Table:    &model.TableName{Schema: "a", Table: "b"},
    31  		Columns:  []*model.Column{{Name: "col1", Type: 3, Value: 10}},
    32  	}}, {}},
    33  	ddlCases: [][]*model.DDLEvent{{{
    34  		CommitTs: 1,
    35  		TableInfo: &model.SimpleTableInfo{
    36  			Schema: "a", Table: "b",
    37  		},
    38  		Query: "create table a",
    39  		Type:  1,
    40  	}}},
    41  })
    42  
    43  func (s *maxwellbatchSuite) testmaxwellBatchCodec(c *check.C, newEncoder func() EventBatchEncoder) {
    44  	for _, cs := range s.rowCases {
    45  		encoder := newEncoder()
    46  		for _, row := range cs {
    47  			_, err := encoder.AppendRowChangedEvent(row)
    48  			c.Assert(err, check.IsNil)
    49  		}
    50  		size := encoder.Size()
    51  		messages := encoder.Build()
    52  		if len(cs) == 0 {
    53  			c.Assert(messages, check.IsNil)
    54  			continue
    55  		}
    56  		c.Assert(messages, check.HasLen, 1)
    57  		c.Assert(len(messages[0].Key)+len(messages[0].Value), check.Equals, size)
    58  	}
    59  
    60  	for _, cs := range s.ddlCases {
    61  		encoder := newEncoder()
    62  		for _, ddl := range cs {
    63  			msg, err := encoder.EncodeDDLEvent(ddl)
    64  			c.Assert(err, check.IsNil)
    65  			c.Assert(msg, check.NotNil)
    66  		}
    67  
    68  	}
    69  }
    70  
    71  func (s *maxwellbatchSuite) TestmaxwellEventBatchCodec(c *check.C) {
    72  	defer testleak.AfterTest(c)()
    73  	s.testmaxwellBatchCodec(c, NewMaxwellEventBatchEncoder)
    74  }
    75  
    76  var _ = check.Suite(&maxwellcolumnSuite{})
    77  
    78  type maxwellcolumnSuite struct{}
    79  
    80  func (s *maxwellcolumnSuite) TestMaxwellFormatCol(c *check.C) {
    81  	defer testleak.AfterTest(c)()
    82  	row := &maxwellMessage{
    83  		Ts:       1,
    84  		Database: "a",
    85  		Table:    "b",
    86  		Type:     "delete",
    87  		Xid:      1,
    88  		Xoffset:  1,
    89  		Position: "",
    90  		Gtid:     "",
    91  		Data: map[string]interface{}{
    92  			"id": "1",
    93  		},
    94  	}
    95  	rowEncode, err := row.Encode()
    96  	c.Assert(err, check.IsNil)
    97  	c.Assert(rowEncode, check.NotNil)
    98  }