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

Function TestConnectionHook

maintnotifications/pool_hook_test.go:119–1009  ·  view source on GitHub ↗

TestConnectionHook tests the Redis connection processor functionality

(t *testing.T)

Source from the content-addressed store, hash-verified

117
118// TestConnectionHook tests the Redis connection processor functionality
119func TestConnectionHook(t *testing.T) {
120 // Create a base dialer for testing
121 baseDialer := func(ctx context.Context, network, addr string) (net.Conn, error) {
122 return &mockNetConn{addr: addr}, nil
123 }
124
125 t.Run("SuccessfulEventDrivenHandoff", func(t *testing.T) {
126 config := &Config{
127 Mode: ModeAuto,
128 EndpointType: EndpointTypeAuto,
129 MaxWorkers: 1, // Use only 1 worker to ensure synchronization
130 HandoffQueueSize: 10, // Explicit queue size to avoid 0-size queue
131 MaxHandoffRetries: 3,
132 }
133 processor := NewPoolHook(baseDialer, "tcp", config, nil)
134 defer processor.Shutdown(context.Background())
135
136 conn := createMockPoolConnection()
137 if err := conn.MarkForHandoff("new-endpoint:6379", 12345); err != nil {
138 t.Fatalf("Failed to mark connection for handoff: %v", err)
139 }
140
141 // Verify connection is marked for handoff
142 if !conn.ShouldHandoff() {
143 t.Fatal("Connection should be marked for handoff")
144 }
145 // Set a mock initialization function with synchronization
146 initConnCalled := make(chan bool, 1)
147 proceedWithInit := make(chan bool, 1)
148 initConnFunc := func(ctx context.Context, cn *pool.Conn) error {
149 select {
150 case initConnCalled <- true:
151 default:
152 }
153 // Wait for test to proceed
154 <-proceedWithInit
155 return nil
156 }
157 conn.SetInitConnFunc(initConnFunc)
158
159 ctx := context.Background()
160 shouldPool, shouldRemove, err := processor.OnPut(ctx, conn)
161 if err != nil {
162 t.Errorf("OnPut should not error: %v", err)
163 }
164
165 // Should pool the connection immediately (handoff queued)
166 if !shouldPool {
167 t.Error("Connection should be pooled immediately with event-driven handoff")
168 }
169 if shouldRemove {
170 t.Error("Connection should not be removed when queuing handoff")
171 }
172
173 // Wait for initialization to be called (indicates handoff started)
174 select {
175 case <-initConnCalled:
176 // Good, initialization was called

Callers

nothing calls this directly

Calls 15

ShutdownMethod · 0.95
MarkForHandoffMethod · 0.95
ShouldHandoffMethod · 0.95
SetInitConnFuncMethod · 0.95
OnPutMethod · 0.95
GetPendingMapMethod · 0.95
GetIDMethod · 0.95
IsUsableMethod · 0.95
OnGetMethod · 0.95
MarkQueuedForHandoffMethod · 0.95
ClearHandoffStateMethod · 0.95
SetUsableMethod · 0.95

Tested by

no test coverage detected