Skip to content

Commit

Permalink
Merge pull request #2 from akutz/feature/tcp-ports
Browse files Browse the repository at this point in the history
TCPPort Helper Funcs
  • Loading branch information
akutz committed Mar 10, 2016
2 parents fc77507 + 3391252 commit 6fa2e80
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ go:
before_install:
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
- go get golang.org/x/tools/cmd/cover
- go get github.com/Masterminds/glide
install:
- glide up
- glide up --resolve-current
- go get -t $(glide nv)
script:
- go install $(glide nv)
- goveralls -service=travis-ci
3 changes: 1 addition & 2 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ import:
repo: https://github.com/kardianos/osext.git
vcs: git
- package: github.com/stretchr/testify
ref: feature/glide
repo: https://github.com/akutz/testify.git
repo: https://github.com/stretchr/testify.git
vcs: git
37 changes: 37 additions & 0 deletions gotil.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,40 @@ func HomeDir() string {
homeDirSet = true
return homeDir
}

const (
minTCPPort = 0
maxTCPPort = 65535
maxReservedTCPPort = 1024
maxRandTCPPort = maxTCPPort - (maxReservedTCPPort + 1)
)

var (
tcpPortRand = rand.New(rand.NewSource(time.Now().UnixNano()))
)

// IsTCPPortAvailable returns a flag indicating whether or not a TCP port is
// available.
func IsTCPPortAvailable(port int) bool {
if port < minTCPPort || port > maxTCPPort {
return false
}
conn, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
return false
}
conn.Close()
return true
}

// RandomTCPPort gets a free, random TCP port between 1025-65535. If no free
// ports are available -1 is returned.
func RandomTCPPort() int {
for i := maxReservedTCPPort; i < maxTCPPort; i++ {
p := tcpPortRand.Intn(maxRandTCPPort) + maxReservedTCPPort + 1
if IsTCPPortAvailable(p) {
return p
}
}
return -1
}
27 changes: 27 additions & 0 deletions gotil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -406,3 +407,29 @@ func TestWriteIndented(t *testing.T) {
func TestHomeDir(t *testing.T) {
assert.NotEqual(t, "", HomeDir())
}

func TestRandomTCPPort(t *testing.T) {
p := RandomTCPPort()
addr := fmt.Sprintf("127.0.0.1:%d", p)
t.Logf("listening on addr %s", addr)
conn, err := net.Listen("tcp", addr)
if err != nil {
t.Fatal(err)
}
conn.Close()
}

func TestIsTCPPortAvailable(t *testing.T) {
p := RandomTCPPort()
addr := fmt.Sprintf("127.0.0.1:%d", p)
t.Logf("listening on addr %s", addr)
conn, err := net.Listen("tcp", addr)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
_, err2 := net.Listen("tcp", addr)
if err2 == nil {
t.Fatalf("addr should be in use %s", addr)
}
}

0 comments on commit 6fa2e80

Please sign in to comment.