github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/pulsar/config_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 pulsar
    15  
    16  import (
    17  	"net/url"
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/apache/pulsar-client-go/pulsar"
    22  	"github.com/aws/aws-sdk-go/aws"
    23  	"github.com/pingcap/tiflow/pkg/config"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestPulsarConfig(t *testing.T) {
    28  	p := &config.PulsarConfig{
    29  		CompressionType:         (*config.PulsarCompressionType)(aws.String("lz4")),
    30  		ConnectionTimeout:       (*config.TimeSec)(aws.Int(defaultConnectionTimeout)),
    31  		OperationTimeout:        (*config.TimeSec)(aws.Int(998)),
    32  		BatchingMaxMessages:     aws.Uint(defaultBatchingMaxSize),
    33  		BatchingMaxPublishDelay: (*config.TimeMill)(aws.Int(defaultBatchingMaxPublishDelay)),
    34  		SendTimeout:             (*config.TimeSec)(aws.Int(123)),
    35  	}
    36  
    37  	// Define test cases
    38  	tests := []struct {
    39  		name    string
    40  		sinkURI string
    41  		wantErr bool
    42  	}{
    43  		{
    44  			name: "Test valid sinkURI",
    45  			sinkURI: "pulsar://127.0.0.1:6650/persistent://tenant/namespace/test-topic" +
    46  				"?send-timeout=0123&compression=lz4&operation-timeout=998",
    47  			wantErr: false,
    48  		},
    49  		{
    50  			name:    "Test invalid sinkURI",
    51  			sinkURI: "pulsar://?send-timeout=123&compression=lz4",
    52  			wantErr: true,
    53  		},
    54  	}
    55  
    56  	for _, tt := range tests {
    57  		t.Run(tt.name, func(t *testing.T) {
    58  			sink, err := url.Parse(tt.sinkURI)
    59  			if err != nil {
    60  				t.Fatalf("Failed to parse sinkURI: %v", err)
    61  			}
    62  
    63  			replicaConfig := config.GetDefaultReplicaConfig()
    64  
    65  			replicaConfig.Sink.PulsarConfig = p
    66  			// Call function under test
    67  			config, err := NewPulsarConfig(sink, replicaConfig.Sink.PulsarConfig)
    68  
    69  			// Assert error value
    70  			if tt.wantErr {
    71  				assert.Error(t, err)
    72  			} else {
    73  				assert.NoError(t, err)
    74  			}
    75  
    76  			// If no error is expected, assert config values
    77  			if !tt.wantErr {
    78  				assert.Equal(t, config.CompressionType.Value(), pulsar.LZ4)
    79  				assert.Equal(t, config.BrokerURL, "pulsar://127.0.0.1:6650")
    80  				assert.Equal(t, config.ConnectionTimeout.Duration(), defaultConnectionTimeout*time.Second)
    81  				assert.Equal(t, config.OperationTimeout.Duration(), 998*time.Second)
    82  				assert.Equal(t, *config.BatchingMaxMessages, defaultBatchingMaxSize)
    83  				assert.Equal(t, config.BatchingMaxPublishDelay.Duration(), defaultBatchingMaxPublishDelay*time.Millisecond)
    84  				assert.Equal(t, config.SendTimeout.Duration(), 123*time.Second)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestGetBrokerURL(t *testing.T) {
    91  	sink, _ := url.Parse("pulsar://localhost:6650/test")
    92  
    93  	replicaConfig := config.GetDefaultReplicaConfig()
    94  	config, _ := NewPulsarConfig(sink, replicaConfig.Sink.PulsarConfig)
    95  
    96  	assert.Equal(t, config.BrokerURL, "pulsar://localhost:6650")
    97  }
    98  
    99  func TestGetSinkURI(t *testing.T) {
   100  	sink, _ := url.Parse("pulsar://127.0.0.1:6650/persistent://tenant/namespace/test-topic" +
   101  		"?max-message-bytes=5000&compression=lz4")
   102  
   103  	replicaConfig := config.GetDefaultReplicaConfig()
   104  	config, _ := NewPulsarConfig(sink, replicaConfig.Sink.PulsarConfig)
   105  
   106  	assert.Equal(t, config.SinkURI, sink)
   107  }
   108  
   109  func TestGetDefaultTopicName(t *testing.T) {
   110  	sink, _ := url.Parse("pulsar://localhost:6650/test")
   111  	replicaConfig := config.GetDefaultReplicaConfig()
   112  	config, _ := NewPulsarConfig(sink, replicaConfig.Sink.PulsarConfig)
   113  	assert.Equal(t, config.GetDefaultTopicName(), "test")
   114  
   115  	sink, _ = url.Parse("pulsar://127.0.0.1:6650/persistent://tenant/namespace/test-topic")
   116  	config, _ = NewPulsarConfig(sink, replicaConfig.Sink.PulsarConfig)
   117  	assert.Equal(t, config.GetDefaultTopicName(), "persistent://tenant/namespace/test-topic")
   118  }