Skip to content

Commit 84e856e

Browse files
committed
improve tests
1 parent 03b0003 commit 84e856e

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

async_handoff_integration_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/redis/go-redis/v9/maintnotifications"
1211
"github.com/redis/go-redis/v9/internal/pool"
1312
"github.com/redis/go-redis/v9/logging"
13+
"github.com/redis/go-redis/v9/maintnotifications"
1414
)
1515

1616
// mockNetConn implements net.Conn for testing
@@ -80,7 +80,7 @@ func TestEventDrivenHandoffIntegration(t *testing.T) {
8080
var initConnCalled atomic.Bool
8181
initConnStarted := make(chan struct{})
8282
initConnFunc := func(ctx context.Context, cn *pool.Conn) error {
83-
close(initConnStarted) // Signal that InitConn has started
83+
close(initConnStarted) // Signal that InitConn has started
8484
time.Sleep(50 * time.Millisecond) // Add delay to keep handoff pending
8585
initConnCalled.Store(true)
8686
return nil
@@ -164,6 +164,10 @@ func TestEventDrivenHandoffIntegration(t *testing.T) {
164164

165165
// Could be the original connection (now handed off) or a new one
166166
testPool.Put(ctx, conn3)
167+
168+
if !initConnCalled.Load() {
169+
t.Error("InitConn should have been called during handoff")
170+
}
167171
})
168172

169173
t.Run("ConcurrentHandoffs", func(t *testing.T) {

internal/pool/conn_state_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,24 +400,33 @@ func TestConnStateMachine_FIFOOrdering(t *testing.T) {
400400
var executionOrder []int
401401
var orderMu sync.Mutex
402402
var wg sync.WaitGroup
403-
var startBarrier sync.WaitGroup
404-
startBarrier.Add(numGoroutines)
403+
404+
// Use channels to ensure deterministic queueing order
405+
// Each goroutine waits for the previous one to queue before it queues
406+
queuedChannels := make([]chan struct{}, numGoroutines)
407+
for i := 0; i < numGoroutines; i++ {
408+
queuedChannels[i] = make(chan struct{})
409+
}
405410

406411
// Launch goroutines that will all wait
407412
for i := 0; i < numGoroutines; i++ {
408413
wg.Add(1)
409414
go func(id int) {
410415
defer wg.Done()
411416

412-
// Wait for all goroutines to be ready
413-
startBarrier.Done()
414-
startBarrier.Wait()
417+
// Wait for previous goroutine to queue (except for goroutine 0)
418+
if id > 0 {
419+
<-queuedChannels[id-1]
420+
}
415421

416-
// Small stagger to ensure queue order
417-
time.Sleep(time.Duration(id) * time.Millisecond)
422+
// Small delay to ensure the previous goroutine's AwaitAndTransition has been called
423+
time.Sleep(5 * time.Millisecond)
418424

419425
ctx := context.Background()
420426

427+
// Signal that we're about to queue
428+
close(queuedChannels[id])
429+
421430
// This should queue in FIFO order
422431
_, err := sm.AwaitAndTransition(ctx, []ConnState{StateIdle}, StateInitializing)
423432
if err != nil {
@@ -437,7 +446,8 @@ func TestConnStateMachine_FIFOOrdering(t *testing.T) {
437446
}(i)
438447
}
439448

440-
// Wait a bit for all goroutines to queue up
449+
// Wait for all goroutines to queue up
450+
<-queuedChannels[numGoroutines-1]
441451
time.Sleep(50 * time.Millisecond)
442452

443453
// Transition to READY to start processing the queue

0 commit comments

Comments
 (0)