github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/sink/dmlsink/mq/dmlproducer/pulsar_dml_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 dmlproducer
    15  
    16  import (
    17  	"context"
    18  	"net/url"
    19  	"testing"
    20  
    21  	"github.com/aws/aws-sdk-go/aws"
    22  	"github.com/pingcap/tiflow/cdc/model"
    23  	"github.com/pingcap/tiflow/pkg/config"
    24  	"github.com/pingcap/tiflow/pkg/sink/codec/common"
    25  	pulsarConfig "github.com/pingcap/tiflow/pkg/sink/pulsar"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  // newPulsarConfig set config
    30  func newPulsarConfig(t *testing.T) (sinkURI *url.URL, replicaConfig *config.ReplicaConfig) {
    31  	sinkURL := "pulsar://127.0.0.1:6650/persistent://public/default/test?" +
    32  		"protocol=canal-json&pulsar-version=v2.10.0&enable-tidb-extension=true&" +
    33  		"authentication-token=eyJhbcGcixxxxxxxxxxxxxx"
    34  	var err error
    35  	sinkURI, err = url.Parse(sinkURL)
    36  	require.NoError(t, err)
    37  	replicaConfig = config.GetDefaultReplicaConfig()
    38  	require.NoError(t, replicaConfig.ValidateAndAdjust(sinkURI))
    39  	var c *config.PulsarConfig
    40  	c, err = pulsarConfig.NewPulsarConfig(sinkURI, replicaConfig.Sink.PulsarConfig)
    41  	require.NoError(t, err)
    42  	replicaConfig.Sink.PulsarConfig = c
    43  	return sinkURI, replicaConfig
    44  }
    45  
    46  func TestNewPulsarDMLProducer(t *testing.T) {
    47  	t.Parallel()
    48  
    49  	sinkURI, rc := newPulsarConfig(t)
    50  	replicaConfig := config.GetDefaultReplicaConfig()
    51  	replicaConfig.Sink = &config.SinkConfig{
    52  		Protocol: aws.String("canal-json"),
    53  	}
    54  	t.Logf(sinkURI.String())
    55  
    56  	ctx, cancel := context.WithCancel(context.Background())
    57  	defer cancel()
    58  
    59  	errCh := make(chan error, 1)
    60  
    61  	ctx = context.WithValue(ctx, "testing.T", t)
    62  	changefeed := model.DefaultChangeFeedID("changefeed-test")
    63  
    64  	failpointCh := make(chan error, 1)
    65  
    66  	client, err := pulsarConfig.NewMockCreatorFactory(rc.Sink.PulsarConfig, changefeed, rc.Sink)
    67  	require.NoError(t, err)
    68  	dml, err := NewPulsarDMLProducerMock(ctx, changefeed, client, rc.Sink, errCh, failpointCh)
    69  	require.NoError(t, err)
    70  	require.NotNil(t, dml)
    71  }
    72  
    73  func Test_pulsarDMLProducer_AsyncSendMessage(t *testing.T) {
    74  	t.Parallel()
    75  
    76  	_, rc := newPulsarConfig(t)
    77  	replicaConfig := config.GetDefaultReplicaConfig()
    78  	replicaConfig.Sink = &config.SinkConfig{
    79  		Protocol: aws.String("canal-json"),
    80  	}
    81  
    82  	ctx, cancel := context.WithCancel(context.Background())
    83  	defer cancel()
    84  
    85  	errCh := make(chan error, 1)
    86  
    87  	ctx = context.WithValue(ctx, "testing.T", t)
    88  	changefeed := model.DefaultChangeFeedID("changefeed-test")
    89  
    90  	failpointCh := make(chan error, 1)
    91  
    92  	client, err := pulsarConfig.NewMockCreatorFactory(rc.Sink.PulsarConfig, changefeed, rc.Sink)
    93  	require.NoError(t, err)
    94  	dml, err := NewPulsarDMLProducerMock(ctx, changefeed, client, rc.Sink, errCh, failpointCh)
    95  	require.NoError(t, err)
    96  	require.NotNil(t, dml)
    97  
    98  	err = dml.AsyncSendMessage(ctx, "test", 0, &common.Message{
    99  		Value:        []byte("this value for test input data"),
   100  		PartitionKey: str2Pointer("test_key"),
   101  	})
   102  	require.NoError(t, err)
   103  }