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

     1  package nats
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"log"
     9  
    10  	"github.com/nats-io/nats.go"
    11  	"go-micro.dev/v5/server"
    12  	"go-micro.dev/v5/transport"
    13  )
    14  
    15  var addrTestCases = []struct {
    16  	name        string
    17  	description string
    18  	addrs       map[string]string // expected address : set address
    19  }{
    20  	{
    21  		"transportOption",
    22  		"set broker addresses through a transport.Option",
    23  		map[string]string{
    24  			"nats://192.168.10.1:5222": "192.168.10.1:5222",
    25  			"nats://10.20.10.0:4222":   "10.20.10.0:4222"},
    26  	},
    27  	{
    28  		"natsOption",
    29  		"set broker addresses through the nats.Option",
    30  		map[string]string{
    31  			"nats://192.168.10.1:5222": "192.168.10.1:5222",
    32  			"nats://10.20.10.0:4222":   "10.20.10.0:4222"},
    33  	},
    34  	{
    35  		"default",
    36  		"check if default Address is set correctly",
    37  		map[string]string{
    38  			"nats://127.0.0.1:4222": ""},
    39  	},
    40  }
    41  
    42  // This test will check if options (here nats addresses) set through either
    43  // transport.Option or via nats.Option are successfully set.
    44  func TestInitAddrs(t *testing.T) {
    45  	for _, tc := range addrTestCases {
    46  		t.Run(tc.name, func(t *testing.T) {
    47  			var tr transport.Transport
    48  			var addrs []string
    49  
    50  			for _, addr := range tc.addrs {
    51  				addrs = append(addrs, addr)
    52  			}
    53  
    54  			switch tc.name {
    55  			case "transportOption":
    56  				// we know that there are just two addrs in the dict
    57  				tr = NewTransport(transport.Addrs(addrs[0], addrs[1]))
    58  			case "natsOption":
    59  				nopts := nats.GetDefaultOptions()
    60  				nopts.Servers = addrs
    61  				tr = NewTransport(Options(nopts))
    62  			case "default":
    63  				tr = NewTransport()
    64  			}
    65  
    66  			ntport, ok := tr.(*ntport)
    67  			if !ok {
    68  				t.Fatal("Expected broker to be of types *nbroker")
    69  			}
    70  			// check if the same amount of addrs we set has actually been set
    71  			if len(ntport.addrs) != len(tc.addrs) {
    72  				t.Errorf("Expected Addr count = %d, Actual Addr count = %d",
    73  					len(ntport.addrs), len(tc.addrs))
    74  			}
    75  
    76  			for _, addr := range ntport.addrs {
    77  				_, ok := tc.addrs[addr]
    78  				if !ok {
    79  					t.Errorf("Expected '%s' has not been set", addr)
    80  				}
    81  			}
    82  		})
    83  	}
    84  }
    85  
    86  var listenAddrTestCases = []struct {
    87  	name     string
    88  	address  string
    89  	mustPass bool
    90  }{
    91  	{"default address", server.DefaultAddress, true},
    92  	{"nats.NewInbox", nats.NewInbox(), true},
    93  	{"correct service name", "micro.test.myservice", true},
    94  	{"several space chars", "micro.test.my new service", false},
    95  	{"one space char", "micro.test.my oldservice", false},
    96  	{"empty", "", false},
    97  }
    98  
    99  func TestListenAddr(t *testing.T) {
   100  	natsURL := os.Getenv("NATS_URL")
   101  	if natsURL == "" {
   102  		log.Println("NATS_URL is undefined - skipping tests")
   103  		return
   104  	}
   105  
   106  	for _, tc := range listenAddrTestCases {
   107  		t.Run(tc.address, func(t *testing.T) {
   108  			nOpts := nats.GetDefaultOptions()
   109  			nOpts.Servers = []string{natsURL}
   110  			nTport := ntport{
   111  				nopts: nOpts,
   112  			}
   113  			trListener, err := nTport.Listen(tc.address)
   114  			if err != nil {
   115  				if tc.mustPass {
   116  					t.Fatalf("%s (%s) is not allowed", tc.name, tc.address)
   117  				}
   118  				// correctly failed
   119  				return
   120  			}
   121  			if trListener.Addr() != tc.address {
   122  				// special case - since an always string will be returned
   123  				if tc.name == "default address" {
   124  					if strings.Contains(trListener.Addr(), "_INBOX.") {
   125  						return
   126  					}
   127  				}
   128  				t.Errorf("expected address %s but got %s", tc.address, trListener.Addr())
   129  			}
   130  		})
   131  	}
   132  }