diff --git a/nats.go b/nats.go index 093be7e..414deb4 100644 --- a/nats.go +++ b/nats.go @@ -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 "" } diff --git a/nats_test.go b/nats_test.go new file mode 100644 index 0000000..1c928bc --- /dev/null +++ b/nats_test.go @@ -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) + } + +}