From 8711fea1ad579fa2ddfd1ca5b8e4d58f79314432 Mon Sep 17 00:00:00 2001 From: Naitik Shah Date: Mon, 19 Aug 2013 23:12:26 -0700 Subject: [PATCH] remove last sleep in test path --- gracehttp/testserver/testserver.go | 59 +++++++++++++++++++----------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/gracehttp/testserver/testserver.go b/gracehttp/testserver/testserver.go index d56828d..68fc4e4 100644 --- a/gracehttp/testserver/testserver.go +++ b/gracehttp/testserver/testserver.go @@ -4,11 +4,14 @@ package main import ( "encoding/json" "flag" - "github.com/daaku/go.grace/gracehttp" + "fmt" "log" "net/http" "os" + "sync" "time" + + "github.com/daaku/go.grace/gracehttp" ) type response struct { @@ -16,40 +19,52 @@ type response struct { Pid int } -var ( - address0 = flag.String("a0", ":48567", "Zero address to bind to.") - address1 = flag.String("a1", ":48568", "First address to bind to.") - address2 = flag.String("a2", ":48569", "Second address to bind to.") - finished = make(chan bool) -) - -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) +func wait(wg *sync.WaitGroup, addr string) { + defer wg.Done() + url := fmt.Sprintf("http://%s/sleep/?duration=0", addr) + for { + if _, err := http.Get(url); err == nil { + return + } } - finished <- true } 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() + err := flag.Set("gracehttp.log", "false") if err != nil { log.Fatalf("Error setting gracehttp.log: %s", err) } - go serve() - time.Sleep(time.Second * 2) // BUG + // print json to stderr once we can successfully connect to all three + // 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 { - log.Fatalf("Error writing startup json: %s", err) + log.Fatalf("Error in gracehttp.Serve: %s", err) } - <-finished } func newHandler() http.Handler {