github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/sink/ddlsink/mq/ddlproducer/kafka_ddl_mock_producer.go (about)

     1  // Copyright 2022 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 ddlproducer
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  
    20  	"github.com/pingcap/tiflow/cdc/model"
    21  	"github.com/pingcap/tiflow/pkg/sink/codec/common"
    22  	"github.com/pingcap/tiflow/pkg/sink/kafka"
    23  )
    24  
    25  var _ DDLProducer = (*MockDDLProducer)(nil)
    26  
    27  // MockDDLProducer is a mock producer for test.
    28  type MockDDLProducer struct {
    29  	events map[string][]*common.Message
    30  }
    31  
    32  // NewMockDDLProducer creates a mock producer.
    33  func NewMockDDLProducer(_ context.Context, _ model.ChangeFeedID, _ kafka.SyncProducer) DDLProducer {
    34  	return &MockDDLProducer{
    35  		events: make(map[string][]*common.Message),
    36  	}
    37  }
    38  
    39  // SyncBroadcastMessage stores a message to all partitions of the topic.
    40  func (m *MockDDLProducer) SyncBroadcastMessage(ctx context.Context, topic string,
    41  	totalPartitionsNum int32, message *common.Message,
    42  ) error {
    43  	for i := 0; i < int(totalPartitionsNum); i++ {
    44  		key := fmt.Sprintf("%s-%d", topic, i)
    45  		if _, ok := m.events[key]; !ok {
    46  			m.events[key] = make([]*common.Message, 0)
    47  		}
    48  		m.events[key] = append(m.events[key], message)
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  // SyncSendMessage stores a message to a partition of the topic.
    55  func (m *MockDDLProducer) SyncSendMessage(_ context.Context, topic string,
    56  	partitionNum int32, message *common.Message,
    57  ) error {
    58  	key := fmt.Sprintf("%s-%d", topic, partitionNum)
    59  	if _, ok := m.events[key]; !ok {
    60  		m.events[key] = make([]*common.Message, 0)
    61  	}
    62  	m.events[key] = append(m.events[key], message)
    63  
    64  	return nil
    65  }
    66  
    67  // Close do nothing.
    68  func (m *MockDDLProducer) Close() {}
    69  
    70  // GetAllEvents returns the events received by the mock producer.
    71  func (m *MockDDLProducer) GetAllEvents() []*common.Message {
    72  	var events []*common.Message
    73  	for _, v := range m.events {
    74  		events = append(events, v...)
    75  	}
    76  	return events
    77  }
    78  
    79  // GetEvents returns the event filtered by the key.
    80  func (m *MockDDLProducer) GetEvents(topic string,
    81  	partitionNum int32,
    82  ) []*common.Message {
    83  	key := fmt.Sprintf("%s-%d", topic, partitionNum)
    84  	return m.events[key]
    85  }