Skip to content

Commit 2117723

Browse files
committed
Add gRPC v1 Reflection API support for client
1 parent 72abba9 commit 2117723

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

fixture/mock_grpc_server_interceptor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func NewCallStats() *CallStats {
1919
}
2020
}
2121

22-
func (cs *CallStats) UnaryInterceptor() grpc.UnaryServerInterceptor {
22+
func (callStats *CallStats) UnaryInterceptor() grpc.UnaryServerInterceptor {
2323
return func(
2424
ctx context.Context,
2525
req any,
@@ -28,10 +28,10 @@ func (cs *CallStats) UnaryInterceptor() grpc.UnaryServerInterceptor {
2828
) (any, error) {
2929
resp, err := handler(ctx, req)
3030

31-
cs.mu.Lock()
32-
defer cs.mu.Unlock()
31+
callStats.mu.Lock()
32+
defer callStats.mu.Unlock()
3333

34-
cs.StatusesByMethod[info.FullMethod] = append(cs.StatusesByMethod[info.FullMethod], status.Convert(err))
34+
callStats.StatusesByMethod[info.FullMethod] = append(callStats.StatusesByMethod[info.FullMethod], status.Convert(err))
3535

3636
return resp, err
3737
}

fixture/mock_server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ func StartGrpcTargetTestServerReflectionV1Alpha(callStats *CallStats) (*grpc.Ser
4040
}
4141

4242
// It uses the test.proto from grpc-testing: https://github.com/grpc/grpc-go/blob/40a879c23a0dc77234d17e0699d074d5fd151bd0/test/grpc_testing/test.proto
43-
func startGrpcTargetTestServer(callStats *CallStats, reflRegFun func(*grpc.Server)) (*grpc.Server, int) {
43+
func startGrpcTargetTestServer(callStats *CallStats, reflRegFunc func(*grpc.Server)) (*grpc.Server, int) {
4444
server := grpc.NewServer(
4545
grpc.UnaryInterceptor(callStats.UnaryInterceptor()),
4646
)
4747
grpc_testing.RegisterTestServiceServer(server, &grpc_testing.UnimplementedTestServiceServer{})
48-
reflRegFun(server)
48+
reflRegFunc(server)
4949

5050
listener, err := net.Listen("tcp", ":0")
5151
if err != nil {

internal/pkg/grpc/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"bytes"
1919
"errors"
2020
"fmt"
21-
"google.golang.org/grpc/credentials/insecure"
2221
"log"
2322
"mittens/internal/pkg/placeholders"
2423
"mittens/internal/pkg/response"
@@ -31,8 +30,8 @@ import (
3130
"golang.org/x/net/context"
3231
"google.golang.org/grpc"
3332
"google.golang.org/grpc/credentials"
33+
"google.golang.org/grpc/credentials/insecure"
3434
"google.golang.org/grpc/metadata"
35-
reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
3635
)
3736

3837
// Client represents a gRPC client.
@@ -85,7 +84,7 @@ func (c *Client) Connect(headers []string) error {
8584
return fmt.Errorf("gRPC dial: %v", err)
8685
}
8786

88-
reflectionClient := grpcreflect.NewClientV1Alpha(contextWithMetadata, reflectpb.NewServerReflectionClient(conn))
87+
reflectionClient := grpcreflect.NewClientAuto(contextWithMetadata, conn)
8988
descriptorSource := grpcurl.DescriptorSourceFromServer(contextWithMetadata, reflectionClient)
9089

9190
log.Print("gRPC client connected")

test/root_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ func TestHttp(t *testing.T) {
161161
assert.True(t, readyFileExists)
162162
}
163163

164-
func TestGrpcAndHttpWithVariousGrpcServerReflectionAPICombinations(t *testing.T) {
164+
func TestGrpcAndHttpWithVariousReflectionAPICombinations(t *testing.T) {
165165
testConfigs := []struct {
166-
name string
167-
setupGrpcServer func(*fixture.CallStats) (*grpc.Server, int)
166+
name string
167+
setupFunc func(*fixture.CallStats) (*grpc.Server, int)
168168
}{
169169
{
170170
"Run with v1 and v1alpha Reflection API support",
@@ -190,9 +190,9 @@ func TestGrpcAndHttpWithVariousGrpcServerReflectionAPICombinations(t *testing.T)
190190
var server *grpc.Server
191191
var port int
192192

193-
if testConfig.setupGrpcServer != nil {
193+
if testConfig.setupFunc != nil {
194194
callStats = fixture.NewCallStats()
195-
server, port = testConfig.setupGrpcServer(callStats)
195+
server, port = testConfig.setupFunc(callStats)
196196
defer server.GracefulStop()
197197
} else {
198198
callStats = grpcCallStats

0 commit comments

Comments
 (0)