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

Function TestLazyExecuteWithParam

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

Source from the content-addressed store, hash-verified

434}
435
436func TestLazyExecuteWithParam(t *testing.T) {
437 ts := servertestkit.CreateTidbTestSuite(t)
438
439 mysqldriver := &mysqlcursor.MySQLDriver{}
440 rawConn, err := mysqldriver.Open(ts.GetDSNWithCursor(10))
441 require.NoError(t, err)
442 conn := rawConn.(mysqlcursor.Connection)
443 defer conn.Close()
444
445 _, err = conn.ExecContext(context.Background(), "drop table if exists t1", nil)
446 require.NoError(t, err)
447 _, err = conn.ExecContext(context.Background(), "create table t1(id int primary key, v int)", nil)
448 require.NoError(t, err)
449 rowCount := 1000
450 for i := 0; i < rowCount; i++ {
451 _, err = conn.ExecContext(context.Background(), fmt.Sprintf("insert into t1 values(%d, %d)", i, i), nil)
452 require.NoError(t, err)
453 }
454
455 _, err = conn.ExecContext(context.Background(), "set tidb_enable_lazy_cursor_fetch = 'ON'", nil)
456 require.NoError(t, err)
457
458 require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/server/avoidEagerCursorFetch", "return"))
459 defer func() {
460 require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/server/avoidEagerCursorFetch"))
461 }()
462
463 // Normal execute. Simple table reader.
464 execTimes := 0
465outerLoop:
466 for execTimes < 50 {
467 execTimes++
468 rawStmt, err := conn.Prepare("select id from t1 where v >= ? and v <= ? order by id")
469 require.NoError(t, err)
470 stmt := rawStmt.(mysqlcursor.Statement)
471
472 // This query will return `rowCount - 500` rows and use cursor fetch.
473 rows, err := stmt.QueryContext(context.Background(), []driver.NamedValue{{Value: int64(500)}, {Value: int64(10000)}})
474 require.NoError(t, err)
475
476 dest := make([]driver.Value, 1)
477 fetchRowCount := int64(0)
478
479 for {
480 // it'll send `FETCH` commands for every 10 rows.
481 err := rows.Next(dest)
482 if err != nil {
483 switch err {
484 case io.EOF:
485 require.Equal(t, int64(rowCount-500), fetchRowCount)
486 rows.Close()
487 break outerLoop
488 default:
489 require.NoError(t, err)
490 }
491 }
492 require.Equal(t, fetchRowCount+500, dest[0])
493 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