go-micro.dev/v5@v5.12.0/util/net/net_test.go (about)

     1  package net
     2  
     3  import (
     4  	"net"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func TestListen(t *testing.T) {
    10  	fn := func(addr string) (net.Listener, error) {
    11  		return net.Listen("tcp", addr)
    12  	}
    13  
    14  	// try to create a number of listeners
    15  	for i := 0; i < 10; i++ {
    16  		l, err := Listen("localhost:10000-11000", fn)
    17  		if err != nil {
    18  			t.Fatal(err)
    19  		}
    20  		defer l.Close()
    21  	}
    22  
    23  	// TODO nats case test
    24  	// natsAddr := "_INBOX.bID2CMRvlNp0vt4tgNBHWf"
    25  	// Expect addr DO NOT has extra ":" at the end!
    26  }
    27  
    28  // TestProxyEnv checks whether we have proxy/network settings in env.
    29  func TestProxyEnv(t *testing.T) {
    30  	service := "foo"
    31  	address := []string{"bar"}
    32  
    33  	s, a, ok := Proxy(service, address)
    34  	if ok {
    35  		t.Fatal("Should not have proxy", s, a, ok)
    36  	}
    37  
    38  	test := func(key, val, expectSrv, expectAddr string) {
    39  		// set env
    40  		os.Setenv(key, val)
    41  
    42  		s, a, ok := Proxy(service, address)
    43  		if !ok {
    44  			t.Fatal("Expected proxy")
    45  		}
    46  		if len(expectSrv) > 0 && s != expectSrv {
    47  			t.Fatal("Expected proxy service", expectSrv, "got", s)
    48  		}
    49  		if len(expectAddr) > 0 {
    50  			if len(a) == 0 || a[0] != expectAddr {
    51  				t.Fatal("Expected proxy address", expectAddr, "got", a)
    52  			}
    53  		}
    54  
    55  		os.Unsetenv(key)
    56  	}
    57  
    58  	test("MICRO_PROXY", "service", "go.micro.proxy", "")
    59  	test("MICRO_NETWORK", "service", "go.micro.network", "")
    60  	test("MICRO_NETWORK_ADDRESS", "10.0.0.1:8081", "", "10.0.0.1:8081")
    61  }