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

Function TestPoolWithHooks

internal/pool/hooks_test.go:184–231  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

182}
183
184func TestPoolWithHooks(t *testing.T) {
185 // Create a pool with hooks
186 hookManager := NewPoolHookManager()
187 testHook := &TestHook{ShouldPool: true, ShouldAccept: true}
188 hookManager.AddHook(testHook)
189
190 opt := &Options{
191 Dialer: func(ctx context.Context) (net.Conn, error) {
192 return &net.TCPConn{}, nil // Mock connection
193 },
194 PoolSize: 1,
195 MaxConcurrentDials: 1,
196 DialTimeout: time.Second,
197 }
198
199 pool := NewConnPool(opt)
200 defer pool.Close()
201
202 // Add hook to pool after creation
203 pool.AddPoolHook(testHook)
204
205 // Verify hooks are initialized
206 manager := pool.hookManager.Load()
207 if manager == nil {
208 t.Error("Expected hookManager to be initialized")
209 }
210
211 if manager.GetHookCount() != 1 {
212 t.Errorf("Expected 1 hook in pool, got %d", manager.GetHookCount())
213 }
214
215 // Test adding hook to pool
216 additionalHook := &TestHook{ShouldPool: true, ShouldAccept: true}
217 pool.AddPoolHook(additionalHook)
218
219 manager = pool.hookManager.Load()
220 if manager.GetHookCount() != 2 {
221 t.Errorf("Expected 2 hooks after adding, got %d", manager.GetHookCount())
222 }
223
224 // Test removing hook from pool
225 pool.RemovePoolHook(additionalHook)
226
227 manager = pool.hookManager.Load()
228 if manager.GetHookCount() != 1 {
229 t.Errorf("Expected 1 hook after removing, got %d", manager.GetHookCount())
230 }
231}
232
233// TestCloseConnInvokesOnRemove guards against a regression where (*ConnPool).CloseConn
234// removed a connection from the pool without notifying pool hooks via OnRemove,

Callers

nothing calls this directly

Calls 9

AddHookMethod · 0.95
CloseMethod · 0.95
AddPoolHookMethod · 0.95
RemovePoolHookMethod · 0.95
NewPoolHookManagerFunction · 0.85
NewConnPoolFunction · 0.85
GetHookCountMethod · 0.80
LoadMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected