mirror of
https://github.com/redis/go-redis.git
synced 2025-12-02 06:22:31 +03:00
improve tests
This commit is contained in:
@@ -8,9 +8,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9/maintnotifications"
|
|
||||||
"github.com/redis/go-redis/v9/internal/pool"
|
"github.com/redis/go-redis/v9/internal/pool"
|
||||||
"github.com/redis/go-redis/v9/logging"
|
"github.com/redis/go-redis/v9/logging"
|
||||||
|
"github.com/redis/go-redis/v9/maintnotifications"
|
||||||
)
|
)
|
||||||
|
|
||||||
// mockNetConn implements net.Conn for testing
|
// mockNetConn implements net.Conn for testing
|
||||||
@@ -164,6 +164,10 @@ func TestEventDrivenHandoffIntegration(t *testing.T) {
|
|||||||
|
|
||||||
// Could be the original connection (now handed off) or a new one
|
// Could be the original connection (now handed off) or a new one
|
||||||
testPool.Put(ctx, conn3)
|
testPool.Put(ctx, conn3)
|
||||||
|
|
||||||
|
if !initConnCalled.Load() {
|
||||||
|
t.Error("InitConn should have been called during handoff")
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("ConcurrentHandoffs", func(t *testing.T) {
|
t.Run("ConcurrentHandoffs", func(t *testing.T) {
|
||||||
|
|||||||
@@ -400,8 +400,13 @@ func TestConnStateMachine_FIFOOrdering(t *testing.T) {
|
|||||||
var executionOrder []int
|
var executionOrder []int
|
||||||
var orderMu sync.Mutex
|
var orderMu sync.Mutex
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
var startBarrier sync.WaitGroup
|
|
||||||
startBarrier.Add(numGoroutines)
|
// Use channels to ensure deterministic queueing order
|
||||||
|
// Each goroutine waits for the previous one to queue before it queues
|
||||||
|
queuedChannels := make([]chan struct{}, numGoroutines)
|
||||||
|
for i := 0; i < numGoroutines; i++ {
|
||||||
|
queuedChannels[i] = make(chan struct{})
|
||||||
|
}
|
||||||
|
|
||||||
// Launch goroutines that will all wait
|
// Launch goroutines that will all wait
|
||||||
for i := 0; i < numGoroutines; i++ {
|
for i := 0; i < numGoroutines; i++ {
|
||||||
@@ -409,15 +414,19 @@ func TestConnStateMachine_FIFOOrdering(t *testing.T) {
|
|||||||
go func(id int) {
|
go func(id int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
// Wait for all goroutines to be ready
|
// Wait for previous goroutine to queue (except for goroutine 0)
|
||||||
startBarrier.Done()
|
if id > 0 {
|
||||||
startBarrier.Wait()
|
<-queuedChannels[id-1]
|
||||||
|
}
|
||||||
|
|
||||||
// Small stagger to ensure queue order
|
// Small delay to ensure the previous goroutine's AwaitAndTransition has been called
|
||||||
time.Sleep(time.Duration(id) * time.Millisecond)
|
time.Sleep(5 * time.Millisecond)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Signal that we're about to queue
|
||||||
|
close(queuedChannels[id])
|
||||||
|
|
||||||
// This should queue in FIFO order
|
// This should queue in FIFO order
|
||||||
_, err := sm.AwaitAndTransition(ctx, []ConnState{StateIdle}, StateInitializing)
|
_, err := sm.AwaitAndTransition(ctx, []ConnState{StateIdle}, StateInitializing)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -437,7 +446,8 @@ func TestConnStateMachine_FIFOOrdering(t *testing.T) {
|
|||||||
}(i)
|
}(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait a bit for all goroutines to queue up
|
// Wait for all goroutines to queue up
|
||||||
|
<-queuedChannels[numGoroutines-1]
|
||||||
time.Sleep(50 * time.Millisecond)
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
|
||||||
// Transition to READY to start processing the queue
|
// Transition to READY to start processing the queue
|
||||||
|
|||||||
Reference in New Issue
Block a user