Skip to content

Commit 826d78d

Browse files
test: Fix race condition in TestStreamRowsHeartbeat (vitessio#18414)
Signed-off-by: siddharth16396 <[email protected]>
1 parent c1cda2b commit 826d78d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

go/vt/vttablet/tabletserver/vstreamer/rowstreamer_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"regexp"
2323
"strconv"
24+
"sync/atomic"
2425
"testing"
2526
"time"
2627

@@ -579,7 +580,7 @@ func TestStreamRowsHeartbeat(t *testing.T) {
579580
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
580581
defer cancel()
581582

582-
heartbeatCount := 0
583+
var heartbeatCount int32
583584
dataReceived := false
584585

585586
var options binlogdatapb.VStreamOptions
@@ -589,9 +590,9 @@ func TestStreamRowsHeartbeat(t *testing.T) {
589590

590591
err := engine.StreamRows(ctx, "select * from t1", nil, func(rows *binlogdatapb.VStreamRowsResponse) error {
591592
if rows.Heartbeat {
592-
heartbeatCount++
593+
atomic.AddInt32(&heartbeatCount, 1)
593594
// After receiving at least 3 heartbeats, we can be confident the fix is working
594-
if heartbeatCount >= 3 {
595+
if atomic.LoadInt32(&heartbeatCount) >= 3 {
595596
cancel()
596597
return nil
597598
}
@@ -616,7 +617,7 @@ func TestStreamRowsHeartbeat(t *testing.T) {
616617
// This is the critical test: we should receive multiple heartbeats
617618
// Without the fix (missing for loop), we would only get 1 heartbeat
618619
// With the fix, we should get at least 3 heartbeats
619-
if heartbeatCount < 3 {
620+
if atomic.LoadInt32(&heartbeatCount) < 3 {
620621
t.Errorf("expected at least 3 heartbeats, got %d. This indicates the heartbeat goroutine is not running continuously", heartbeatCount)
621622
}
622623
}

0 commit comments

Comments
 (0)