Skip to content

Commit 73ff273

Browse files
committed
fix race in test
1 parent b42c19b commit 73ff273

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

internal/pool/pool_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"net"
77
"sync"
8+
"sync/atomic"
89
"testing"
910
"time"
1011

@@ -442,9 +443,9 @@ func TestDialerRetryConfiguration(t *testing.T) {
442443
ctx := context.Background()
443444

444445
t.Run("CustomDialerRetries", func(t *testing.T) {
445-
attempts := 0
446+
var attempts int64
446447
failingDialer := func(ctx context.Context) (net.Conn, error) {
447-
attempts++
448+
atomic.AddInt64(&attempts, 1)
448449
return nil, errors.New("dial failed")
449450
}
450451

@@ -465,18 +466,19 @@ func TestDialerRetryConfiguration(t *testing.T) {
465466

466467
// Should have attempted at least 3 times (DialerRetries = 3)
467468
// There might be additional attempts due to pool logic
468-
if attempts < 3 {
469-
t.Errorf("Expected at least 3 dial attempts, got %d", attempts)
469+
finalAttempts := atomic.LoadInt64(&attempts)
470+
if finalAttempts < 3 {
471+
t.Errorf("Expected at least 3 dial attempts, got %d", finalAttempts)
470472
}
471-
if attempts > 6 {
472-
t.Errorf("Expected around 3 dial attempts, got %d (too many)", attempts)
473+
if finalAttempts > 6 {
474+
t.Errorf("Expected around 3 dial attempts, got %d (too many)", finalAttempts)
473475
}
474476
})
475477

476478
t.Run("DefaultDialerRetries", func(t *testing.T) {
477-
attempts := 0
479+
var attempts int64
478480
failingDialer := func(ctx context.Context) (net.Conn, error) {
479-
attempts++
481+
atomic.AddInt64(&attempts, 1)
480482
return nil, errors.New("dial failed")
481483
}
482484

@@ -495,8 +497,9 @@ func TestDialerRetryConfiguration(t *testing.T) {
495497
}
496498

497499
// Should have attempted 5 times (default DialerRetries = 5)
498-
if attempts != 5 {
499-
t.Errorf("Expected 5 dial attempts (default), got %d", attempts)
500+
finalAttempts := atomic.LoadInt64(&attempts)
501+
if finalAttempts != 5 {
502+
t.Errorf("Expected 5 dial attempts (default), got %d", finalAttempts)
500503
}
501504
})
502505
}

0 commit comments

Comments
 (0)