micro-broker-nats/nats_test.go

97 lines
2.3 KiB
Go
Raw Normal View History

package nats
import (
"fmt"
2018-01-05 22:18:00 +03:00
"testing"
"github.com/micro/go-micro/broker"
"github.com/nats-io/go-nats"
)
2018-01-05 22:18:00 +03:00
var addrTestCases = []struct {
name string
description string
addrs map[string]string // expected address : set address
}{
{
2018-01-06 18:19:15 +03:00
"brokerOpts",
2018-01-05 22:18:00 +03:00
"set broker addresses through a broker.Option in constructor",
map[string]string{
"nats://192.168.10.1:5222": "192.168.10.1:5222",
"nats://10.20.10.0:4222": "10.20.10.0:4222"},
},
{
2018-01-06 18:19:15 +03:00
"brokerInit",
2018-01-05 22:18:00 +03:00
"set broker addresses through a broker.Option in broker.Init()",
map[string]string{
"nats://192.168.10.1:5222": "192.168.10.1:5222",
"nats://10.20.10.0:4222": "10.20.10.0:4222"},
},
{
2018-01-06 18:19:15 +03:00
"natsOpts",
2018-01-05 22:18:00 +03:00
"set broker addresses through the nats.Option in constructor",
map[string]string{
"nats://192.168.10.1:5222": "192.168.10.1:5222",
"nats://10.20.10.0:4222": "10.20.10.0:4222"},
},
{
"default",
"check if default Address is set correctly",
map[string]string{
"nats://localhost:4222": ""},
},
}
// 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) {
2018-01-05 22:18:00 +03:00
for _, tc := range addrTestCases {
t.Run(fmt.Sprintf("%s: %s", tc.name, tc.description), func(t *testing.T) {
2018-01-05 22:18:00 +03:00
var br broker.Broker
var addrs []string
2018-01-05 22:18:00 +03:00
for _, addr := range tc.addrs {
addrs = append(addrs, addr)
}
2018-01-05 22:18:00 +03:00
switch tc.name {
2018-01-06 18:19:15 +03:00
case "brokerOpts":
2018-01-05 22:18:00 +03:00
// we know that there are just two addrs in the dict
br = NewBroker(broker.Addrs(addrs[0], addrs[1]))
br.Init()
2018-01-06 18:19:15 +03:00
case "brokerInit":
2018-01-05 22:18:00 +03:00
br = NewBroker()
// we know that there are just two addrs in the dict
br.Init(broker.Addrs(addrs[0], addrs[1]))
2018-01-06 18:19:15 +03:00
case "natsOpts":
2018-01-05 22:18:00 +03:00
nopts := nats.GetDefaultOptions()
nopts.Servers = addrs
br = NewBroker(Options(nopts))
2018-01-05 22:18:00 +03:00
br.Init()
case "default":
br = NewBroker()
br.Init()
}
2018-01-05 22:18:00 +03:00
natsBroker, ok := br.(*nbroker)
if !ok {
t.Fatal("Expected broker to be of types *nbroker")
}
// check if the same amount of addrs we set has actually been set
if len(natsBroker.addrs) != len(tc.addrs) {
t.Errorf("Expected Addr count = %d, Actual Addr count = %d",
len(natsBroker.addrs), len(tc.addrs))
}
2018-01-05 22:18:00 +03:00
for _, addr := range natsBroker.addrs {
_, ok := tc.addrs[addr]
if !ok {
t.Errorf("Expected '%s' has not been set", addr)
}
}
})
2018-01-05 22:18:00 +03:00
}
}