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

Function TestMovedAndAskErrorAddressExtraction

internal/proto/redis_errors_test.go:266–326  ·  view source on GitHub ↗

TestMovedAndAskErrorAddressExtraction tests address extraction from MOVED/ASK errors

(t *testing.T)

Source from the content-addressed store, hash-verified

264
265// TestMovedAndAskErrorAddressExtraction tests address extraction from MOVED/ASK errors
266func TestMovedAndAskErrorAddressExtraction(t *testing.T) {
267 tests := []struct {
268 name string
269 errorMsg string
270 expectedAddr string
271 }{
272 {
273 name: "MOVED with IP address",
274 errorMsg: "MOVED 3999 127.0.0.1:6381",
275 expectedAddr: "127.0.0.1:6381",
276 },
277 {
278 name: "MOVED with hostname",
279 errorMsg: "MOVED 3999 redis-node-1:6379",
280 expectedAddr: "redis-node-1:6379",
281 },
282 {
283 name: "ASK with IP address",
284 errorMsg: "ASK 3999 192.168.1.100:6380",
285 expectedAddr: "192.168.1.100:6380",
286 },
287 {
288 name: "ASK with hostname",
289 errorMsg: "ASK 3999 redis-node-2:6379",
290 expectedAddr: "redis-node-2:6379",
291 },
292 }
293
294 for _, tt := range tests {
295 t.Run(tt.name, func(t *testing.T) {
296 err := parseTypedRedisError(tt.errorMsg)
297
298 var addr string
299 if movedErr, ok := IsMovedError(err); ok {
300 addr = movedErr.Addr()
301 } else if askErr, ok := IsAskError(err); ok {
302 addr = askErr.Addr()
303 } else {
304 t.Fatalf("Error is neither MOVED nor ASK: %v", err)
305 }
306
307 if addr != tt.expectedAddr {
308 t.Errorf("Address mismatch: got %q, want %q", addr, tt.expectedAddr)
309 }
310
311 // Test with wrapped error
312 wrappedErr := fmt.Errorf("wrapped: %w", err)
313 if movedErr, ok := IsMovedError(wrappedErr); ok {
314 addr = movedErr.Addr()
315 } else if askErr, ok := IsAskError(wrappedErr); ok {
316 addr = askErr.Addr()
317 } else {
318 t.Fatalf("Wrapped error is neither MOVED nor ASK: %v", wrappedErr)
319 }
320
321 if addr != tt.expectedAddr {
322 t.Errorf("Address mismatch in wrapped error: got %q, want %q", addr, tt.expectedAddr)
323 }

Callers

nothing calls this directly

Calls 5

parseTypedRedisErrorFunction · 0.85
IsMovedErrorFunction · 0.70
IsAskErrorFunction · 0.70
RunMethod · 0.45
AddrMethod · 0.45

Tested by

no test coverage detected