added test for address and updated addrs to return real conn address

This commit is contained in:
darren-west 2017-05-24 14:09:33 +01:00 committed by Vasiliy Tolstov
parent 260b112006
commit c67ac99441
2 changed files with 40 additions and 0 deletions

View File

@ -55,9 +55,13 @@ func (n *subscriber) Unsubscribe() error {
}
func (n *nbroker) Address() string {
if n.conn != nil && n.conn.IsConnected() {
return n.conn.ConnectedUrl()
}
if len(n.addrs) > 0 {
return n.addrs[0]
}
return ""
}

36
nats_test.go Normal file
View File

@ -0,0 +1,36 @@
package nats
import (
"testing"
"fmt"
"github.com/micro/go-micro/broker"
)
// TestInitAddrs tests issue #100. Ensures that if the addrs is set by an option in init it will be used.
func TestInitAddrs(t *testing.T) {
nb := NewBroker()
addr1, addr2 := "192.168.10.1:5222", "10.20.10.0:4222"
nb.Init(broker.Addrs(addr1, addr2))
if len(nb.Options().Addrs) != 2 {
t.Errorf("Expected Addr count = 2, Actual Addr count = %d", len(nb.Options().Addrs))
}
natsBroker, ok := nb.(*nbroker)
if !ok {
t.Fatal("Expected broker to be of types *nbroker")
}
addr1f := fmt.Sprintf("nats://%s", addr1)
addr2f := fmt.Sprintf("nats://%s", addr2)
if natsBroker.addrs[0] != addr1f && natsBroker.addrs[1] != addr2f {
expAddr, actAddr := fmt.Sprintf("%s,%s", addr1f, addr2f), fmt.Sprintf("%s,%s", natsBroker.addrs[0], natsBroker.addrs[1])
t.Errorf("Expected = '%s', Actual = '%s'", expAddr, actAddr)
}
}