Skip to content

Commit

Permalink
Avoid timing issues in the integration tests
Browse files Browse the repository at this point in the history
Wait for the gateway server to get ready.
#609 (comment)
  • Loading branch information
yugui committed Apr 27, 2018
1 parent 5ae7f11 commit cfd8078
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
7 changes: 6 additions & 1 deletion examples/gateway/handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gateway

import (
"fmt"
"net/http"
"path"
"strings"
Expand Down Expand Up @@ -44,5 +45,9 @@ func preflightHandler(w http.ResponseWriter, r *http.Request) {
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"}
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
glog.Infof("preflight request for %s", r.URL.Path)
return
}

func healthzHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "ok")
}
1 change: 1 addition & 0 deletions examples/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Run(ctx context.Context, opts Options) error {

mux := http.NewServeMux()
mux.HandleFunc("/swagger/", swaggerServer(opts.SwaggerDir))
mux.HandleFunc("/healthz", healthzHandler)

gw, err := newGateway(ctx, opts.GRPCServer.Network, opts.GRPCServer.Addr, opts.Mux)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions examples/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"
"sync"
"testing"
"time"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -60,8 +59,9 @@ func TestForwardResponseOption(t *testing.T) {
return
}
}()

time.Sleep(100 * time.Millisecond)
if err := waitForGateway(ctx, 8081); err != nil {
t.Errorf("waitForGateway(ctx, 8081) failed with %v; want success", err)
}
testEcho(t, 8081, "application/vnd.docker.plugins.v1.1+json")
}

Expand Down
30 changes: 29 additions & 1 deletion examples/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"flag"
"fmt"
"net/http"
"os"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -32,6 +34,30 @@ func runGateway(ctx context.Context, addr string, opts ...gwruntime.ServeMuxOpti
})
}

func waitForGateway(ctx context.Context, port uint16) error {
t := time.NewTimer(10 * time.Second)

var err error
for {
if r, err := http.Get(fmt.Sprintf("http://localhost:%d/healthz", port)); err == nil {
if r.Status == http.StatusOK {
return nil
}
err = fmt.Errorf("server localhost:%d returned an unexpected status %d", port, r.Status)
}

glog.Infof("Waiting for localhost:%d to get ready", port)
s := time.NewTimer(10 * time.Millisecond)
select {
case <-ctx.Done():
return err
case <-t.C:
return err
case <-s.C:
}
}
}

func runServers(ctx context.Context) <-chan error {
ch := make(chan error, 2)
go func() {
Expand All @@ -57,7 +83,9 @@ func TestMain(m *testing.M) {

ch := make(chan int, 1)
go func() {
time.Sleep(100 * time.Millisecond)
if err := waitForGateway(ctx, 8080); err != nil {
glog.Errorf("waitForGateway(ctx, 8080) failed with %v; want success", err)
}
ch <- m.Run()
}()

Expand Down
21 changes: 9 additions & 12 deletions examples/integration/proto_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ func TestWithProtoErrorHandler(t *testing.T) {

const port = 8082
go runServer(ctx, t, port)

// Waiting for the server's getting available.
// TODO(yugui) find a better way to wait
time.Sleep(100 * time.Millisecond)

if err := waitForGateway(ctx, 8082); err != nil {
t.Errorf("waitForGateway(ctx, 8082) failed with %v; want success", err)
}
testEcho(t, port, "application/json")
testEchoBody(t, port)
}
Expand All @@ -50,9 +48,9 @@ func TestABEWithProtoErrorHandler(t *testing.T) {

const port = 8083
go runServer(ctx, t, port)
// Waiting for the server's getting available.
// TODO(yugui) find a better way to wait
time.Sleep(100 * time.Millisecond)
if err := waitForGateway(ctx, 8083); err != nil {
t.Errorf("waitForGateway(ctx, 8083) failed with %v; want success", err)
}

testABECreate(t, port)
testABECreateBody(t, port)
Expand Down Expand Up @@ -122,10 +120,9 @@ func TestUnknownPathWithProtoError(t *testing.T) {

const port = 8084
go runServer(ctx, t, port)

// Waiting for the server's getting available.
// TODO(yugui) find a better way to wait
time.Sleep(100 * time.Millisecond)
if err := waitForGateway(ctx, 8084); err != nil {
t.Errorf("waitForGateway(ctx, 8084) failed with %v; want success", err)
}

url := fmt.Sprintf("http://localhost:%d", port)
resp, err := http.Post(url, "application/json", strings.NewReader("{}"))
Expand Down

0 comments on commit cfd8078

Please sign in to comment.