@@ -25,6 +25,7 @@ import (
25
25
"github.com/pingcap/tidb/pkg/sessionctx/variable"
26
26
"github.com/pingcap/tidb/pkg/ttl/cache"
27
27
"github.com/pingcap/tidb/pkg/types"
28
+ "github.com/pingcap/tidb/pkg/util"
28
29
"github.com/pingcap/tidb/pkg/util/chunk"
29
30
"github.com/stretchr/testify/require"
30
31
)
@@ -446,3 +447,51 @@ func TestScanTaskCheck(t *testing.T) {
446
447
require .Equal (t , 1 , len (ch ))
447
448
require .Equal (t , "Total Rows: 1, Success Rows: 0, Error Rows: 0" , task .statistics .String ())
448
449
}
450
+
451
+ func TestScanTaskCancelStmt (t * testing.T ) {
452
+ task := & ttlScanTask {
453
+ ctx : context .Background (),
454
+ tbl : newMockTTLTbl (t , "t1" ),
455
+ TTLTask : & cache.TTLTask {
456
+ ExpireTime : time .UnixMilli (0 ),
457
+ ScanRangeStart : []types.Datum {types .NewIntDatum (0 )},
458
+ },
459
+ statistics : & ttlStatistics {},
460
+ }
461
+
462
+ testCancel := func (ctx context.Context , doCancel func ()) {
463
+ mockPool := newMockSessionPool (t )
464
+ startExec := make (chan struct {})
465
+ mockPool .se .sessionInfoSchema = newMockInfoSchema (task .tbl .TableInfo )
466
+ mockPool .se .executeSQL = func (_ context.Context , _ string , _ ... any ) ([]chunk.Row , error ) {
467
+ close (startExec )
468
+ select {
469
+ case <- mockPool .se .killed :
470
+ return nil , errors .New ("killed" )
471
+ case <- time .After (10 * time .Second ):
472
+ return nil , errors .New ("timeout" )
473
+ }
474
+ }
475
+ wg := util.WaitGroupWrapper {}
476
+ wg .Run (func () {
477
+ select {
478
+ case <- startExec :
479
+ case <- time .After (10 * time .Second ):
480
+ require .FailNow (t , "timeout" )
481
+ }
482
+ doCancel ()
483
+ })
484
+ r := task .doScan (ctx , nil , mockPool )
485
+ require .NotNil (t , r )
486
+ require .EqualError (t , r .err , "killed" )
487
+ wg .Wait ()
488
+ }
489
+
490
+ // test cancel with input context
491
+ ctx , cancel := context .WithCancel (context .Background ())
492
+ testCancel (ctx , cancel )
493
+
494
+ // test cancel with task context
495
+ task .ctx , cancel = context .WithCancel (context .Background ())
496
+ testCancel (context .Background (), cancel )
497
+ }
0 commit comments