(t *testing.T)
| 462 | } |
| 463 | |
| 464 | func TestConnStateMachine_FIFOWithFastPath(t *testing.T) { |
| 465 | sm := NewConnStateMachine() |
| 466 | sm.Transition(StateIdle) // Start in READY so fast path is available |
| 467 | |
| 468 | const numGoroutines = 10 |
| 469 | var executionOrder []int |
| 470 | var orderMu sync.Mutex |
| 471 | var wg sync.WaitGroup |
| 472 | var startBarrier sync.WaitGroup |
| 473 | startBarrier.Add(numGoroutines) |
| 474 | |
| 475 | // Launch goroutines that will all try the fast path |
| 476 | for i := 0; i < numGoroutines; i++ { |
| 477 | wg.Add(1) |
| 478 | go func(id int) { |
| 479 | defer wg.Done() |
| 480 | |
| 481 | // Wait for all goroutines to be ready |
| 482 | startBarrier.Done() |
| 483 | startBarrier.Wait() |
| 484 | |
| 485 | // Small stagger to establish arrival order |
| 486 | time.Sleep(time.Duration(id) * 100 * time.Microsecond) |
| 487 | |
| 488 | ctx := context.Background() |
| 489 | |
| 490 | // This might use fast path (CAS) or slow path (queue) |
| 491 | _, err := sm.AwaitAndTransition(ctx, []ConnState{StateIdle}, StateInitializing) |
| 492 | if err != nil { |
| 493 | t.Errorf("Goroutine %d: failed to transition: %v", id, err) |
| 494 | return |
| 495 | } |
| 496 | |
| 497 | // Record execution order |
| 498 | orderMu.Lock() |
| 499 | executionOrder = append(executionOrder, id) |
| 500 | orderMu.Unlock() |
| 501 | |
| 502 | t.Logf("Goroutine %d: executed (position %d)", id, len(executionOrder)) |
| 503 | |
| 504 | // Simulate work |
| 505 | time.Sleep(5 * time.Millisecond) |
| 506 | |
| 507 | // Transition back to READY to allow next waiter |
| 508 | sm.Transition(StateIdle) |
| 509 | }(i) |
| 510 | } |
| 511 | |
| 512 | wg.Wait() |
| 513 | |
| 514 | t.Logf("Execution order: %v", executionOrder) |
| 515 | |
| 516 | // Check if FIFO was maintained |
| 517 | // With the current fast-path implementation, this might NOT be FIFO |
| 518 | fifoViolations := 0 |
| 519 | for i := 0; i < numGoroutines; i++ { |
| 520 | if executionOrder[i] != i { |
| 521 | fifoViolations++ |
nothing calls this directly
no test coverage detected