Skip to content

Commit b2275e7

Browse files
authored
Allow setting own gRPC server when calling NewService (#400)
* Add `NewServiceWithGrpcServer` to gRPC service Signed-off-by: pimmerks <[email protected]> * Apply suggestion of not creating new variable Signed-off-by: pimmerks <[email protected]> --------- Signed-off-by: pimmerks <[email protected]>
1 parent 3929306 commit b2275e7

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

service/grpc/service.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,21 @@ func NewService(address string) (s common.Service, err error) {
3939
err = fmt.Errorf("failed to TCP listen on %s: %w", address, err)
4040
return
4141
}
42-
s = newService(lis)
42+
s = newService(lis, nil)
4343
return
4444
}
4545

4646
// NewServiceWithListener creates new Service with specific listener.
4747
func NewServiceWithListener(lis net.Listener, opts ...grpc.ServerOption) common.Service {
48-
return newService(lis, opts...)
48+
return newService(lis, nil, opts...)
4949
}
5050

51-
func newService(lis net.Listener, opts ...grpc.ServerOption) *Server {
51+
// NewServiceWithGrpcServer creates a new Service with specific listener and grpcServer
52+
func NewServiceWithGrpcServer(lis net.Listener, server *grpc.Server) common.Service {
53+
return newService(lis, server)
54+
}
55+
56+
func newService(lis net.Listener, grpcServer *grpc.Server, opts ...grpc.ServerOption) *Server {
5257
s := &Server{
5358
listener: lis,
5459
invokeHandlers: make(map[string]common.ServiceInvocationHandler),
@@ -57,10 +62,13 @@ func newService(lis net.Listener, opts ...grpc.ServerOption) *Server {
5762
authToken: os.Getenv(common.AppAPITokenEnvVar),
5863
}
5964

60-
gs := grpc.NewServer(opts...)
61-
pb.RegisterAppCallbackServer(gs, s)
62-
pb.RegisterAppCallbackHealthCheckServer(gs, s)
63-
s.grpcServer = gs
65+
if grpcServer == nil {
66+
grpcServer = grpc.NewServer(opts...)
67+
}
68+
69+
pb.RegisterAppCallbackServer(grpcServer, s)
70+
pb.RegisterAppCallbackHealthCheckServer(grpcServer, s)
71+
s.grpcServer = grpcServer
6472

6573
return s
6674
}

service/grpc/service_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"testing"
1818

1919
"github.com/stretchr/testify/assert"
20+
"google.golang.org/grpc"
2021
"google.golang.org/grpc/test/bufconn"
2122
)
2223

@@ -31,13 +32,19 @@ func TestServerWithListener(t *testing.T) {
3132
assert.NotNil(t, server)
3233
}
3334

35+
func TestServerWithGrpcServer(t *testing.T) {
36+
grpcServer := grpc.NewServer()
37+
server := NewServiceWithGrpcServer(bufconn.Listen(1024*1024), grpcServer)
38+
assert.NotNil(t, server)
39+
}
40+
3441
func TestService(t *testing.T) {
3542
_, err := NewService("")
3643
assert.Errorf(t, err, "expected error from lack of address")
3744
}
3845

3946
func getTestServer() *Server {
40-
return newService(bufconn.Listen(1024 * 1024))
47+
return newService(bufconn.Listen(1024*1024), nil)
4148
}
4249

4350
func startTestServer(server *Server) {

0 commit comments

Comments
 (0)