TestNodeAddress tests that NodeAddress is correctly preserved when cluster nodes are created from ClusterSlots responses.
(t *testing.T)
| 186 | // TestNodeAddress tests that NodeAddress is correctly preserved |
| 187 | // when cluster nodes are created from ClusterSlots responses. |
| 188 | func TestNodeAddress(t *testing.T) { |
| 189 | t.Run("preserves node address when loopback is replaced", func(t *testing.T) { |
| 190 | opt := &ClusterOptions{} |
| 191 | opt.init() |
| 192 | nodes := newClusterNodes(opt) |
| 193 | defer nodes.Close() |
| 194 | |
| 195 | // Create cluster state with loopback addresses |
| 196 | // Origin is non-loopback, so loopback addresses will be replaced |
| 197 | slots := []ClusterSlot{{ |
| 198 | Start: 0, |
| 199 | End: 5460, |
| 200 | Nodes: []ClusterNode{{Addr: "127.0.0.1:7001"}}, |
| 201 | }, { |
| 202 | Start: 5461, |
| 203 | End: 10922, |
| 204 | Nodes: []ClusterNode{{Addr: "127.0.0.1:7002"}}, |
| 205 | }} |
| 206 | |
| 207 | state, err := newClusterState(nodes, slots, "10.10.10.10:1234") |
| 208 | if err != nil { |
| 209 | t.Fatalf("newClusterState failed: %v", err) |
| 210 | } |
| 211 | |
| 212 | // Verify Addr is transformed (loopback replaced with origin host) |
| 213 | if got := state.slots[0].nodes[0].Client.Options().Addr; got != "10.10.10.10:7001" { |
| 214 | t.Errorf("Addr = %q, want %q", got, "10.10.10.10:7001") |
| 215 | } |
| 216 | if got := state.slots[1].nodes[0].Client.Options().Addr; got != "10.10.10.10:7002" { |
| 217 | t.Errorf("Addr = %q, want %q", got, "10.10.10.10:7002") |
| 218 | } |
| 219 | |
| 220 | // Verify NodeAddress is preserved (original from ClusterSlots) |
| 221 | if got := state.slots[0].nodes[0].Client.NodeAddress(); got != "127.0.0.1:7001" { |
| 222 | t.Errorf("NodeAddress = %q, want %q", got, "127.0.0.1:7001") |
| 223 | } |
| 224 | if got := state.slots[1].nodes[0].Client.NodeAddress(); got != "127.0.0.1:7002" { |
| 225 | t.Errorf("NodeAddress = %q, want %q", got, "127.0.0.1:7002") |
| 226 | } |
| 227 | }) |
| 228 | |
| 229 | t.Run("preserves FQDN node addresses", func(t *testing.T) { |
| 230 | opt := &ClusterOptions{} |
| 231 | opt.init() |
| 232 | nodes := newClusterNodes(opt) |
| 233 | defer nodes.Close() |
| 234 | |
| 235 | slots := []ClusterSlot{{ |
| 236 | Start: 0, |
| 237 | End: 5460, |
| 238 | Nodes: []ClusterNode{{Addr: "redis-master.example.com:6379"}}, |
| 239 | }, { |
| 240 | Start: 5461, |
| 241 | End: 10922, |
| 242 | Nodes: []ClusterNode{{Addr: "redis-replica.example.com:6379"}}, |
| 243 | }} |
| 244 | |
| 245 | state, err := newClusterState(nodes, slots, "10.10.10.10:1234") |
nothing calls this directly
no test coverage detected