Skip to content

Commit

Permalink
host: use broadcast_address in ConnectAddress (apache#923)
Browse files Browse the repository at this point in the history
If the rpc_address is set to 0.0.0.0 then we should use
broadcast_address if it is set.
  • Loading branch information
Zariel authored Jun 25, 2017
1 parent f3cb873 commit da4a503
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
7 changes: 4 additions & 3 deletions host_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ func (h *HostInfo) ConnectAddress() net.IP {
// Use 'rpc_address' if provided and it's not 0.0.0.0
if h.rpcAddress != nil && !h.rpcAddress.IsUnspecified() {
return h.rpcAddress
}
// Peer should always be set if this from 'system.peer'
if h.peer != nil {
} else if h.broadcastAddress != nil && !h.broadcastAddress.IsUnspecified() {
return h.broadcastAddress
} else if h.peer != nil {
// Peer should always be set if this from 'system.peer'
return h.peer
}
}
Expand Down
31 changes: 31 additions & 0 deletions host_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,34 @@ func TestGetHostsWithFilter(t *testing.T) {
}
}
}

func TestHostInfo_ConnectAddress(t *testing.T) {
var localhost = net.IPv4(127, 0, 0, 1)
tests := []struct {
name string
connectAddr net.IP
rpcAddr net.IP
broadcastAddr net.IP
peer net.IP
}{
{name: "rpc_address", rpcAddr: localhost},
{name: "connect_address", connectAddr: localhost},
{name: "broadcast_address", broadcastAddr: localhost},
{name: "peer", peer: localhost},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
host := &HostInfo{
connectAddress: test.connectAddr,
rpcAddress: test.rpcAddr,
broadcastAddress: test.broadcastAddr,
peer: test.peer,
}

if addr := host.ConnectAddress(); !addr.Equal(localhost) {
t.Fatalf("expected ConnectAddress to be %s got %s", localhost, addr)
}
})
}
}

0 comments on commit da4a503

Please sign in to comment.