Skip to content

Commit

Permalink
add test case for invoke in process gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
hb-chen committed Apr 19, 2020
1 parent 2db19b7 commit 6e57b55
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
10 changes: 8 additions & 2 deletions examples/internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1660,13 +1660,19 @@ func testResponseStrings(t *testing.T, port int) {
}

func TestRequestQueryParams(t *testing.T) {
testRequestQueryParams(t, 8088)
}

func TestRequestQueryParamsInProcessGateway(t *testing.T) {
testRequestQueryParams(t, 8089)
}

func testRequestQueryParams(t *testing.T, port int) {
if testing.Short() {
t.Skip()
return
}

port := 8088

formValues := url.Values{}
formValues.Set("string_value", "hello-world")
formValues.Add("repeated_string_value", "demo1")
Expand Down
7 changes: 6 additions & 1 deletion examples/internal/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func waitForGateway(ctx context.Context, port uint16) error {
}

func runServers(ctx context.Context) <-chan error {
ch := make(chan error, 2)
ch := make(chan error, 3)
go func() {
if err := server.Run(ctx, *network, *endpoint); err != nil {
ch <- fmt.Errorf("cannot run grpc service: %v", err)
Expand All @@ -68,6 +68,11 @@ func runServers(ctx context.Context) <-chan error {
ch <- fmt.Errorf("cannot run gateway service: %v", err)
}
}()
go func() {
if err := server.RunInProcessGateway(ctx, ":8089"); err != nil {
ch <- fmt.Errorf("cannot run in process gateway service: %v", err)
}
}()
return ch
}

Expand Down
36 changes: 36 additions & 0 deletions examples/internal/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package server
import (
"context"
"net"
"net/http"

"github.com/golang/glog"
examples "github.com/grpc-ecosystem/grpc-gateway/examples/internal/proto/examplepb"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -38,3 +40,37 @@ func Run(ctx context.Context, network, address string) error {
}()
return s.Serve(l)
}

// RunInProcessGateway starts the invoke in process http gateway.
func RunInProcessGateway(ctx context.Context, addr string, opts ...runtime.ServeMuxOption) error {
mux := runtime.NewServeMux(opts...)

examples.RegisterEchoServiceHandlerServer(ctx, mux, newEchoServer())
examples.RegisterFlowCombinationHandlerServer(ctx, mux, newFlowCombinationServer())
examples.RegisterNonStandardServiceHandlerServer(ctx, mux, newNonStandardServer())

abe := newABitOfEverythingServer()
examples.RegisterABitOfEverythingServiceHandlerServer(ctx, mux, abe)
examples.RegisterStreamServiceHandlerServer(ctx, mux, abe)
examples.RegisterResponseBodyServiceHandlerServer(ctx, mux, newResponseBodyServer())

s := &http.Server{
Addr: addr,
Handler: mux,
}

go func() {
<-ctx.Done()
glog.Infof("Shutting down the http gateway server")
if err := s.Shutdown(context.Background()); err != nil {
glog.Errorf("Failed to shutdown http gateway server: %v", err)
}
}()

if err := s.ListenAndServe(); err != http.ErrServerClosed {
glog.Errorf("Failed to listen and serve: %v", err)
return err
}
return nil

}

0 comments on commit 6e57b55

Please sign in to comment.