Skip to content

Commit

Permalink
gRPC helpers (#59)
Browse files Browse the repository at this point in the history
* Add bare server

* Add test test mock starter

* Add grpc module

* Remove left over println

* Add method to remove random gRPC response
  • Loading branch information
mactep authored Aug 28, 2023
1 parent 112a763 commit 729c57c
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pkg/grpc/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package grpc

import (
"context"
"net"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"go.uber.org/fx"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
)

func StartTestGrpcService(lc fx.Lifecycle, server GrpcServer, logger *zerolog.Logger) *grpc.ClientConn {
buffer := 101024 * 1024
lis := bufconn.Listen(buffer)

baseServer := server.GetGrpcServer()
go func() {
if err := baseServer.Serve(lis); err != nil {
logger.Printf("error serving server: %v", err)
}
}()

conn, err := grpc.DialContext(context.Background(), "",
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
logger.Printf("error connecting to server: %v", err)
}

closer := func() {
err := lis.Close()
if err != nil {
log.Printf("error closing listener: %v", err)
}
baseServer.Stop()
}

lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
closer()
return nil
},
})

return conn
}
9 changes: 9 additions & 0 deletions pkg/grpc/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package grpc

import "go.uber.org/fx"

var Module = fx.Options(
fx.Provide(
NewServer,
),
)
56 changes: 56 additions & 0 deletions pkg/grpc/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package grpc

import (
"context"
"fmt"
"net"

"github.com/rs/zerolog"
"google.golang.org/grpc"
)

type BareServer struct {
Server *grpc.Server
}

func NewServer(logger *zerolog.Logger) *BareServer {
grpcServer := &BareServer{}

sv := grpc.NewServer(
grpc.UnaryInterceptor(
grpcLoggerInterceptor(logger),
),
)
grpcServer.Server = sv

return grpcServer
}

func (s *BareServer) GetListener() (net.Listener, error) {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 50051))
if err != nil {
return nil, err
}

return lis, nil
}

func (s *BareServer) GetGrpcServer() *grpc.Server {
return s.Server
}

func grpcLoggerInterceptor(logger *zerolog.Logger) func(context.Context, interface{}, *grpc.UnaryServerInfo, grpc.UnaryHandler) (interface{}, error) {
return func(ctx context.Context,
req interface{},
_ *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
// Add logger to context
lctx := logger.WithContext(ctx)

// Calls the handler
h, err := handler(lctx, req)

return h, err
}
}
16 changes: 16 additions & 0 deletions pkg/grpc/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package grpc

import "strings"

type ProtoMessage interface {
String() string
}

// StringifySnapshot converts a protobuf response to a string and removes all double spaces.
// This is needed because of a pseudo-random effect https://github.com/golang/protobuf/issues/1121
func StringifySnapshot(resp ProtoMessage) string {
respStr := resp.String()
respStr = strings.ReplaceAll(respStr, " ", " ")

return respStr
}

0 comments on commit 729c57c

Please sign in to comment.