github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/integration/tests/case_delete.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 tests 15 16 import ( 17 "errors" 18 19 "github.com/pingcap/log" 20 "github.com/pingcap/ticdc/integration/framework" 21 "go.uber.org/zap" 22 ) 23 24 // DeleteCase is base impl of test case for delete operation 25 type DeleteCase struct { 26 framework.Task 27 } 28 29 // NewDeleteCase create a test case which contains delete ddls 30 func NewDeleteCase(task framework.Task) *DeleteCase { 31 return &DeleteCase{ 32 Task: task, 33 } 34 } 35 36 // Name impl framework.Task interface 37 func (c *DeleteCase) Name() string { 38 return "Delete" 39 } 40 41 // Run impl framework.Task interface 42 func (c *DeleteCase) Run(ctx *framework.TaskContext) error { 43 _, err := ctx.Upstream.ExecContext(ctx.Ctx, "create table test (id int primary key, value int)") 44 if err != nil { 45 return err 46 } 47 48 table := ctx.SQLHelper().GetTable("test") 49 50 // To wait on a batch of SQL requests, create a slice of Awaitables 51 reqs := make([]framework.Awaitable, 0) 52 for i := 0; i < 1000; i++ { 53 // Only send, do not wait 54 req := table.Insert(map[string]interface{}{ 55 "id": i, 56 "value": i, 57 }).Send() 58 reqs = append(reqs, req) 59 } 60 61 err = framework.All(ctx.SQLHelper(), reqs).Wait().Check() 62 if err != nil { 63 return err 64 } 65 66 deletes := make([]framework.Awaitable, 0, 1000) 67 for i := 0; i < 1000; i++ { 68 req := table.Delete(map[string]interface{}{ 69 "id": i, 70 }).Send() 71 deletes = append(deletes, req) 72 } 73 74 for _, req := range deletes { 75 err := req.Wait().Check() 76 if err != nil { 77 return err 78 } 79 } 80 81 count := 0 82 err = ctx.Downstream.QueryRowContext(ctx.Ctx, "select count(*) from test").Scan(&count) 83 if err != nil { 84 return err 85 } 86 87 if count != 0 { 88 log.Warn("table is not empty", zap.Int("count", count)) 89 return errors.New("table is not empty") 90 } 91 92 return nil 93 }