1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-19 11:43:14 +03:00
Files
go-redis/internal/pushprocessor/pushprocessor_test.go
Nedyalko Dyakov 5972b4c23f refactor: move all push notification logic to root package and remove adapters
Consolidate all push notification handling logic in the root package to eliminate
adapters and simplify the architecture. This provides direct access to concrete
types without any intermediate layers or type conversions.

Key Changes:

1. Moved Core Types to Root Package:
   - Moved Registry, Processor, VoidProcessor to push_notifications.go
   - Moved all push notification constants to root package
   - Removed internal/pushnotif package dependencies
   - Direct implementation without internal abstractions

2. Eliminated All Adapters:
   - Removed handlerAdapter that bridged internal and public interfaces
   - Removed voidProcessorAdapter for void processor functionality
   - Removed convertInternalToPublicContext conversion functions
   - Direct usage of concrete types throughout

3. Simplified Architecture:
   - PushNotificationHandlerContext directly implemented in root package
   - PushNotificationHandler directly implemented in root package
   - Registry, Processor, VoidProcessor directly in root package
   - No intermediate layers or type conversions needed

4. Direct Type Usage:
   - GetClusterClient() returns *ClusterClient directly
   - GetSentinelClient() returns *SentinelClient directly
   - GetRegularClient() returns *Client directly
   - GetPubSub() returns *PubSub directly
   - No interface casting or type assertions required

5. Updated All Integration Points:
   - Updated redis.go to use direct types
   - Updated pubsub.go to use direct types
   - Updated sentinel.go to use direct types
   - Removed all internal/pushnotif imports
   - Simplified context creation and usage

6. Core Implementation in Root Package:
   ```go
   // Direct implementation - no adapters needed
   type Registry struct {
       handlers  map[string]PushNotificationHandler
       protected map[string]bool
   }

   type Processor struct {
       registry *Registry
   }

   type VoidProcessor struct{}
   ```

7. Handler Context with Concrete Types:
   ```go
   type PushNotificationHandlerContext interface {
       GetClusterClient() *ClusterClient    // Direct concrete type
       GetSentinelClient() *SentinelClient  // Direct concrete type
       GetRegularClient() *Client           // Direct concrete type
       GetPubSub() *PubSub                  // Direct concrete type
   }
   ```

8. Comprehensive Test Suite:
   - Added push_notifications_test.go with full test coverage
   - Tests for Registry, Processor, VoidProcessor
   - Tests for HandlerContext with concrete type access
   - Tests for all push notification constants
   - Validates all functionality works correctly

9. Benefits:
   - Eliminated complex adapter pattern
   - Removed unnecessary type conversions
   - Simplified codebase with direct type usage
   - Better performance without adapter overhead
   - Cleaner architecture with single source of truth
   - Enhanced developer experience with direct access

10. Architecture Simplification:
    Before: Client -> Adapter -> Internal -> Adapter -> Handler
    After:  Client -> Handler (direct)

    No more:
    - handlerAdapter bridging interfaces
    - voidProcessorAdapter for void functionality
    - convertInternalToPublicContext conversions
    - Complex type mapping between layers

This refactoring provides a much cleaner, simpler architecture where all
push notification logic lives in the root package with direct access to
concrete Redis client types, eliminating unnecessary complexity while
maintaining full functionality and type safety.
2025-07-04 21:13:47 +03:00

776 lines
24 KiB
Go

package pushprocessor
import (
"context"
"io"
"strings"
"testing"
"github.com/redis/go-redis/v9/internal"
"github.com/redis/go-redis/v9/internal/proto"
)
// TestHandler implements Handler interface for testing
type TestHandler struct {
name string
handled [][]interface{}
returnValue bool
}
func NewTestHandler(name string, returnValue bool) *TestHandler {
return &TestHandler{
name: name,
handled: make([][]interface{}, 0),
returnValue: returnValue,
}
}
func (h *TestHandler) HandlePushNotification(ctx context.Context, handlerCtx HandlerContext, notification []interface{}) bool {
h.handled = append(h.handled, notification)
// Store the handler context for testing if needed
_ = handlerCtx
return h.returnValue
}
func (h *TestHandler) GetHandledNotifications() [][]interface{} {
return h.handled
}
func (h *TestHandler) Reset() {
h.handled = make([][]interface{}, 0)
}
// TestReaderInterface defines the interface needed for testing
type TestReaderInterface interface {
PeekReplyType() (byte, error)
PeekPushNotificationName() (string, error)
ReadReply() (interface{}, error)
}
// MockReader implements TestReaderInterface for testing
type MockReader struct {
peekReplies []peekReply
peekIndex int
readReplies []interface{}
readErrors []error
readIndex int
}
type peekReply struct {
replyType byte
err error
}
func NewMockReader() *MockReader {
return &MockReader{
peekReplies: make([]peekReply, 0),
readReplies: make([]interface{}, 0),
readErrors: make([]error, 0),
readIndex: 0,
peekIndex: 0,
}
}
func (m *MockReader) AddPeekReplyType(replyType byte, err error) {
m.peekReplies = append(m.peekReplies, peekReply{replyType: replyType, err: err})
}
func (m *MockReader) AddReadReply(reply interface{}, err error) {
m.readReplies = append(m.readReplies, reply)
m.readErrors = append(m.readErrors, err)
}
func (m *MockReader) PeekReplyType() (byte, error) {
if m.peekIndex >= len(m.peekReplies) {
return 0, io.EOF
}
peek := m.peekReplies[m.peekIndex]
m.peekIndex++
return peek.replyType, peek.err
}
func (m *MockReader) ReadReply() (interface{}, error) {
if m.readIndex >= len(m.readReplies) {
return nil, io.EOF
}
reply := m.readReplies[m.readIndex]
err := m.readErrors[m.readIndex]
m.readIndex++
return reply, err
}
func (m *MockReader) PeekPushNotificationName() (string, error) {
// return the notification name from the next read reply
if m.readIndex >= len(m.readReplies) {
return "", io.EOF
}
reply := m.readReplies[m.readIndex]
if reply == nil {
return "", nil
}
notification, ok := reply.([]interface{})
if !ok {
return "", nil
}
if len(notification) == 0 {
return "", nil
}
name, ok := notification[0].(string)
if !ok {
return "", nil
}
return name, nil
}
func (m *MockReader) Reset() {
m.readIndex = 0
m.peekIndex = 0
}
// testProcessPendingNotifications is a test version that accepts our mock reader
func testProcessPendingNotifications(processor *Processor, ctx context.Context, reader TestReaderInterface) error {
if reader == nil {
return nil
}
// Create a test handler context
handlerCtx := NewHandlerContext(nil, nil, nil, nil, false)
for {
// Check if there are push notifications available
replyType, err := reader.PeekReplyType()
if err != nil {
// No more data or error - this is normal
break
}
// Only process push notifications
if replyType != proto.RespPush {
break
}
notificationName, err := reader.PeekPushNotificationName()
if err != nil {
// Error reading - continue to next iteration
break
}
// Skip notifications that should be handled by other systems
if shouldSkipNotification(notificationName) {
break
}
// Read the push notification
reply, err := reader.ReadReply()
if err != nil {
// Error reading - continue to next iteration
internal.Logger.Printf(ctx, "push: error reading push notification: %v", err)
continue
}
// Convert to slice of interfaces
notification, ok := reply.([]interface{})
if !ok {
continue
}
// Handle the notification directly
if len(notification) > 0 {
// Extract the notification type (first element)
if notificationType, ok := notification[0].(string); ok {
// Get the handler for this notification type
if handler := processor.registry.GetHandler(notificationType); handler != nil {
// Handle the notification with context
handler.HandlePushNotification(ctx, handlerCtx, notification)
}
}
}
}
return nil
}
// TestRegistry tests the Registry implementation
func TestRegistry(t *testing.T) {
t.Run("NewRegistry", func(t *testing.T) {
registry := NewRegistry()
if registry == nil {
t.Error("NewRegistry should return a non-nil registry")
}
if registry.handlers == nil {
t.Error("Registry handlers map should be initialized")
}
if registry.protected == nil {
t.Error("Registry protected map should be initialized")
}
})
t.Run("RegisterHandler", func(t *testing.T) {
registry := NewRegistry()
handler := NewTestHandler("test", true)
// Test successful registration
err := registry.RegisterHandler("MOVING", handler, false)
if err != nil {
t.Errorf("RegisterHandler should succeed, got error: %v", err)
}
// Test duplicate registration
err = registry.RegisterHandler("MOVING", handler, false)
if err == nil {
t.Error("RegisterHandler should return error for duplicate registration")
}
if !strings.Contains(err.Error(), "handler already registered") {
t.Errorf("Expected error about duplicate registration, got: %v", err)
}
// Test protected registration
err = registry.RegisterHandler("MIGRATING", handler, true)
if err != nil {
t.Errorf("RegisterHandler with protected=true should succeed, got error: %v", err)
}
})
t.Run("GetHandler", func(t *testing.T) {
registry := NewRegistry()
handler := NewTestHandler("test", true)
// Test getting non-existent handler
result := registry.GetHandler("NONEXISTENT")
if result != nil {
t.Error("GetHandler should return nil for non-existent handler")
}
// Test getting existing handler
err := registry.RegisterHandler("MOVING", handler, false)
if err != nil {
t.Fatalf("Failed to register handler: %v", err)
}
result = registry.GetHandler("MOVING")
if result != handler {
t.Error("GetHandler should return the registered handler")
}
})
t.Run("UnregisterHandler", func(t *testing.T) {
registry := NewRegistry()
handler := NewTestHandler("test", true)
// Test unregistering non-existent handler
err := registry.UnregisterHandler("NONEXISTENT")
if err == nil {
t.Error("UnregisterHandler should return error for non-existent handler")
}
if !strings.Contains(err.Error(), "no handler registered") {
t.Errorf("Expected error about no handler registered, got: %v", err)
}
// Test unregistering regular handler
err = registry.RegisterHandler("MOVING", handler, false)
if err != nil {
t.Fatalf("Failed to register handler: %v", err)
}
err = registry.UnregisterHandler("MOVING")
if err != nil {
t.Errorf("UnregisterHandler should succeed for regular handler, got error: %v", err)
}
// Verify handler is removed
result := registry.GetHandler("MOVING")
if result != nil {
t.Error("Handler should be removed after unregistration")
}
// Test unregistering protected handler
err = registry.RegisterHandler("MIGRATING", handler, true)
if err != nil {
t.Fatalf("Failed to register protected handler: %v", err)
}
err = registry.UnregisterHandler("MIGRATING")
if err == nil {
t.Error("UnregisterHandler should return error for protected handler")
}
if !strings.Contains(err.Error(), "cannot unregister protected handler") {
t.Errorf("Expected error about protected handler, got: %v", err)
}
// Verify protected handler is still there
result = registry.GetHandler("MIGRATING")
if result != handler {
t.Error("Protected handler should still be registered after failed unregistration")
}
})
t.Run("GetRegisteredPushNotificationNames", func(t *testing.T) {
registry := NewRegistry()
handler1 := NewTestHandler("test1", true)
handler2 := NewTestHandler("test2", true)
// Test empty registry
names := registry.GetRegisteredPushNotificationNames()
if len(names) != 0 {
t.Errorf("Empty registry should return empty slice, got: %v", names)
}
// Test with registered handlers
err := registry.RegisterHandler("MOVING", handler1, false)
if err != nil {
t.Fatalf("Failed to register handler1: %v", err)
}
err = registry.RegisterHandler("MIGRATING", handler2, true)
if err != nil {
t.Fatalf("Failed to register handler2: %v", err)
}
names = registry.GetRegisteredPushNotificationNames()
if len(names) != 2 {
t.Errorf("Expected 2 registered names, got: %d", len(names))
}
// Check that both names are present (order doesn't matter)
nameMap := make(map[string]bool)
for _, name := range names {
nameMap[name] = true
}
if !nameMap["MOVING"] {
t.Error("MOVING should be in registered names")
}
if !nameMap["MIGRATING"] {
t.Error("MIGRATING should be in registered names")
}
})
}
// TestProcessor tests the Processor implementation
func TestProcessor(t *testing.T) {
t.Run("NewProcessor", func(t *testing.T) {
processor := NewProcessor()
if processor == nil {
t.Error("NewProcessor should return a non-nil processor")
}
if processor.registry == nil {
t.Error("Processor should have a non-nil registry")
}
})
t.Run("GetHandler", func(t *testing.T) {
processor := NewProcessor()
handler := NewTestHandler("test", true)
// Test getting non-existent handler
result := processor.GetHandler("NONEXISTENT")
if result != nil {
t.Error("GetHandler should return nil for non-existent handler")
}
// Test getting existing handler
err := processor.RegisterHandler("MOVING", handler, false)
if err != nil {
t.Fatalf("Failed to register handler: %v", err)
}
result = processor.GetHandler("MOVING")
if result != handler {
t.Error("GetHandler should return the registered handler")
}
})
t.Run("RegisterHandler", func(t *testing.T) {
processor := NewProcessor()
handler := NewTestHandler("test", true)
// Test successful registration
err := processor.RegisterHandler("MOVING", handler, false)
if err != nil {
t.Errorf("RegisterHandler should succeed, got error: %v", err)
}
// Test duplicate registration
err = processor.RegisterHandler("MOVING", handler, false)
if err == nil {
t.Error("RegisterHandler should return error for duplicate registration")
}
})
t.Run("UnregisterHandler", func(t *testing.T) {
processor := NewProcessor()
handler := NewTestHandler("test", true)
// Test unregistering non-existent handler
err := processor.UnregisterHandler("NONEXISTENT")
if err == nil {
t.Error("UnregisterHandler should return error for non-existent handler")
}
// Test successful unregistration
err = processor.RegisterHandler("MOVING", handler, false)
if err != nil {
t.Fatalf("Failed to register handler: %v", err)
}
err = processor.UnregisterHandler("MOVING")
if err != nil {
t.Errorf("UnregisterHandler should succeed, got error: %v", err)
}
})
t.Run("ProcessPendingNotifications", func(t *testing.T) {
processor := NewProcessor()
handler := NewTestHandler("test", true)
ctx := context.Background()
// Test with nil reader
handlerCtx := NewHandlerContext(nil, nil, nil, nil, false)
err := processor.ProcessPendingNotifications(ctx, handlerCtx, nil)
if err != nil {
t.Errorf("ProcessPendingNotifications with nil reader should not error, got: %v", err)
}
// Test with empty reader (no buffered data)
reader := proto.NewReader(strings.NewReader(""))
err = processor.ProcessPendingNotifications(ctx, handlerCtx, reader)
if err != nil {
t.Errorf("ProcessPendingNotifications with empty reader should not error, got: %v", err)
}
// Register a handler for testing
err = processor.RegisterHandler("MOVING", handler, false)
if err != nil {
t.Fatalf("Failed to register handler: %v", err)
}
// Test with mock reader - peek error (no push notifications available)
mockReader := NewMockReader()
mockReader.AddPeekReplyType(proto.RespString, io.EOF) // EOF means no more data
err = testProcessPendingNotifications(processor, ctx, mockReader)
if err != nil {
t.Errorf("ProcessPendingNotifications should handle peek EOF gracefully, got: %v", err)
}
// Test with mock reader - non-push reply type
mockReader = NewMockReader()
mockReader.AddPeekReplyType(proto.RespString, nil) // Not RespPush
err = testProcessPendingNotifications(processor, ctx, mockReader)
if err != nil {
t.Errorf("ProcessPendingNotifications should handle non-push reply types gracefully, got: %v", err)
}
// Test with mock reader - push notification with ReadReply error
mockReader = NewMockReader()
mockReader.AddPeekReplyType(proto.RespPush, nil)
mockReader.AddReadReply(nil, io.ErrUnexpectedEOF) // ReadReply fails
mockReader.AddPeekReplyType(proto.RespString, io.EOF) // No more push notifications
err = testProcessPendingNotifications(processor, ctx, mockReader)
if err != nil {
t.Errorf("ProcessPendingNotifications should handle ReadReply errors gracefully, got: %v", err)
}
// Test with mock reader - push notification with invalid reply type
mockReader = NewMockReader()
mockReader.AddPeekReplyType(proto.RespPush, nil)
mockReader.AddReadReply("not-a-slice", nil) // Invalid reply type
mockReader.AddPeekReplyType(proto.RespString, io.EOF) // No more push notifications
err = testProcessPendingNotifications(processor, ctx, mockReader)
if err != nil {
t.Errorf("ProcessPendingNotifications should handle invalid reply types gracefully, got: %v", err)
}
// Test with mock reader - valid push notification with handler
mockReader = NewMockReader()
mockReader.AddPeekReplyType(proto.RespPush, nil)
notification := []interface{}{"MOVING", "slot", "12345"}
mockReader.AddReadReply(notification, nil)
mockReader.AddPeekReplyType(proto.RespString, io.EOF) // No more push notifications
handler.Reset()
err = testProcessPendingNotifications(processor, ctx, mockReader)
if err != nil {
t.Errorf("ProcessPendingNotifications should handle valid notifications, got: %v", err)
}
// Check that handler was called
handled := handler.GetHandledNotifications()
if len(handled) != 1 {
t.Errorf("Expected 1 handled notification, got: %d", len(handled))
} else if len(handled[0]) != 3 || handled[0][0] != "MOVING" {
t.Errorf("Expected MOVING notification, got: %v", handled[0])
}
// Test with mock reader - valid push notification without handler
mockReader = NewMockReader()
mockReader.AddPeekReplyType(proto.RespPush, nil)
notification = []interface{}{"UNKNOWN", "data"}
mockReader.AddReadReply(notification, nil)
mockReader.AddPeekReplyType(proto.RespString, io.EOF) // No more push notifications
err = testProcessPendingNotifications(processor, ctx, mockReader)
if err != nil {
t.Errorf("ProcessPendingNotifications should handle notifications without handlers, got: %v", err)
}
// Test with mock reader - empty notification
mockReader = NewMockReader()
mockReader.AddPeekReplyType(proto.RespPush, nil)
emptyNotification := []interface{}{}
mockReader.AddReadReply(emptyNotification, nil)
mockReader.AddPeekReplyType(proto.RespString, io.EOF) // No more push notifications
err = testProcessPendingNotifications(processor, ctx, mockReader)
if err != nil {
t.Errorf("ProcessPendingNotifications should handle empty notifications, got: %v", err)
}
// Test with mock reader - notification with non-string type
mockReader = NewMockReader()
mockReader.AddPeekReplyType(proto.RespPush, nil)
invalidTypeNotification := []interface{}{123, "data"} // First element is not string
mockReader.AddReadReply(invalidTypeNotification, nil)
mockReader.AddPeekReplyType(proto.RespString, io.EOF) // No more push notifications
err = testProcessPendingNotifications(processor, ctx, mockReader)
if err != nil {
t.Errorf("ProcessPendingNotifications should handle invalid notification types, got: %v", err)
}
// Test the actual ProcessPendingNotifications method with real proto.Reader
// Test with nil reader
err = processor.ProcessPendingNotifications(ctx, handlerCtx, nil)
if err != nil {
t.Errorf("ProcessPendingNotifications with nil reader should not error, got: %v", err)
}
// Test with empty reader (no buffered data)
protoReader := proto.NewReader(strings.NewReader(""))
err = processor.ProcessPendingNotifications(ctx, handlerCtx, protoReader)
if err != nil {
t.Errorf("ProcessPendingNotifications with empty reader should not error, got: %v", err)
}
// Test with reader that has some data but not push notifications
protoReader = proto.NewReader(strings.NewReader("+OK\r\n"))
err = processor.ProcessPendingNotifications(ctx, handlerCtx, protoReader)
if err != nil {
t.Errorf("ProcessPendingNotifications with non-push data should not error, got: %v", err)
}
})
}
// TestVoidProcessor tests the VoidProcessor implementation
func TestVoidProcessor(t *testing.T) {
t.Run("NewVoidProcessor", func(t *testing.T) {
processor := NewVoidProcessor()
if processor == nil {
t.Error("NewVoidProcessor should return a non-nil processor")
}
})
t.Run("GetHandler", func(t *testing.T) {
processor := NewVoidProcessor()
// VoidProcessor should always return nil for any handler name
result := processor.GetHandler("MOVING")
if result != nil {
t.Error("VoidProcessor GetHandler should always return nil")
}
result = processor.GetHandler("MIGRATING")
if result != nil {
t.Error("VoidProcessor GetHandler should always return nil")
}
result = processor.GetHandler("")
if result != nil {
t.Error("VoidProcessor GetHandler should always return nil for empty string")
}
})
t.Run("RegisterHandler", func(t *testing.T) {
processor := NewVoidProcessor()
handler := NewTestHandler("test", true)
// VoidProcessor should always return error for registration
err := processor.RegisterHandler("MOVING", handler, false)
if err == nil {
t.Error("VoidProcessor RegisterHandler should always return error")
}
if !strings.Contains(err.Error(), "cannot register push notification handler") {
t.Errorf("Expected error about cannot register, got: %v", err)
}
if !strings.Contains(err.Error(), "push notifications are disabled") {
t.Errorf("Expected error about disabled push notifications, got: %v", err)
}
// Test with protected flag
err = processor.RegisterHandler("MIGRATING", handler, true)
if err == nil {
t.Error("VoidProcessor RegisterHandler should always return error even with protected=true")
}
// Test with empty handler name
err = processor.RegisterHandler("", handler, false)
if err == nil {
t.Error("VoidProcessor RegisterHandler should always return error even with empty name")
}
})
t.Run("UnregisterHandler", func(t *testing.T) {
processor := NewVoidProcessor()
// VoidProcessor should always return error for unregistration
err := processor.UnregisterHandler("MOVING")
if err == nil {
t.Error("VoidProcessor UnregisterHandler should always return error")
}
if !strings.Contains(err.Error(), "cannot unregister push notification handler") {
t.Errorf("Expected error about cannot unregister, got: %v", err)
}
if !strings.Contains(err.Error(), "push notifications are disabled") {
t.Errorf("Expected error about disabled push notifications, got: %v", err)
}
// Test with empty handler name
err = processor.UnregisterHandler("")
if err == nil {
t.Error("VoidProcessor UnregisterHandler should always return error even with empty name")
}
})
t.Run("ProcessPendingNotifications", func(t *testing.T) {
processor := NewVoidProcessor()
ctx := context.Background()
handlerCtx := NewHandlerContext(nil, nil, nil, nil, false)
// VoidProcessor should always succeed and do nothing
err := processor.ProcessPendingNotifications(ctx, handlerCtx, nil)
if err != nil {
t.Errorf("VoidProcessor ProcessPendingNotifications should never error, got: %v", err)
}
// Test with various readers
reader := proto.NewReader(strings.NewReader(""))
err = processor.ProcessPendingNotifications(ctx, handlerCtx, reader)
if err != nil {
t.Errorf("VoidProcessor ProcessPendingNotifications should never error, got: %v", err)
}
reader = proto.NewReader(strings.NewReader("some data"))
err = processor.ProcessPendingNotifications(ctx, handlerCtx, reader)
if err != nil {
t.Errorf("VoidProcessor ProcessPendingNotifications should never error, got: %v", err)
}
})
}
// TestShouldSkipNotification tests the shouldSkipNotification function
func TestShouldSkipNotification(t *testing.T) {
t.Run("PubSubMessages", func(t *testing.T) {
pubSubMessages := []string{
"message", // Regular pub/sub message
"pmessage", // Pattern pub/sub message
"subscribe", // Subscription confirmation
"unsubscribe", // Unsubscription confirmation
"psubscribe", // Pattern subscription confirmation
"punsubscribe", // Pattern unsubscription confirmation
"smessage", // Sharded pub/sub message (Redis 7.0+)
}
for _, msgType := range pubSubMessages {
if !shouldSkipNotification(msgType) {
t.Errorf("shouldSkipNotification(%q) should return true", msgType)
}
}
})
t.Run("NonPubSubMessages", func(t *testing.T) {
nonPubSubMessages := []string{
"MOVING", // Cluster slot migration
"MIGRATING", // Cluster slot migration
"MIGRATED", // Cluster slot migration
"FAILING_OVER", // Cluster failover
"FAILED_OVER", // Cluster failover
"unknown", // Unknown message type
"", // Empty string
"MESSAGE", // Case sensitive - should not match
"PMESSAGE", // Case sensitive - should not match
}
for _, msgType := range nonPubSubMessages {
if shouldSkipNotification(msgType) {
t.Errorf("shouldSkipNotification(%q) should return false", msgType)
}
}
})
}
// TestPubSubFiltering tests that pub/sub messages are filtered out during processing
func TestPubSubFiltering(t *testing.T) {
t.Run("PubSubMessagesIgnored", func(t *testing.T) {
processor := NewProcessor()
handler := NewTestHandler("test", true)
ctx := context.Background()
// Register a handler for a non-pub/sub notification
err := processor.RegisterHandler("MOVING", handler, false)
if err != nil {
t.Fatalf("Failed to register handler: %v", err)
}
// Test with mock reader - pub/sub message should be ignored
mockReader := NewMockReader()
mockReader.AddPeekReplyType(proto.RespPush, nil)
pubSubNotification := []interface{}{"message", "channel", "data"}
mockReader.AddReadReply(pubSubNotification, nil)
mockReader.AddPeekReplyType(proto.RespString, io.EOF) // No more push notifications
handler.Reset()
err = testProcessPendingNotifications(processor, ctx, mockReader)
if err != nil {
t.Errorf("ProcessPendingNotifications should handle pub/sub messages gracefully, got: %v", err)
}
// Check that handler was NOT called for pub/sub message
handled := handler.GetHandledNotifications()
if len(handled) != 0 {
t.Errorf("Expected 0 handled notifications for pub/sub message, got: %d", len(handled))
}
})
t.Run("NonPubSubMessagesProcessed", func(t *testing.T) {
processor := NewProcessor()
handler := NewTestHandler("test", true)
ctx := context.Background()
// Register a handler for a non-pub/sub notification
err := processor.RegisterHandler("MOVING", handler, false)
if err != nil {
t.Fatalf("Failed to register handler: %v", err)
}
// Test with mock reader - non-pub/sub message should be processed
mockReader := NewMockReader()
mockReader.AddPeekReplyType(proto.RespPush, nil)
clusterNotification := []interface{}{"MOVING", "slot", "12345"}
mockReader.AddReadReply(clusterNotification, nil)
mockReader.AddPeekReplyType(proto.RespString, io.EOF) // No more push notifications
handler.Reset()
err = testProcessPendingNotifications(processor, ctx, mockReader)
if err != nil {
t.Errorf("ProcessPendingNotifications should handle cluster notifications, got: %v", err)
}
// Check that handler WAS called for cluster notification
handled := handler.GetHandledNotifications()
if len(handled) != 1 {
t.Errorf("Expected 1 handled notification for cluster message, got: %d", len(handled))
} else if len(handled[0]) != 3 || handled[0][0] != "MOVING" {
t.Errorf("Expected MOVING notification, got: %v", handled[0])
}
})
}