github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/mq_protocol_tests/framework/canal/kafka_single_table.go (about)

     1  // Copyright 2020 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 canal
    15  
    16  import (
    17  	"database/sql"
    18  	"time"
    19  
    20  	"github.com/pingcap/log"
    21  	"github.com/pingcap/tiflow/tests/mq_protocol_tests/framework"
    22  )
    23  
    24  const (
    25  	testDbName = "testdb"
    26  )
    27  
    28  // SingleTableTask provides a basic implementation for an Avro test case
    29  type SingleTableTask struct {
    30  	TableName           string
    31  	UseJSON             bool
    32  	EnableTiDBExtension bool
    33  }
    34  
    35  // Name implements Task
    36  func (c *SingleTableTask) Name() string {
    37  	log.Warn("SingleTableTask should be embedded in another Task")
    38  	return "SingleTableTask-" + c.TableName
    39  }
    40  
    41  // GetCDCProfile implements Task
    42  func (c *SingleTableTask) GetCDCProfile() *framework.CDCProfile {
    43  	var protocol string
    44  	if c.UseJSON {
    45  		protocol = "canal-json"
    46  	} else {
    47  		protocol = "canal"
    48  	}
    49  
    50  	sinkURI := "kafka://kafka:9092/" + testDbName + "?kafka-version=2.6.0&protocol=" + protocol
    51  	if c.EnableTiDBExtension {
    52  		sinkURI += "&enable-tidb-extension=true"
    53  	}
    54  
    55  	return &framework.CDCProfile{
    56  		PDUri:      framework.UpstreamPD,
    57  		SinkURI:    sinkURI,
    58  		ConfigFile: "/configs/canal-test-config.toml",
    59  	}
    60  }
    61  
    62  // Prepare implements Task
    63  func (c *SingleTableTask) Prepare(taskContext *framework.TaskContext) error {
    64  	err := taskContext.CreateDB(testDbName)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	_ = taskContext.Upstream.Close()
    70  	taskContext.Upstream, err = sql.Open("mysql", framework.UpstreamDSN+testDbName)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	_ = taskContext.Downstream.Close()
    76  	taskContext.Downstream, err = sql.Open("mysql", framework.DownstreamDSN+testDbName)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	taskContext.Downstream.SetConnMaxLifetime(5 * time.Second)
    81  
    82  	if taskContext.WaitForReady != nil {
    83  		log.Info("Waiting for env to be ready")
    84  		return taskContext.WaitForReady()
    85  	}
    86  	return nil
    87  }
    88  
    89  // Run implements Task
    90  func (c *SingleTableTask) Run(taskContext *framework.TaskContext) error {
    91  	log.Warn("SingleTableTask has been run")
    92  	return nil
    93  }