From 20ad05f68640f242e35e150400a52ba99a8d99c8 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Mon, 23 Sep 2024 14:44:08 +0800 Subject: [PATCH] executor: add test for issue 55500 and it has been fixed by #53489 (#55503) (#56049) ref pingcap/tidb#55500 --- executor/executor_test.go | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/executor/executor_test.go b/executor/executor_test.go index 9ef7f77009cc3..9b9c4b841b46d 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -19,6 +19,7 @@ import ( "context" "fmt" "math" + "math/rand" "path/filepath" "reflect" "runtime" @@ -6707,3 +6708,64 @@ func TestIssue48756(t *testing.T) { "Warning 1105 ", )) } + +func TestQueryWithKill(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test;") + tk.MustExec("drop table if exists tkq;") + tk.MustExec("create table tkq (a int key, b int, index idx_b(b));") + tk.MustExec("insert into tkq values (1,1);") + var wg sync.WaitGroup + ch := make(chan context.CancelFunc, 1024) + testDuration := time.Second * 10 + for i := 0; i < 10; i++ { + wg.Add(1) + go func() { + defer wg.Done() + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test;") + start := time.Now() + for { + ctx, cancel := context.WithCancel(context.Background()) + ch <- cancel + rs, err := tk.ExecWithContext(ctx, "select a from tkq where b = 1;") + if err == nil { + require.NotNil(t, rs) + rows, err := session.ResultSetToStringSlice(ctx, tk.Session(), rs) + if err == nil { + require.Equal(t, 1, len(rows)) + require.Equal(t, 1, len(rows[0])) + require.Equal(t, "1", fmt.Sprintf("%v", rows[0][0])) + } + } + if err != nil { + require.Equal(t, context.Canceled, err) + } + if rs != nil { + rs.Close() + } + if time.Since(start) > testDuration { + return + } + } + }() + } + wg.Add(1) + go func() { + defer wg.Done() + for { + select { + case cancel := <-ch: + // mock for random kill query + if len(ch) < 5 { + time.Sleep(time.Duration(rand.Intn(1000)) * time.Nanosecond) + } + cancel() + case <-time.After(time.Second): + return + } + } + }() + wg.Wait() +}