github.com/micro/go-micro/v2@v2.9.1/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  
    29  // TestProxyEnv checks whether we have proxy/network settings in env
    30  func TestProxyEnv(t *testing.T) {
    31  	service := "foo"
    32  	address := []string{"bar"}
    33  
    34  	s, a, ok := Proxy(service, address)
    35  	if ok {
    36  		t.Fatal("Should not have proxy", s, a, ok)
    37  	}
    38  
    39  	test := func(key, val, expectSrv, expectAddr string) {
    40  		// set env
    41  		os.Setenv(key, val)
    42  
    43  		s, a, ok := Proxy(service, address)
    44  		if !ok {
    45  			t.Fatal("Expected proxy")
    46  		}
    47  		if len(expectSrv) > 0 && s != expectSrv {
    48  			t.Fatal("Expected proxy service", expectSrv, "got", s)
    49  		}
    50  		if len(expectAddr) > 0 {
    51  			if len(a) == 0 || a[0] != expectAddr {
    52  				t.Fatal("Expected proxy address", expectAddr, "got", a)
    53  			}
    54  		}
    55  
    56  		os.Unsetenv(key)
    57  	}
    58  
    59  	test("MICRO_PROXY", "service", "go.micro.proxy", "")
    60  	test("MICRO_NETWORK", "service", "go.micro.network", "")
    61  	test("MICRO_NETWORK_ADDRESS", "10.0.0.1:8081", "", "10.0.0.1:8081")
    62  }