go-micro.dev/v5@v5.12.0/broker/nats/nats_test.go (about)

     1  package nats
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	natsp "github.com/nats-io/nats.go"
     8  	"go-micro.dev/v5/broker"
     9  )
    10  
    11  var addrTestCases = []struct {
    12  	name        string
    13  	description string
    14  	addrs       map[string]string // expected address : set address
    15  }{
    16  	{
    17  		"brokerOpts",
    18  		"set broker addresses through a broker.Option in constructor",
    19  		map[string]string{
    20  			"nats://192.168.10.1:5222": "192.168.10.1:5222",
    21  			"nats://10.20.10.0:4222":   "10.20.10.0:4222"},
    22  	},
    23  	{
    24  		"brokerInit",
    25  		"set broker addresses through a broker.Option in broker.Init()",
    26  		map[string]string{
    27  			"nats://192.168.10.1:5222": "192.168.10.1:5222",
    28  			"nats://10.20.10.0:4222":   "10.20.10.0:4222"},
    29  	},
    30  	{
    31  		"natsOpts",
    32  		"set broker addresses through the nats.Option in constructor",
    33  		map[string]string{
    34  			"nats://192.168.10.1:5222": "192.168.10.1:5222",
    35  			"nats://10.20.10.0:4222":   "10.20.10.0:4222"},
    36  	},
    37  	{
    38  		"default",
    39  		"check if default Address is set correctly",
    40  		map[string]string{
    41  			"nats://127.0.0.1:4222": "",
    42  		},
    43  	},
    44  }
    45  
    46  // TestInitAddrs tests issue #100. Ensures that if the addrs is set by an option in init it will be used.
    47  func TestInitAddrs(t *testing.T) {
    48  	for _, tc := range addrTestCases {
    49  		t.Run(fmt.Sprintf("%s: %s", tc.name, tc.description), func(t *testing.T) {
    50  			var br broker.Broker
    51  			var addrs []string
    52  
    53  			for _, addr := range tc.addrs {
    54  				addrs = append(addrs, addr)
    55  			}
    56  
    57  			switch tc.name {
    58  			case "brokerOpts":
    59  				// we know that there are just two addrs in the dict
    60  				br = NewNatsBroker(broker.Addrs(addrs[0], addrs[1]))
    61  				br.Init()
    62  			case "brokerInit":
    63  				br = NewNatsBroker()
    64  				// we know that there are just two addrs in the dict
    65  				br.Init(broker.Addrs(addrs[0], addrs[1]))
    66  			case "natsOpts":
    67  				nopts := natsp.GetDefaultOptions()
    68  				nopts.Servers = addrs
    69  				br = NewNatsBroker(Options(nopts))
    70  				br.Init()
    71  			case "default":
    72  				br = NewNatsBroker()
    73  				br.Init()
    74  			}
    75  
    76  			natsBroker, ok := br.(*natsBroker)
    77  			if !ok {
    78  				t.Fatal("Expected broker to be of types *natsBroker")
    79  			}
    80  			// check if the same amount of addrs we set has actually been set, default
    81  			// have only 1 address nats://127.0.0.1:4222 (current nats code) or
    82  			// nats://localhost:4222 (older code version)
    83  			if len(natsBroker.addrs) != len(tc.addrs) && tc.name != "default" {
    84  				t.Errorf("Expected Addr count = %d, Actual Addr count = %d",
    85  					len(natsBroker.addrs), len(tc.addrs))
    86  			}
    87  
    88  			for _, addr := range natsBroker.addrs {
    89  				_, ok := tc.addrs[addr]
    90  				if !ok {
    91  					t.Errorf("Expected '%s' has not been set", addr)
    92  				}
    93  			}
    94  		})
    95  	}
    96  }