@@ -401,32 +401,16 @@ func TestConnStateMachine_FIFOOrdering(t *testing.T) {
401401 var orderMu sync.Mutex
402402 var wg sync.WaitGroup
403403
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- }
410-
411- // Launch goroutines that will all wait
404+ // Launch goroutines one at a time, ensuring each is queued before launching the next
412405 for i := 0 ; i < numGoroutines ; i ++ {
413406 wg .Add (1 )
407+ expectedWaiters := int32 (i + 1 )
408+
414409 go func (id int ) {
415410 defer wg .Done ()
416411
417- // Wait for previous goroutine to queue (except for goroutine 0)
418- if id > 0 {
419- <- queuedChannels [id - 1 ]
420- }
421-
422- // Small delay to ensure the previous goroutine's AwaitAndTransition has been called
423- time .Sleep (5 * time .Millisecond )
424-
425412 ctx := context .Background ()
426413
427- // Signal that we're about to queue
428- close (queuedChannels [id ])
429-
430414 // This should queue in FIFO order
431415 _ , err := sm .AwaitAndTransition (ctx , []ConnState {StateIdle }, StateInitializing )
432416 if err != nil {
@@ -441,16 +425,30 @@ func TestConnStateMachine_FIFOOrdering(t *testing.T) {
441425
442426 t .Logf ("Goroutine %d: executed (position %d)" , id , len (executionOrder ))
443427
444- // Transition back to READY to allow next waiter
428+ // Transition back to IDLE to allow next waiter
445429 sm .Transition (StateIdle )
446430 }(i )
431+
432+ // Wait until this goroutine has been queued before launching the next
433+ // Poll the waiter count to ensure the goroutine is actually queued
434+ timeout := time .After (100 * time .Millisecond )
435+ for {
436+ if sm .waiterCount .Load () >= expectedWaiters {
437+ break
438+ }
439+ select {
440+ case <- timeout :
441+ t .Fatalf ("Timeout waiting for goroutine %d to queue" , i )
442+ case <- time .After (1 * time .Millisecond ):
443+ // Continue polling
444+ }
445+ }
447446 }
448447
449- // Wait for all goroutines to queue up
450- <- queuedChannels [numGoroutines - 1 ]
451- time .Sleep (50 * time .Millisecond )
448+ // Give all goroutines time to fully settle in the queue
449+ time .Sleep (10 * time .Millisecond )
452450
453- // Transition to READY to start processing the queue
451+ // Transition to IDLE to start processing the queue
454452 sm .Transition (StateIdle )
455453
456454 wg .Wait ()
0 commit comments