remove last sleep in test path

This commit is contained in:
Naitik Shah 2013-08-19 23:12:26 -07:00
parent a4481adb67
commit 8711fea1ad

View File

@ -4,11 +4,14 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"github.com/daaku/go.grace/gracehttp" "fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
"sync"
"time" "time"
"github.com/daaku/go.grace/gracehttp"
) )
type response struct { type response struct {
@ -16,40 +19,52 @@ type response struct {
Pid int Pid int
} }
var ( func wait(wg *sync.WaitGroup, addr string) {
address0 = flag.String("a0", ":48567", "Zero address to bind to.") defer wg.Done()
address1 = flag.String("a1", ":48568", "First address to bind to.") url := fmt.Sprintf("http://%s/sleep/?duration=0", addr)
address2 = flag.String("a2", ":48569", "Second address to bind to.") for {
finished = make(chan bool) if _, err := http.Get(url); err == nil {
) return
}
func serve() {
err := gracehttp.Serve(
&http.Server{Addr: *address0, Handler: newHandler()},
&http.Server{Addr: *address1, Handler: newHandler()},
&http.Server{Addr: *address2, Handler: newHandler()},
)
if err != nil {
log.Fatalf("Error in gracehttp.Serve: %s", err)
} }
finished <- true
} }
func main() { func main() {
var addrs [3]string
flag.StringVar(&addrs[0], "a0", ":48560", "Zero address to bind to.")
flag.StringVar(&addrs[1], "a1", ":48561", "First address to bind to.")
flag.StringVar(&addrs[2], "a2", ":48562", "Second address to bind to.")
flag.Parse() flag.Parse()
err := flag.Set("gracehttp.log", "false") err := flag.Set("gracehttp.log", "false")
if err != nil { if err != nil {
log.Fatalf("Error setting gracehttp.log: %s", err) log.Fatalf("Error setting gracehttp.log: %s", err)
} }
go serve() // print json to stderr once we can successfully connect to all three
time.Sleep(time.Second * 2) // BUG // addresses. the ensures we only print the line once we're ready to serve.
go func() {
var wg sync.WaitGroup
wg.Add(len(addrs))
for _, addr := range addrs {
go wait(&wg, addr)
}
wg.Wait()
err = json.NewEncoder(os.Stderr).Encode(&response{Pid: os.Getpid()}) err = json.NewEncoder(os.Stderr).Encode(&response{Pid: os.Getpid()})
if err != nil {
log.Fatalf("Error writing startup json: %s", err)
}
}()
err = gracehttp.Serve(
&http.Server{Addr: addrs[0], Handler: newHandler()},
&http.Server{Addr: addrs[1], Handler: newHandler()},
&http.Server{Addr: addrs[2], Handler: newHandler()},
)
if err != nil { if err != nil {
log.Fatalf("Error writing startup json: %s", err) log.Fatalf("Error in gracehttp.Serve: %s", err)
} }
<-finished
} }
func newHandler() http.Handler { func newHandler() http.Handler {