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

Code before:
		require.NoError(t, err)
		_, err = db.Exec("INSERT INTO retry_test VALUES (1, 0)")
		require.NoError(t, err)

		step1T1Started := make(chan struct{})
		step2T2Committed := make(chan struct{})

		connect := func() *sql.Conn {
			conn, err := db.Conn(context.Background())
			require.NoError(t, err)
			if isOptimistic {
				_, err = conn.ExecContext(context.Background(), "SET tidb_txn_mode = 'optimistic'")
				require.NoError(t, err)
				// We cannot set `tidb_disable_txn_auto_retry` to `OFF` because it's already deprecated.
				// For the test, we just use failpoint to workaround this limitation.
				testfailpoint.Enable(t, "github.com/pingcap/tidb/pkg/sessiontxn/isolation/injectOptimisticTxnRetryable", "return(true)")
			}

			return conn
		}

		testResults = testResults[:0]
		var wg sync.WaitGroup
		// Transaction 1
		wg.Go(func() {
			defer wg.Done()
			conn := connect()
			defer conn.Close()

			_, err = conn.ExecContext(context.Background(), "BEGIN")
			require.NoError(t, err)
			close(step1T1Started)
			<-step2T2Committed
			_, err = conn.ExecContext(context.Background(), "UPDATE retry_test SET val = val + 10 WHERE id = 1")
			require.NoError(t, err)
			_, err = conn.ExecContext(context.Background(), "COMMIT")
			require.NoError(t, err)
		})
		// Transaction 2
		wg.Go(func() {
			defer wg.Done()
			<-step1T1Started
			conn := connect()
			defer conn.Close()

			_, err = conn.ExecContext(context.Background(), "BEGIN")
			require.NoError(t, err)
			_, err = conn.ExecContext(context.Background(), "UPDATE retry_test SET val = val + 20 WHERE id = 1")
			require.NoError(t, err)
			_, err = conn.ExecContext(context.Background(), "COMMIT")
			require.NoError(t, err)

Review comment (suggestion):
I remember it wil internally call `wg.Done()`. so we should not repeat it. (please check source code)

Code after:
		require.NoError(t, err)
		_, err = db.Exec("INSERT INTO retry_test VALUES (1, 0)")
		require.NoError(t, err)

		step1T1Started := make(chan struct{})
		step2T2Committed := make(chan struct{})

		connect := func() *sql.Conn {
			conn, err := db.Conn(context.Background())
			require.NoError(t, err)
			if isOptimistic {
				_, err = conn.ExecContext(context.Background(), "SET tidb_txn_mode = 'optimistic'")
				require.NoError(t, err)
				// We cannot set `tidb_disable_txn_auto_retry` to `OFF` because it's already deprecated.
				// For the test, we just use failpoint to workaround this limitation.
				testfailpoint.Enable(t, "github.com/pingcap/tidb/pkg/sessiontxn/isolation/injectOptimisticTxnRetryable", "return(true)")
			}

			return conn
		}

		testResults = testResults[:0]
		var wg sync.WaitGroup
		// Transaction 1
		wg.Go(func() {
			conn := connect()
			defer conn.Close()

			_, err = conn.ExecContext(context.Background(), "BEGIN")
			require.NoError(t, err)
			close(step1T1Started)
			<-step2T2Committed
			_, err = conn.ExecContext(context.Background(), "UPDATE retry_test SET val = val + 10 WHERE id = 1")
			require.NoError(t, err)
			_, err = conn.ExecContext(context.Background(), "COMMIT")
			require.NoError(t, err)
		})
		// Transaction 2
		wg.Go(func() {
			<-step1T1Started
			conn := connect()
			defer conn.Close()

			_, err = conn.ExecContext(context.Background(), "BEGIN")
			require.NoError(t, err)
			_, err = conn.ExecContext(context.Background(), "UPDATE retry_test SET val = val + 20 WHERE id = 1")
			require.NoError(t, err)
			_, err = conn.ExecContext(context.Background(), "COMMIT")
			require.NoError(t, err)

			close(step2T2Committed)