From c67ac99441b397e03034d90e28101f1db7793b19 Mon Sep 17 00:00:00 2001 From: darren-west Date: Wed, 24 May 2017 14:09:33 +0100 Subject: [PATCH] added test for address and updated addrs to return real conn address --- nats.go | 4 ++++ nats_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 nats_test.go 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) + } + +}