@@ -20,11 +20,13 @@ import (
20
20
"strings"
21
21
"sync/atomic"
22
22
"testing"
23
+ "time"
23
24
24
25
"github.com/ngaut/pools"
25
26
"github.com/pingcap/tidb/pkg/parser/ast"
26
27
"github.com/pingcap/tidb/pkg/sessionctx"
27
28
"github.com/pingcap/tidb/pkg/testkit"
29
+ "github.com/pingcap/tidb/pkg/ttl/cache"
28
30
"github.com/pingcap/tidb/pkg/ttl/ttlworker"
29
31
"github.com/pingcap/tidb/pkg/util"
30
32
"github.com/pingcap/tidb/pkg/util/logutil"
@@ -185,3 +187,64 @@ func TestGetSessionWithFault(t *testing.T) {
185
187
require .True (t , se != nil || err != nil )
186
188
}
187
189
}
190
+
191
+ func TestNewScanSession (t * testing.T ) {
192
+ _ , dom := testkit .CreateMockStoreAndDomain (t )
193
+ pool := newFaultSessionPool (dom .SysSessionPool ())
194
+ pool .setFault (newFaultWithFilter (func (s string ) bool { return false }, newFaultAfterCount (0 )))
195
+ se , err := ttlworker .GetSessionForTest (pool )
196
+ require .NoError (t , err )
197
+
198
+ _ , err = se .ExecuteSQL (context .Background (), "set @@tidb_distsql_scan_concurrency=123" )
199
+ require .NoError (t , err )
200
+ require .Equal (t , 123 , se .GetSessionVars ().DistSQLScanConcurrency ())
201
+
202
+ _ , err = se .ExecuteSQL (context .Background (), "set @@tidb_enable_paging=ON" )
203
+ require .NoError (t , err )
204
+ require .True (t , se .GetSessionVars ().EnablePaging )
205
+
206
+ for _ , errSQL := range []string {
207
+ "" ,
208
+ "set @@tidb_distsql_scan_concurrency=1" ,
209
+ "set @@tidb_enable_paging=OFF" ,
210
+ } {
211
+ t .Run ("test err in SQL: " + errSQL , func (t * testing.T ) {
212
+ var faultCnt atomic.Int64
213
+ pool .setFault (newFaultWithFilter (func (s string ) bool {
214
+ if s == errSQL && s != "" {
215
+ faultCnt .Add (1 )
216
+ return true
217
+ }
218
+ return false
219
+ }, newFaultAfterCount (0 )))
220
+ tblSe , restore , err := ttlworker .NewScanSession (se , & cache.PhysicalTable {}, time .Now ())
221
+ if errSQL == "" {
222
+ // success case
223
+ require .NoError (t , err )
224
+ require .NotNil (t , tblSe )
225
+ require .NotNil (t , restore )
226
+ require .Same (t , se , tblSe .Session )
227
+ require .Equal (t , int64 (0 ), faultCnt .Load ())
228
+
229
+ // NewScanSession should override @@dist_sql_scan_concurrency and @@tidb_enable_paging
230
+ require .Equal (t , 1 , se .GetSessionVars ().DistSQLScanConcurrency ())
231
+ require .False (t , se .GetSessionVars ().EnablePaging )
232
+
233
+ // restore should restore the session variables
234
+ restore ()
235
+ require .Equal (t , 123 , se .GetSessionVars ().DistSQLScanConcurrency ())
236
+ require .True (t , se .GetSessionVars ().EnablePaging )
237
+ } else {
238
+ // error case
239
+ require .Equal (t , int64 (1 ), faultCnt .Load ())
240
+ require .EqualError (t , err , "fault in test" )
241
+ require .Nil (t , tblSe )
242
+ require .Nil (t , restore )
243
+
244
+ // NewScanSession should not change session state if error occurs
245
+ require .Equal (t , 123 , se .GetSessionVars ().DistSQLScanConcurrency ())
246
+ require .True (t , se .GetSessionVars ().EnablePaging )
247
+ }
248
+ })
249
+ }
250
+ }
0 commit comments