github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/mq_protocol_tests/cases/case_simple.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 cases
    15  
    16  import (
    17  	"github.com/pingcap/errors"
    18  	"github.com/pingcap/tiflow/tests/mq_protocol_tests/framework"
    19  )
    20  
    21  // SimpleCase is base impl of simple test case
    22  type SimpleCase struct {
    23  	framework.Task
    24  }
    25  
    26  // NewSimpleCase create a test case which has some simple dmls, ddls
    27  func NewSimpleCase(task framework.Task) *SimpleCase {
    28  	return &SimpleCase{
    29  		Task: task,
    30  	}
    31  }
    32  
    33  // Name impl framework.Task interface
    34  func (s *SimpleCase) Name() string {
    35  	return "Simple"
    36  }
    37  
    38  // Run impl framework.Task interface
    39  func (s *SimpleCase) Run(ctx *framework.TaskContext) error {
    40  	_, err := ctx.Upstream.ExecContext(ctx.Ctx, "create table test (id int primary key, value int)")
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	// Get a handle of an existing table
    46  	table := ctx.SQLHelper().GetTable("test")
    47  	// Create an SQL request, send it to the upstream, wait for completion and check the correctness of replication
    48  	err = table.Insert(map[string]interface{}{
    49  		"id":    0,
    50  		"value": 0,
    51  	}).Send().Wait().Check()
    52  	if err != nil {
    53  		return errors.AddStack(err)
    54  	}
    55  
    56  	// To wait on a batch of SQL requests, create a slice of Awaitables
    57  	reqs := make([]framework.Awaitable, 0)
    58  	for i := 1; i < 1000; i++ {
    59  		// Only send, do not wait
    60  		req := table.Insert(map[string]interface{}{
    61  			"id":    i,
    62  			"value": i,
    63  		}).Send()
    64  		reqs = append(reqs, req)
    65  	}
    66  
    67  	// Wait on SQL requests in batch and check the correctness
    68  	err = framework.All(ctx.SQLHelper(), reqs).Wait().Check()
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	err = table.Upsert(map[string]interface{}{
    74  		"id":    0,
    75  		"value": 1,
    76  	}).Send().Wait().Check()
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	err = table.Delete(map[string]interface{}{
    82  		"id": 0,
    83  	}).Send().Wait().Check()
    84  	return err
    85  }