github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/port_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"cgl.tideland.biz/asserts"
     5  	"net"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func addrPort(address net.Addr) string {
    11  	parts := strings.Split(address.String(), ":")
    12  	return parts[len(parts)-1]
    13  }
    14  
    15  func Test_netListenerInRange(t *testing.T) {
    16  	assert := asserts.NewTestingAsserts(t, true)
    17  
    18  	// Open up port 10000 so that we take up a port
    19  	L1000, err := net.Listen("tcp", "127.0.0.1:11000")
    20  	defer L1000.Close()
    21  	assert.Nil(err, "should be able to bind to port 10000")
    22  
    23  	if err == nil {
    24  		// Verify it selects an open port
    25  		L := netListenerInRange(11000, 11005)
    26  		assert.NotNil(L, "should have a listener")
    27  		assert.Equal(addrPort(L.Addr()), "11001", "should bind to open port")
    28  
    29  		// Returns nil if there are no open ports
    30  		L = netListenerInRange(11000, 11000)
    31  		assert.Nil(L, "should not get a listener")
    32  	}
    33  }