github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/sink/dmlsink/factory/factory_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 factory 15 16 import ( 17 "context" 18 "fmt" 19 "net/url" 20 "strings" 21 "testing" 22 23 "github.com/pingcap/tiflow/cdc/model" 24 "github.com/pingcap/tiflow/cdc/sink/dmlsink/mq" 25 "github.com/pingcap/tiflow/cdc/sink/dmlsink/mq/dmlproducer" 26 "github.com/pingcap/tiflow/pkg/config" 27 cerror "github.com/pingcap/tiflow/pkg/errors" 28 "github.com/pingcap/tiflow/pkg/pdutil" 29 "github.com/pingcap/tiflow/pkg/sink/kafka" 30 "github.com/pingcap/tiflow/pkg/spanz" 31 "github.com/prometheus/client_golang/prometheus" 32 "github.com/stretchr/testify/require" 33 ) 34 35 func newForTest(ctx context.Context, 36 sinkURIStr string, 37 cfg *config.ReplicaConfig, 38 errCh chan error, 39 ) (*SinkFactory, error) { 40 sinkURI, err := url.Parse(sinkURIStr) 41 if err != nil { 42 return nil, cerror.WrapError(cerror.ErrSinkURIInvalid, err) 43 } 44 45 s := &SinkFactory{} 46 schema := strings.ToLower(sinkURI.Scheme) 47 switch schema { 48 case "kafka", "kafka+ssl": 49 mqs, err := mq.NewKafkaDMLSink(ctx, model.DefaultChangeFeedID("test"), 50 sinkURI, cfg, errCh, 51 // Use mock kafka clients for test. 52 kafka.NewMockFactory, dmlproducer.NewDMLMockProducer) 53 if err != nil { 54 return nil, err 55 } 56 s.txnSink = mqs 57 default: 58 return nil, 59 cerror.ErrSinkURIInvalid.GenWithStack("the sink scheme (%s) is not supported", schema) 60 } 61 return s, nil 62 } 63 64 func TestSinkFactory(t *testing.T) { 65 t.Parallel() 66 67 ctx, cancel := context.WithCancel(context.Background()) 68 defer cancel() 69 70 ctx = context.WithValue(ctx, "testing.T", t) 71 uriTemplate := "kafka://%s/%s?kafka-version=0.9.0.0&max-batch-size=1" + 72 "&max-message-bytes=1048576&partition-num=1" + 73 "&kafka-client-id=unit-test&auto-create-topic=false&compression=gzip&protocol=open-protocol" 74 uri := fmt.Sprintf(uriTemplate, "127.0.0.1:9092", kafka.DefaultMockTopicName) 75 76 sinkURI, err := url.Parse(uri) 77 require.NoError(t, err) 78 replicaConfig := config.GetDefaultReplicaConfig() 79 replicaConfig.Sink.KafkaConfig = &config.KafkaConfig{} 80 require.NoError(t, replicaConfig.ValidateAndAdjust(sinkURI)) 81 errCh := make(chan error, 1) 82 83 sinkFactory, err := newForTest(ctx, uri, replicaConfig, errCh) 84 require.NotNil(t, sinkFactory) 85 require.NoError(t, err) 86 require.NotNil(t, sinkFactory.txnSink) 87 88 tableSink := sinkFactory.CreateTableSink(model.DefaultChangeFeedID("1"), 89 spanz.TableIDToComparableSpan(1), 90 0, 91 pdutil.NewClock4Test(), 92 prometheus.NewCounter(prometheus.CounterOpts{}), 93 prometheus.NewHistogram(prometheus.HistogramOpts{})) 94 require.NotNil(t, tableSink, "table sink can be created") 95 96 sinkFactory.Close() 97 }