github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/mq_protocol_tests/framework/mysql/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 mysql
    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  	CheckOleValue bool
    32  }
    33  
    34  // Name implements Task
    35  func (c *SingleTableTask) Name() string {
    36  	log.Warn("SingleTableTask should be embedded in another Task")
    37  	return "SingleTableTask-" + c.TableName
    38  }
    39  
    40  // GetCDCProfile implements Task
    41  func (c *SingleTableTask) GetCDCProfile() *framework.CDCProfile {
    42  	sinkURI := "mysql://downstream-tidb:4000/" + testDbName
    43  	if c.CheckOleValue {
    44  		sinkURI = "simple-mysql://downstream-tidb:4000/" + testDbName + "?check-old-value=true"
    45  	}
    46  	return &framework.CDCProfile{
    47  		PDUri:      framework.UpstreamPD,
    48  		SinkURI:    sinkURI,
    49  		ConfigFile: "/configs/enable-oldvalue-config.toml",
    50  	}
    51  }
    52  
    53  // Prepare implements Task
    54  func (c *SingleTableTask) Prepare(taskContext *framework.TaskContext) error {
    55  	err := taskContext.CreateDB(testDbName)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	_ = taskContext.Upstream.Close()
    61  	taskContext.Upstream, err = sql.Open("mysql", framework.UpstreamDSN+testDbName)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	_ = taskContext.Downstream.Close()
    67  	taskContext.Downstream, err = sql.Open("mysql", framework.DownstreamDSN+testDbName)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	taskContext.Downstream.SetConnMaxLifetime(5 * time.Second)
    72  
    73  	if taskContext.WaitForReady != nil {
    74  		log.Info("Waiting for env to be ready")
    75  		return taskContext.WaitForReady()
    76  	}
    77  	return nil
    78  }
    79  
    80  // Run implements Task
    81  func (c *SingleTableTask) Run(taskContext *framework.TaskContext) error {
    82  	log.Warn("SingleTableTask has been run")
    83  	return nil
    84  }