github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/sink/ddlsink/mq/ddlproducer/kafka_ddl_producer_test.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  	"testing"
    19  	"time"
    20  
    21  	"github.com/IBM/sarama"
    22  	"github.com/pingcap/tiflow/cdc/model"
    23  	cerror "github.com/pingcap/tiflow/pkg/errors"
    24  	"github.com/pingcap/tiflow/pkg/sink/codec/common"
    25  	"github.com/pingcap/tiflow/pkg/sink/kafka"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func getOptions() *kafka.Options {
    30  	options := kafka.NewOptions()
    31  	options.Version = "0.9.0.0"
    32  	options.ClientID = "test-client"
    33  	options.PartitionNum = int32(kafka.DefaultMockPartitionNum)
    34  	options.AutoCreate = false
    35  	options.BrokerEndpoints = []string{"127.0.0.1:9092"}
    36  
    37  	return options
    38  }
    39  
    40  func TestSyncBroadcastMessage(t *testing.T) {
    41  	ctx, cancel := context.WithCancel(context.Background())
    42  	options := getOptions()
    43  	options.MaxMessages = 1
    44  
    45  	ctx = context.WithValue(ctx, "testing.T", t)
    46  	changefeed := model.DefaultChangeFeedID("changefeed-test")
    47  	factory, err := kafka.NewMockFactory(options, changefeed)
    48  	require.NoError(t, err)
    49  
    50  	syncProducer, err := factory.SyncProducer(ctx)
    51  	require.NoError(t, err)
    52  
    53  	p := NewKafkaDDLProducer(ctx, changefeed, syncProducer)
    54  
    55  	for i := 0; i < kafka.DefaultMockPartitionNum; i++ {
    56  		syncProducer.(*kafka.MockSaramaSyncProducer).Producer.ExpectSendMessageAndSucceed()
    57  	}
    58  	err = p.SyncBroadcastMessage(ctx, kafka.DefaultMockTopicName,
    59  		kafka.DefaultMockPartitionNum, &common.Message{Ts: 417318403368288260})
    60  	require.NoError(t, err)
    61  
    62  	p.Close()
    63  	err = p.SyncBroadcastMessage(ctx, kafka.DefaultMockTopicName,
    64  		kafka.DefaultMockPartitionNum, &common.Message{Ts: 417318403368288260})
    65  	require.ErrorIs(t, err, cerror.ErrKafkaProducerClosed)
    66  	cancel()
    67  }
    68  
    69  func TestSyncSendMessage(t *testing.T) {
    70  	ctx, cancel := context.WithCancel(context.Background())
    71  	options := getOptions()
    72  
    73  	ctx = context.WithValue(ctx, "testing.T", t)
    74  	changefeed := model.DefaultChangeFeedID("changefeed-test")
    75  	factory, err := kafka.NewMockFactory(options, changefeed)
    76  	require.NoError(t, err)
    77  
    78  	syncProducer, err := factory.SyncProducer(ctx)
    79  	require.NoError(t, err)
    80  
    81  	p := NewKafkaDDLProducer(ctx, changefeed, syncProducer)
    82  
    83  	syncProducer.(*kafka.MockSaramaSyncProducer).Producer.ExpectSendMessageAndSucceed()
    84  	err = p.SyncSendMessage(ctx, kafka.DefaultMockTopicName, 0, &common.Message{Ts: 417318403368288260})
    85  	require.NoError(t, err)
    86  
    87  	p.Close()
    88  	err = p.SyncSendMessage(ctx, kafka.DefaultMockTopicName, 0, &common.Message{Ts: 417318403368288260})
    89  	require.ErrorIs(t, err, cerror.ErrKafkaProducerClosed)
    90  	cancel()
    91  }
    92  
    93  func TestProducerSendMsgFailed(t *testing.T) {
    94  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
    95  	defer cancel()
    96  	options := getOptions()
    97  	options.MaxMessages = 1
    98  	options.MaxMessageBytes = 1
    99  
   100  	ctx = context.WithValue(ctx, "testing.T", t)
   101  
   102  	// This will make the first send failed.
   103  	changefeed := model.DefaultChangeFeedID("changefeed-test")
   104  	factory, err := kafka.NewMockFactory(options, changefeed)
   105  	require.NoError(t, err)
   106  
   107  	syncProducer, err := factory.SyncProducer(ctx)
   108  	require.NoError(t, err)
   109  
   110  	p := NewKafkaDDLProducer(ctx, changefeed, syncProducer)
   111  	defer p.Close()
   112  
   113  	syncProducer.(*kafka.MockSaramaSyncProducer).Producer.ExpectSendMessageAndFail(sarama.ErrMessageTooLarge)
   114  	err = p.SyncSendMessage(ctx, kafka.DefaultMockTopicName, 0, &common.Message{Ts: 417318403368288260})
   115  	require.ErrorIs(t, err, sarama.ErrMessageTooLarge)
   116  }
   117  
   118  func TestProducerDoubleClose(t *testing.T) {
   119  	ctx, cancel := context.WithCancel(context.Background())
   120  	defer cancel()
   121  	options := getOptions()
   122  
   123  	ctx = context.WithValue(ctx, "testing.T", t)
   124  	changefeed := model.DefaultChangeFeedID("changefeed-test")
   125  	factory, err := kafka.NewMockFactory(options, changefeed)
   126  	require.NoError(t, err)
   127  
   128  	syncProducer, err := factory.SyncProducer(ctx)
   129  	require.NoError(t, err)
   130  
   131  	p := NewKafkaDDLProducer(ctx, changefeed, syncProducer)
   132  
   133  	p.Close()
   134  	p.Close()
   135  }