Skip to content

Commit

Permalink
Add test test mock starter
Browse files Browse the repository at this point in the history
  • Loading branch information
mactep committed Aug 21, 2023
1 parent e8ce0ce commit a5d65f4
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/grpc/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package grpc

import (
"context"
"fmt"
"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 {
fmt.Println("Starting test gRPC service")
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
}

0 comments on commit a5d65f4

Please sign in to comment.