From 3bce32d2ba31386bb95e1d971256f7e16413b8d8 Mon Sep 17 00:00:00 2001 From: cbcwestwolf <1004626265@qq.com> Date: Mon, 18 Nov 2024 16:24:31 +0800 Subject: [PATCH] init: add a ut case --- .../addindextest1/disttask_test.go | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/tests/realtikvtest/addindextest1/disttask_test.go b/tests/realtikvtest/addindextest1/disttask_test.go index 8e5b008c4f7eb..690b283df9fe1 100644 --- a/tests/realtikvtest/addindextest1/disttask_test.go +++ b/tests/realtikvtest/addindextest1/disttask_test.go @@ -17,9 +17,11 @@ package addindextest import ( "context" "fmt" + "strings" "sync" "sync/atomic" "testing" + "time" "github.com/pingcap/failpoint" "github.com/pingcap/tidb/pkg/config" @@ -109,7 +111,7 @@ func TestAddIndexDistBasic(t *testing.T) { tk.MustExec(`set global tidb_enable_dist_task=0;`) } -func TestAddIndexDistCancel(t *testing.T) { +func TestAddIndexDistCancelWithPartition(t *testing.T) { store := realtikvtest.CreateMockStoreAndSetup(t) if store.Name() != "TiKV" { t.Skip("TiKV store only") @@ -150,6 +152,69 @@ func TestAddIndexDistCancel(t *testing.T) { tk.MustExec(`set global tidb_enable_dist_task=0;`) } +func TestAddIndexDistCancel(t *testing.T) { + store := realtikvtest.CreateMockStoreAndSetup(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("drop database if exists addindexlit;") + tk.MustExec("create database addindexlit;") + tk.MustExec("use addindexlit;") + tk.MustExec(`set global tidb_ddl_enable_fast_reorg=on;`) + tk.MustExec("create table t (a int, b int);") + tk.MustExec("insert into t values (1, 1), (2, 2), (3, 3);") + + tk2 := testkit.NewTestKit(t, store) + tk2.MustExec("use addindexlit;") + tk2.MustExec(`set global tidb_ddl_enable_fast_reorg=on;`) + tk2.MustExec("create table t2 (a int, b int);") + tk2.MustExec("insert into t2 values (1, 1), (2, 2), (3, 3);") + + wg := &sync.WaitGroup{} + wg.Add(2) + go func() { + tk.MustExec("alter table t add index idx(a);") + wg.Done() + }() + go func() { + tk2.MustExec("alter table t2 add index idx_b(b);") + wg.Done() + }() + wg.Wait() + rows := tk.MustQuery("admin show ddl jobs 2;").Rows() + require.Len(t, rows, 2) + require.True(t, strings.Contains(rows[0][3].(string) /* job_type */, "ingest")) + require.True(t, strings.Contains(rows[1][3].(string) /* job_type */, "ingest")) + require.Equal(t, rows[0][7].(string) /* row_count */, "3") + require.Equal(t, rows[1][7].(string) /* row_count */, "3") + + tk.MustExec("set @@global.tidb_enable_dist_task = 1;") + + // test cancel is timely + enter := make(chan struct{}) + testfailpoint.EnableCall( + t, + "github.com/pingcap/tidb/pkg/lightning/backend/local/beforeExecuteRegionJob", + func(ctx context.Context) { + close(enter) + select { + case <-time.After(time.Second * 20): + case <-ctx.Done(): + } + }) + wg.Add(1) + go func() { + defer wg.Done() + err := tk2.ExecToErr("alter table t add index idx_ba(b, a);") + require.ErrorContains(t, err, "Cancelled DDL job") + }() + <-enter + jobID := tk.MustQuery("admin show ddl jobs 1;").Rows()[0][0].(string) + now := time.Now() + tk.MustExec("admin cancel ddl jobs " + jobID) + wg.Wait() + // cancel should be timely + require.Less(t, time.Since(now).Seconds(), 10.0) +} + func TestAddIndexDistPauseAndResume(t *testing.T) { t.Skip("unstable") // TODO(tangenta): fix this unstable test store := realtikvtest.CreateMockStoreAndSetup(t)