Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gRPC over cmux and add unit tests #1758

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions cmd/query/app/grpc_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
)

var (
grpcServerPort = ":0"
errStorageMsgGRPC = "Storage error"
errStorageGRPC = errors.New(errStorageMsgGRPC)
errStatusStorageGRPC = status.Error(2, errStorageMsgGRPC)
Expand Down Expand Up @@ -138,7 +137,7 @@ type grpcClient struct {
}

func newGRPCServer(t *testing.T, q *querysvc.QueryService, logger *zap.Logger, tracer opentracing.Tracer) (*grpc.Server, net.Addr) {
lis, _ := net.Listen("tcp", grpcServerPort)
lis, _ := net.Listen("tcp", ":0")
grpcServer := grpc.NewServer()
grpcHandler := NewGRPCHandler(q, logger, tracer)
api_v2.RegisterQueryServiceServer(grpcServer, grpcHandler)
Expand All @@ -151,8 +150,8 @@ func newGRPCServer(t *testing.T, q *querysvc.QueryService, logger *zap.Logger, t
return grpcServer, lis.Addr()
}

func newGRPCClient(t *testing.T, addr net.Addr) *grpcClient {
conn, err := grpc.Dial(addr.String(), grpc.WithInsecure())
func newGRPCClient(t *testing.T, addr string) *grpcClient {
conn, err := grpc.Dial(addr, grpc.WithInsecure())
require.NoError(t, err)

return &grpcClient{
Expand Down Expand Up @@ -192,7 +191,7 @@ func initializeTestServerGRPCWithOptions(t *testing.T) *grpcServer {

func withServerAndClient(t *testing.T, actualTest func(server *grpcServer, client *grpcClient)) {
server := initializeTestServerGRPCWithOptions(t)
client := newGRPCClient(t, server.lisAddr)
client := newGRPCClient(t, server.lisAddr.String())
defer server.server.Stop()
defer client.conn.Close()

Expand Down
7 changes: 6 additions & 1 deletion cmd/query/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ func (s *Server) Start() error {

grpcListener := cmuxServer.Match(
cmux.HTTP2HeaderField("content-type", "application/grpc"),
cmux.HTTP2HeaderField("content-type", "application/grpc+proto"))
cmux.HTTP2HeaderField("content-type", "application/grpc+proto"),
)
// grpcListener := cmuxServer.MatchWithWriters(
// cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"),
// cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc+proto"),
// )
httpListener := cmuxServer.Match(cmux.Any())

go func() {
Expand Down
39 changes: 35 additions & 4 deletions cmd/query/app/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package app

import (
"context"
"fmt"
"testing"
"time"
Expand All @@ -28,6 +29,9 @@ import (
"github.com/jaegertracing/jaeger/cmd/query/app/querysvc"
"github.com/jaegertracing/jaeger/pkg/healthcheck"
"github.com/jaegertracing/jaeger/ports"
"github.com/jaegertracing/jaeger/proto-gen/api_v2"
depsmocks "github.com/jaegertracing/jaeger/storage/dependencystore/mocks"
spanstoremocks "github.com/jaegertracing/jaeger/storage/spanstore/mocks"
)

func TestServerError(t *testing.T) {
Expand All @@ -43,13 +47,40 @@ func TestServer(t *testing.T) {
flagsSvc := flags.NewService(ports.AgentAdminHTTP)
flagsSvc.Logger = zap.NewNop()

querySvc := &querysvc.QueryService{}
spanReader := &spanstoremocks.Reader{}
dependencyReader := &depsmocks.Reader{}

querySvc := querysvc.NewQueryService(spanReader, dependencyReader, querysvc.QueryServiceOptions{})

tracer := opentracing.NoopTracer{}

server := NewServer(flagsSvc, querySvc, &QueryOptions{Port: ports.QueryAdminHTTP,
BearerTokenPropagation: true}, tracer)
server := NewServer(flagsSvc, querySvc,
&QueryOptions{Port: ports.QueryAdminHTTP, BearerTokenPropagation: true},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the listening port here to -

		&QueryOptions{Port: ports.QueryHTTP, BearerTokenPropagation: true},

.. and using cmuxServer.MatchWithWriters(...) as suggested by @32leaves seems to work for me.

We definitely needed an integration test with cmux though, thanks for pointing out!

tracer)
assert.NoError(t, server.Start())

client := newGRPCClient(t, fmt.Sprintf(":%d", ports.QueryHTTP))
defer client.conn.Close()

var queryErr error
for i := 0; i < 10; i++ {
queryErr = func() error {
ctx, cancel := context.WithTimeout(context.Background(), 2000*time.Millisecond)
defer cancel()

_, err := client.GetTrace(ctx, &api_v2.GetTraceRequest{})
if err != nil {
t.Log("cannot GetTrace", err)
}
return err
}()
if queryErr == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
assert.NoError(t, queryErr, "Connection test did not succeed")

// TODO wait for servers to come up and test http and grpc endpoints
time.Sleep(1 * time.Second)

Expand Down Expand Up @@ -82,6 +113,6 @@ func TestServerGracefulExit(t *testing.T) {

for _, logEntry := range logs.All() {
assert.True(t, logEntry.Level != zap.ErrorLevel,
fmt.Sprintf("Error log found on server exit: %v", logEntry))
"Error log found on server exit: %v", logEntry)
}
}