MCPcopy
hub / github.com/redis/go-redis / TestWantConn_RaceConditionNilContext

Function TestWantConn_RaceConditionNilContext

internal/pool/want_conn_test.go:457–505  ·  view source on GitHub ↗

TestWantConn_RaceConditionNilContext tests the race condition where getCtxForDial can return nil after the context is cancelled. This test verifies that the fix in newConn handles nil context gracefully.

(t *testing.T)

Source from the content-addressed store, hash-verified

455// getCtxForDial can return nil after the context is cancelled.
456// This test verifies that the fix in newConn handles nil context gracefully.
457func TestWantConn_RaceConditionNilContext(t *testing.T) {
458 // This test simulates the race condition described in the issue:
459 // 1. Main goroutine creates a wantConn with a context
460 // 2. Background goroutine starts but hasn't called getCtxForDial yet
461 // 3. Main goroutine times out and calls cancel(), setting w.ctx to nil
462 // 4. Background goroutine calls getCtxForDial() and gets nil
463 // 5. Background goroutine calls newConn(nil, true) which should not panic
464
465 dialCtx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
466 defer cancel()
467
468 w := &wantConn{
469 ctx: dialCtx,
470 cancelCtx: cancel,
471 result: make(chan wantConnResult, 1),
472 }
473
474 // Simulate the race condition by canceling the context
475 // and then trying to get it
476 var wg sync.WaitGroup
477 wg.Add(1)
478
479 go func() {
480 defer wg.Done()
481 // Small delay to ensure cancel happens first
482 time.Sleep(10 * time.Millisecond)
483
484 // This should return nil after cancel
485 ctx := w.getCtxForDial()
486
487 // Verify that we got nil context
488 if ctx != nil {
489 t.Errorf("Expected nil context after cancel, got %v", ctx)
490 }
491 }()
492
493 // Cancel the context immediately
494 w.cancel()
495
496 wg.Wait()
497
498 // Verify the wantConn state
499 if w.isOngoing() {
500 t.Error("wantConn should be marked as done after cancel")
501 }
502 if w.getCtxForDial() != nil {
503 t.Error("wantConn.ctx should be nil after cancel")
504 }
505}
506
507// TestWantConnQueue_dropFrontDone_EmptyQueue tests dropFrontDone on an empty queue.
508func TestWantConnQueue_dropFrontDone_EmptyQueue(t *testing.T) {

Callers

nothing calls this directly

Calls 7

getCtxForDialMethod · 0.95
cancelMethod · 0.95
isOngoingMethod · 0.95
WithTimeoutMethod · 0.80
WaitMethod · 0.80
AddMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected