Skip to content

Commit 736bfe6

Browse files
committed
pkg/server/tests: stabilize TestAuditPluginRetrying with deterministic auto-commit retries
- inject unistore failpoint in auto-commit branch - protect audit result collection with a mutex to avoid concurrent append races - switch concurrent UPDATE to and close each - test-only change in , no production logic touched Signed-off-by: yibin87 <huyibin@pingcap.com>
1 parent 6084333 commit 736bfe6

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

pkg/server/tests/commontest/tidb_test.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3517,6 +3517,24 @@ func TestAuditPluginRetrying(t *testing.T) {
35173517
retrying bool
35183518
}
35193519
testResults := make([]normalTest, 0)
3520+
var testResultsMu sync.Mutex
3521+
appendResult := func(audit normalTest) {
3522+
testResultsMu.Lock()
3523+
defer testResultsMu.Unlock()
3524+
testResults = append(testResults, audit)
3525+
}
3526+
resetResults := func() {
3527+
testResultsMu.Lock()
3528+
defer testResultsMu.Unlock()
3529+
testResults = testResults[:0]
3530+
}
3531+
getResults := func() []normalTest {
3532+
testResultsMu.Lock()
3533+
defer testResultsMu.Unlock()
3534+
results := make([]normalTest, len(testResults))
3535+
copy(results, testResults)
3536+
return results
3537+
}
35203538

35213539
onGeneralEvent := func(ctx context.Context, sctx *variable.SessionVars, event plugin.GeneralEvent, cmd string) {
35223540
// Only consider the Completed event
@@ -3529,7 +3547,7 @@ func TestAuditPluginRetrying(t *testing.T) {
35293547
audit.retrying = retrying.(bool)
35303548
}
35313549
audit.sql = sctx.StmtCtx.OriginalSQL
3532-
testResults = append(testResults, audit)
3550+
appendResult(audit)
35333551
}
35343552
plugin.LoadPluginForTest(t, onGeneralEvent)
35353553
defer plugin.Shutdown(context.Background())
@@ -3546,25 +3564,29 @@ func TestAuditPluginRetrying(t *testing.T) {
35463564
_, err = db.Exec("INSERT INTO auto_retry_test VALUES (1, 0)")
35473565
require.NoError(t, err)
35483566

3549-
testResults = testResults[:0]
3567+
resetResults()
3568+
// Inject deterministic write conflicts to make statement retries observable in auto-commit mode.
3569+
testfailpoint.Enable(t, "github.com/pingcap/tidb/pkg/store/mockstore/unistore/tikv/pessimisticLockReturnWriteConflict", "10*return(true)")
35503570
// a big enough concurrency to trigger retries
35513571
concurrency := 500
35523572
var wg sync.WaitGroup
35533573
for range concurrency {
35543574
wg.Add(1)
35553575
conn, err := db.Conn(context.Background())
35563576
require.NoError(t, err)
3557-
go func() {
3577+
go func(conn *sql.Conn) {
35583578
defer wg.Done()
3559-
_, err := conn.QueryContext(context.Background(), "UPDATE auto_retry_test SET val = val + 1 WHERE id = 1")
3579+
defer conn.Close()
3580+
_, err := conn.ExecContext(context.Background(), "UPDATE auto_retry_test SET val = val + 1 WHERE id = 1")
35603581
require.NoError(t, err)
3561-
}()
3582+
}(conn)
35623583
}
35633584
wg.Wait()
35643585

3565-
require.Greater(t, len(testResults), concurrency)
3586+
results := getResults()
3587+
require.Greater(t, len(results), concurrency)
35663588
nonRetryingCount := 0
3567-
for _, res := range testResults {
3589+
for _, res := range results {
35683590
if !res.retrying {
35693591
nonRetryingCount++
35703592
}
@@ -3597,7 +3619,7 @@ func TestAuditPluginRetrying(t *testing.T) {
35973619
return conn
35983620
}
35993621

3600-
testResults = testResults[:0]
3622+
resetResults()
36013623
var wg sync.WaitGroup
36023624
wg.Add(2)
36033625
// Transaction 1
@@ -3633,9 +3655,10 @@ func TestAuditPluginRetrying(t *testing.T) {
36333655
}()
36343656
wg.Wait()
36353657

3658+
results := getResults()
36363659
retryingCount := 0
36373660
nonRetryingCount := 0
3638-
for _, res := range testResults {
3661+
for _, res := range results {
36393662
if res.retrying {
36403663
retryingCount++
36413664
} else {
@@ -3665,7 +3688,7 @@ func TestAuditPluginRetrying(t *testing.T) {
36653688
ts.RunTests(t, nil, func(dbt *testkit.DBTestKit) {
36663689
db := dbt.GetDB()
36673690

3668-
testResults = testResults[:0]
3691+
resetResults()
36693692
runExplicitTransactionRetry(db, true)
36703693
})
36713694
}

0 commit comments

Comments
 (0)