packer-cn/packer/rpc/port_test.go

31 lines
734 B
Go
Raw Normal View History

package rpc
import (
"cgl.tideland.biz/asserts"
"net"
"strings"
"testing"
)
func addrPort(address net.Addr) string {
parts := strings.Split(address.String(), ":")
return parts[len(parts) - 1]
}
func Test_netListenerInRange(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Verify it selects an open port
L1000, err := net.Listen("tcp", ":10000")
defer L1000.Close()
assert.Nil(err, "should be able to bind to port 10000")
L := netListenerInRange(10000, 10005)
assert.NotNil(L, "should have a listener")
assert.Equal(addrPort(L.Addr()), "10001", "should bind to open port")
// Returns nil if there are no open ports
L = netListenerInRange(10000, 10000)
assert.Nil(L, "should not get a listener")
}