2013-05-04 16:17:22 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func addrPort(address net.Addr) string {
|
|
|
|
parts := strings.Split(address.String(), ":")
|
2013-05-10 20:01:24 -04:00
|
|
|
return parts[len(parts)-1]
|
2013-05-04 16:17:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_netListenerInRange(t *testing.T) {
|
2013-05-04 16:32:02 -04:00
|
|
|
// Open up port 10000 so that we take up a port
|
2013-07-01 13:44:12 -04:00
|
|
|
L1000, err := net.Listen("tcp", "127.0.0.1:11000")
|
2013-05-04 16:17:22 -04:00
|
|
|
defer L1000.Close()
|
2013-10-16 23:04:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
2013-05-04 16:17:22 -04:00
|
|
|
|
2013-05-05 20:38:50 -04:00
|
|
|
if err == nil {
|
|
|
|
// Verify it selects an open port
|
|
|
|
L := netListenerInRange(11000, 11005)
|
2013-10-16 23:04:57 -04:00
|
|
|
if L == nil {
|
|
|
|
t.Fatal("L should not be nil")
|
|
|
|
}
|
|
|
|
if addrPort(L.Addr()) != "11001" {
|
|
|
|
t.Fatalf("bad: %s", L.Addr())
|
|
|
|
}
|
2013-05-04 16:17:22 -04:00
|
|
|
|
2013-05-05 20:38:50 -04:00
|
|
|
// Returns nil if there are no open ports
|
|
|
|
L = netListenerInRange(11000, 11000)
|
2013-10-16 23:04:57 -04:00
|
|
|
if L != nil {
|
|
|
|
t.Fatalf("bad: %#v", L)
|
|
|
|
}
|
2013-05-05 20:38:50 -04:00
|
|
|
}
|
2013-05-04 16:17:22 -04:00
|
|
|
}
|