Repository: pingcap/tidb
File: pkg/server/tests/commontest/tidb_test.go (Go)

Code before:
	_, err = conn.ExecContext(context.Background(), "commit")
	// because TiKV responses UndeterminedResult, the connection should be disconnected without reporting mysql error.
	require.EqualError(t, err, "invalid connection")
}

func TestIssue62673(t *testing.T) {
	ts := servertestkit.CreateTidbTestSuite(t)
	ts.RunTests(t, func(c *mysql.Config) {
		c.AllowAllFiles = true
	}, func(dbt *testkit.DBTestKit) {
		ctx := context.Background()

		// Generate a big local file
		filePath := filepath.Join(t.TempDir(), "big_file.txt")
		mysql.RegisterLocalFile(filePath)
		for i := range 1000 {
			err := os.WriteFile(filePath, fmt.Appendf(nil, "%d\n", i), os.ModePerm)
			require.NoError(t, err)
		}

		conn, err := dbt.GetDB().Conn(ctx)
		require.NoError(t, err)

		testserverclient.MustExec(ctx, t, conn, "create table t (a int)")

		err = failpoint.Enable("github.com/pingcap/tidb/pkg/executor/InCommitWork", "panic(\"mock panic\")")
		require.NoError(t, err)
		defer failpoint.Disable("github.com/pingcap/tidb/pkg/executor/InCommitWork")

		testStart := time.Now()
		testDuration := time.Second
		for time.Since(testStart) < testDuration {
			_, err = conn.ExecContext(ctx, fmt.Sprintf("LOAD DATA LOCAL INFILE '%s' INTO TABLE t", filePath))
			require.Error(t, err)

			testserverclient.MustQuery(ctx, t, ts.TestServerClient, conn, "SELECT COUNT(*) FROM t")
		}
	})
}


Review comment (bug):
1. we have `testfailpoint` package to simplify these lines
2. Why this is panic? The effect of panic is not clear, maybe `sleep` is better

Code after:

	_, err = conn.ExecContext(context.Background(), "insert into t values(1)")
	require.NoError(t, err)

	require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/store/mockstore/unistore/rpcCommitResult", `return("undeterminedResult")`))
	defer func() {
		require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/store/mockstore/unistore/rpcCommitResult"))
	}()

	_, err = conn.ExecContext(context.Background(), "commit")
	// because TiKV responses UndeterminedResult, the connection should be disconnected without reporting mysql error.
	require.EqualError(t, err, "invalid connection")
}

func TestAuditPluginInfoForStarting(t *testing.T) {
	ts := servertestkit.CreateTidbTestSuite(t)

	type normalTest struct {
		sql         string
		rows        uint64
		stmtType    string
		dbs         string
		tables      string
		cmd         string
		event       plugin.GeneralEvent
		params      string
		redactedSQL string
		stmtID      uint32
		currentDB   string
	}

	var dbNames, tableNames []string
	var testResults []normalTest

	onGeneralEvent := func(ctx context.Context, sctx *variable.SessionVars, event plugin.GeneralEvent, cmd string) {
		dbNames = dbNames[:0]
		tableNames = tableNames[:0]
		for _, value := range sctx.StmtCtx.Tables {
			dbNames = append(dbNames, value.DB)
			tableNames = append(tableNames, value.Table)
		}
		redactedSQL, _ := sctx.StmtCtx.SQLDigest()
		audit := normalTest{
			sql:         sctx.StmtCtx.OriginalSQL,
			rows:        sctx.StmtCtx.AffectedRows(),
			stmtType:    sctx.StmtCtx.StmtType,
			dbs:         strings.Join(dbNames, ","),
			tables:      strings.Join(tableNames, ","),
			cmd:         cmd,
			event:       event,
			params:      sctx.PlanCacheParams.String(),