MCPcopy
hub / github.com/pingcap/tidb / TestLazyExecuteSelection

Function TestLazyExecuteSelection

pkg/server/tests/cursor/cursor_test.go:374–434  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

372}
373
374func TestLazyExecuteSelection(t *testing.T) {
375 ts := servertestkit.CreateTidbTestSuite(t)
376
377 mysqldriver := &mysqlcursor.MySQLDriver{}
378 rawConn, err := mysqldriver.Open(ts.GetDSNWithCursor(10))
379 require.NoError(t, err)
380 conn := rawConn.(mysqlcursor.Connection)
381 defer conn.Close()
382
383 _, err = conn.ExecContext(context.Background(), "drop table if exists t1", nil)
384 require.NoError(t, err)
385 _, err = conn.ExecContext(context.Background(), "create table t1(id int primary key, v int)", nil)
386 require.NoError(t, err)
387 rowCount := 1000
388 for i := 0; i < rowCount; i++ {
389 _, err = conn.ExecContext(context.Background(), fmt.Sprintf("insert into t1 values(%d, %d)", i, i), nil)
390 require.NoError(t, err)
391 }
392
393 _, err = conn.ExecContext(context.Background(), "set tidb_enable_lazy_cursor_fetch = 'ON'", nil)
394 require.NoError(t, err)
395
396 require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/server/avoidEagerCursorFetch", "return"))
397 defer func() {
398 require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/server/avoidEagerCursorFetch"))
399 }()
400
401 // Normal execute. Simple table reader.
402 execTimes := 0
403outerLoop:
404 for execTimes < 50 {
405 execTimes++
406 rawStmt, err := conn.Prepare("select id from t1 where v >= ? order by id")
407 require.NoError(t, err)
408 stmt := rawStmt.(mysqlcursor.Statement)
409
410 // This query will return `rowCount - 500` rows and use cursor fetch.
411 rows, err := stmt.QueryContext(context.Background(), []driver.NamedValue{{Value: int64(500)}})
412 require.NoError(t, err)
413
414 dest := make([]driver.Value, 1)
415 fetchRowCount := int64(0)
416
417 for {
418 // it'll send `FETCH` commands for every 10 rows.
419 err := rows.Next(dest)
420 if err != nil {
421 switch err {
422 case io.EOF:
423 require.Equal(t, int64(rowCount-500), fetchRowCount)
424 rows.Close()
425 break outerLoop
426 default:
427 require.NoError(t, err)
428 }
429 }
430 require.Equal(t, fetchRowCount+500, dest[0])
431 fetchRowCount++

Callers

nothing calls this directly

Calls 11

CreateTidbTestSuiteFunction · 0.92
GetDSNWithCursorMethod · 0.80
EnableMethod · 0.80
DisableMethod · 0.80
PrepareMethod · 0.80
OpenMethod · 0.65
CloseMethod · 0.65
ExecContextMethod · 0.65
QueryContextMethod · 0.65
NextMethod · 0.65
EqualMethod · 0.65

Tested by

no test coverage detected