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

     1  // Copyright 2023 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  
    20  	"github.com/pingcap/tiflow/cdc/model"
    21  	"github.com/pingcap/tiflow/pkg/config"
    22  	"github.com/pingcap/tiflow/pkg/leakutil"
    23  	"github.com/pingcap/tiflow/pkg/sink/codec/common"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  // TestPulsarSyncSendMessage is a integration test for pulsar producer
    28  func TestPulsarSyncSendMessage(t *testing.T) {
    29  	leakutil.VerifyNone(t)
    30  
    31  	type args struct {
    32  		ctx          context.Context
    33  		topic        string
    34  		partition    int32
    35  		message      *common.Message
    36  		changefeedID model.ChangeFeedID
    37  		pulsarConfig *config.PulsarConfig
    38  		errCh        chan error
    39  	}
    40  	tests := []struct {
    41  		name    string
    42  		args    args
    43  		wantErr bool
    44  	}{
    45  		{
    46  			name: "test SyncSendMessage",
    47  			args: args{
    48  				ctx:          context.Background(),
    49  				topic:        "test",
    50  				partition:    1,
    51  				changefeedID: model.ChangeFeedID{ID: "test", Namespace: "test_namespace"},
    52  				message: &common.Message{
    53  					Value:        []byte("this value for test input data"),
    54  					PartitionKey: str2Pointer("test_key"),
    55  				},
    56  				errCh: make(chan error),
    57  			},
    58  		},
    59  	}
    60  	for _, tt := range tests {
    61  		p, err := NewMockPulsarProducer(tt.args.ctx, tt.args.changefeedID,
    62  			tt.args.pulsarConfig, nil)
    63  
    64  		require.NoError(t, err)
    65  
    66  		err = p.SyncSendMessage(tt.args.ctx, tt.args.topic,
    67  			tt.args.partition, tt.args.message)
    68  		require.NoError(t, err)
    69  		require.Len(t, p.GetEvents(tt.args.topic), 1)
    70  
    71  		p.Close()
    72  
    73  	}
    74  }
    75  
    76  // TestPulsarSyncBroadcastMessage is a integration test for pulsar producer
    77  func TestPulsarSyncBroadcastMessage(t *testing.T) {
    78  	// leakutil.VerifyNone(t)
    79  
    80  	type args struct {
    81  		ctx          context.Context
    82  		topic        string
    83  		partition    int32
    84  		message      *common.Message
    85  		changefeedID model.ChangeFeedID
    86  		pulsarConfig *config.PulsarConfig
    87  		errCh        chan error
    88  	}
    89  	tests := []struct {
    90  		name    string
    91  		args    args
    92  		wantErr bool
    93  	}{
    94  		{
    95  			name: "test SyncBroadcastMessage",
    96  			args: args{
    97  				ctx:          context.Background(),
    98  				topic:        "test",
    99  				partition:    1,
   100  				changefeedID: model.ChangeFeedID{ID: "test", Namespace: "test_namespace"},
   101  				message: &common.Message{
   102  					Value:        []byte("this value for test input data"),
   103  					PartitionKey: str2Pointer("test_key"),
   104  				},
   105  				errCh: make(chan error),
   106  			},
   107  		},
   108  	}
   109  	for _, tt := range tests {
   110  		p, err := NewMockPulsarProducer(tt.args.ctx, tt.args.changefeedID,
   111  			tt.args.pulsarConfig, nil)
   112  
   113  		require.NoError(t, err)
   114  
   115  		err = p.SyncSendMessage(tt.args.ctx, tt.args.topic,
   116  			tt.args.partition, tt.args.message)
   117  		require.NoError(t, err)
   118  		require.Len(t, p.GetEvents(tt.args.topic), 1)
   119  
   120  		p.Close()
   121  
   122  	}
   123  }