|
| 1 | +package redis |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync/atomic" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/redis/go-redis/v9/maintnotifications" |
| 9 | +) |
| 10 | + |
| 11 | +// TestClusterClientSMigratedCallback tests that ClusterClient sets up SMIGRATED callback on node clients |
| 12 | +func TestClusterClientSMigratedCallback(t *testing.T) { |
| 13 | + t.Run("CallbackSetupWithMaintNotifications", func(t *testing.T) { |
| 14 | + // Track if state reload was called |
| 15 | + var reloadCalled atomic.Bool |
| 16 | + |
| 17 | + // Create cluster options with maintnotifications enabled |
| 18 | + opt := &ClusterOptions{ |
| 19 | + Addrs: []string{"localhost:7000"}, // Dummy address |
| 20 | + MaintNotificationsConfig: &maintnotifications.Config{ |
| 21 | + Mode: maintnotifications.ModeEnabled, |
| 22 | + }, |
| 23 | + // Use custom NewClient to track when nodes are created |
| 24 | + NewClient: func(opt *Options) *Client { |
| 25 | + client := NewClient(opt) |
| 26 | + return client |
| 27 | + }, |
| 28 | + } |
| 29 | + |
| 30 | + // Create cluster client |
| 31 | + cluster := NewClusterClient(opt) |
| 32 | + defer cluster.Close() |
| 33 | + |
| 34 | + // Manually trigger node creation by calling GetOrCreate |
| 35 | + // This simulates what happens during normal cluster operations |
| 36 | + node, err := cluster.nodes.GetOrCreate("localhost:7000") |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("Failed to create node: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + // Get the maintnotifications manager from the node client |
| 42 | + manager := node.Client.GetMaintNotificationsManager() |
| 43 | + if manager == nil { |
| 44 | + t.Skip("MaintNotifications manager not initialized (expected if not connected to real Redis)") |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + // Temporarily replace the cluster state reload with our test version |
| 49 | + var receivedSlot int |
| 50 | + originalCallback := manager |
| 51 | + manager.SetClusterStateReloadCallback(func(ctx context.Context, slot int) { |
| 52 | + reloadCalled.Store(true) |
| 53 | + receivedSlot = slot |
| 54 | + }) |
| 55 | + |
| 56 | + // Trigger the callback (this is what SMIGRATED notification would do) |
| 57 | + ctx := context.Background() |
| 58 | + testSlot := 1234 |
| 59 | + manager.TriggerClusterStateReload(ctx, testSlot) |
| 60 | + |
| 61 | + // Verify callback was called |
| 62 | + if !reloadCalled.Load() { |
| 63 | + t.Error("Cluster state reload callback should have been called") |
| 64 | + } |
| 65 | + |
| 66 | + // Verify slot was passed correctly |
| 67 | + if receivedSlot != testSlot { |
| 68 | + t.Errorf("Expected slot %d, got %d", testSlot, receivedSlot) |
| 69 | + } |
| 70 | + _ = originalCallback |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("NoCallbackWithoutMaintNotifications", func(t *testing.T) { |
| 74 | + // Create cluster options WITHOUT maintnotifications |
| 75 | + opt := &ClusterOptions{ |
| 76 | + Addrs: []string{"localhost:7000"}, // Dummy address |
| 77 | + // MaintNotificationsConfig is nil |
| 78 | + } |
| 79 | + |
| 80 | + // Create cluster client |
| 81 | + cluster := NewClusterClient(opt) |
| 82 | + defer cluster.Close() |
| 83 | + |
| 84 | + // The OnNewNode callback should not be registered when MaintNotificationsConfig is nil |
| 85 | + // This test just verifies that the cluster client doesn't panic |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +// TestClusterClientSMigratedIntegration tests SMIGRATED notification handling in cluster context |
| 90 | +func TestClusterClientSMigratedIntegration(t *testing.T) { |
| 91 | + t.Run("SMigratedTriggersStateReload", func(t *testing.T) { |
| 92 | + // This test verifies the integration between SMIGRATED notification and cluster state reload |
| 93 | + // We verify that the callback is properly set up to call cluster.state.LazyReload() |
| 94 | + |
| 95 | + // Create cluster options with maintnotifications enabled |
| 96 | + opt := &ClusterOptions{ |
| 97 | + Addrs: []string{"localhost:7000"}, |
| 98 | + MaintNotificationsConfig: &maintnotifications.Config{ |
| 99 | + Mode: maintnotifications.ModeEnabled, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + // Create cluster client |
| 104 | + cluster := NewClusterClient(opt) |
| 105 | + defer cluster.Close() |
| 106 | + |
| 107 | + // Create a node |
| 108 | + node, err := cluster.nodes.GetOrCreate("localhost:7000") |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("Failed to create node: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + // Get the maintnotifications manager |
| 114 | + manager := node.Client.GetMaintNotificationsManager() |
| 115 | + if manager == nil { |
| 116 | + t.Skip("MaintNotifications manager not initialized (expected if not connected to real Redis)") |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + // Verify that the callback is set by checking it's not nil |
| 121 | + // We can't directly test LazyReload being called without a real cluster, |
| 122 | + // but we can verify the callback mechanism works |
| 123 | + var callbackWorks atomic.Bool |
| 124 | + var receivedSlot int |
| 125 | + manager.SetClusterStateReloadCallback(func(ctx context.Context, slot int) { |
| 126 | + callbackWorks.Store(true) |
| 127 | + receivedSlot = slot |
| 128 | + }) |
| 129 | + |
| 130 | + ctx := context.Background() |
| 131 | + testSlot := 5678 |
| 132 | + manager.TriggerClusterStateReload(ctx, testSlot) |
| 133 | + |
| 134 | + if !callbackWorks.Load() { |
| 135 | + t.Error("Callback mechanism should work") |
| 136 | + } |
| 137 | + |
| 138 | + if receivedSlot != testSlot { |
| 139 | + t.Errorf("Expected slot %d, got %d", testSlot, receivedSlot) |
| 140 | + } |
| 141 | + }) |
| 142 | +} |
| 143 | + |
| 144 | +// TestSMigratingAndSMigratedConstants verifies the SMIGRATING and SMIGRATED constants are exported |
| 145 | +func TestSMigratingAndSMigratedConstants(t *testing.T) { |
| 146 | + // This test verifies that the SMIGRATING and SMIGRATED constants are properly defined |
| 147 | + // and accessible from the maintnotifications package |
| 148 | + if maintnotifications.NotificationSMigrating != "SMIGRATING" { |
| 149 | + t.Errorf("Expected NotificationSMigrating to be 'SMIGRATING', got: %s", maintnotifications.NotificationSMigrating) |
| 150 | + } |
| 151 | + |
| 152 | + if maintnotifications.NotificationSMigrated != "SMIGRATED" { |
| 153 | + t.Errorf("Expected NotificationSMigrated to be 'SMIGRATED', got: %s", maintnotifications.NotificationSMigrated) |
| 154 | + } |
| 155 | +} |
| 156 | + |
0 commit comments