Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Converts the client_tribe_func_test.go to use a net.Listen to find an…
Browse files Browse the repository at this point in the history
… open port
  • Loading branch information
lynxbat authored and jcooklin committed Nov 24, 2015
1 parent 7a471fa commit d547321
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions mgmt/rest/client/client_tribe_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"

Expand All @@ -41,16 +41,24 @@ import (
"github.com/intelsdi-x/pulse/scheduler"
)

var NextPort int32 = 46000

func getPort() int {
defer incrPort()
return int(atomic.LoadInt32(&NextPort))
}

func incrPort() {
atomic.AddInt32(&NextPort, 10)
NextPort += 10
// This attempts to use net.Listen to find an open port since
// the tribe config has to know of one BEFORE the REST API starts...
count := 0
// This will loop 1000 times before panicing
// If it finds a port it will return out of the function
for count < 1000 {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err == nil {
// Grab port from listener
p := ln.Addr().(*net.TCPAddr).Port
ln.Close()
return p
}
count++
}
// We tried 1000 times and just give up
panic("Could not get a port")
}

func readBody(r *http.Response) []byte {
Expand Down

0 comments on commit d547321

Please sign in to comment.