Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Fix for the google wrappers support in unary calls and tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Sep 7, 2023
1 parent 9328083 commit 05a4176
Show file tree
Hide file tree
Showing 6 changed files with 478 additions and 5 deletions.
96 changes: 92 additions & 4 deletions grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,28 @@ import (
"strings"
"testing"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/known/wrapperspb"

"github.com/dop251/goja"
"github.com/golang/protobuf/ptypes/any"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"go.k6.io/k6/lib/testutils/httpmultibin"
grpcanytesting "go.k6.io/k6/lib/testutils/httpmultibin/grpc_any_testing"
"go.k6.io/k6/lib/testutils/httpmultibin/grpc_testing"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"go.k6.io/k6/metrics"
"google.golang.org/grpc/metadata"
grpcstats "google.golang.org/grpc/stats"
"google.golang.org/grpc/status"

"github.com/grafana/xk6-grpc/grpc/testdata/wrappers_testing"
"github.com/grafana/xk6-grpc/lib/netext/grpcext"
"go.k6.io/k6/metrics"
)

func TestClient(t *testing.T) {
Expand Down Expand Up @@ -680,6 +683,91 @@ func TestClient(t *testing.T) {
err: "no gRPC connection",
},
},
{
name: "Wrappers",
setup: func(hb *httpmultibin.HTTPMultiBin) {
srv := wrappers_testing.Register(hb.ServerGRPC)

srv.TestStringImplementation = func(_ context.Context, sv *wrappers.StringValue) (*wrappers.StringValue, error) {
return &wrapperspb.StringValue{
Value: "hey " + sv.Value,
}, nil
}
},
initString: codeBlock{
code: `
const client = new grpc.Client();
client.load([], "../grpc/testdata/wrappers_testing/test.proto");
`,
},
vuString: codeBlock{
code: `
client.connect("GRPCBIN_ADDR");
let respString = client.invoke("grpc.wrappers.testing.Service/TestString", "John")
if (respString.message !== "hey John") {
throw new Error("expected to get 'hey John', but got a " + respString.message)
}
`,
},
},
{
name: "WrappersWithReflection",
setup: func(hb *httpmultibin.HTTPMultiBin) {
reflection.Register(hb.ServerGRPC)

srv := wrappers_testing.Register(hb.ServerGRPC)

srv.TestIntegerImplementation = func(_ context.Context, iv *wrappers.Int64Value) (*wrappers.Int64Value, error) {
return &wrappers.Int64Value{
Value: 2 * iv.Value,
}, nil
}

srv.TestStringImplementation = func(_ context.Context, sv *wrappers.StringValue) (*wrappers.StringValue, error) {
return &wrapperspb.StringValue{
Value: "hey " + sv.Value,
}, nil
}

srv.TestBooleanImplementation = func(_ context.Context, bv *wrappers.BoolValue) (*wrappers.BoolValue, error) {
return &wrapperspb.BoolValue{
Value: bv.Value != true,
}, nil
}

srv.TestDoubleImplementation = func(_ context.Context, bv *wrappers.DoubleValue) (*wrappers.DoubleValue, error) {
return &wrapperspb.DoubleValue{
Value: bv.Value * 2,
}, nil
}
},
initString: codeBlock{
code: `
const client = new grpc.Client();
`,
},
vuString: codeBlock{
code: `
client.connect("GRPCBIN_ADDR", {reflect: true});
let respString = client.invoke("grpc.wrappers.testing.Service/TestString", "John")
if (respString.message !== "hey John") {
throw new Error("expected to get 'hey John', but got a " + respString.message)
}
let respInt = client.invoke("grpc.wrappers.testing.Service/TestInteger", "3")
if (respInt.message !== "6") {
throw new Error("expected to get '6', but got a " + respInt.message)
}
let respDouble = client.invoke("grpc.wrappers.testing.Service/TestDouble", "2.7")
if (respDouble.message !== 5.4) {
throw new Error("expected to get '5.4', but got a " + respDouble.message)
}
`,
},
},
}

for _, tt := range tests {
Expand Down
60 changes: 60 additions & 0 deletions grpc/testdata/wrappers_testing/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package wrappers_testing

import (
context "context"

wrappers "github.com/golang/protobuf/ptypes/wrappers"
grpc "google.golang.org/grpc"
)

//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative test.proto

// Register registers a test service that could be used for the testing gRPC wrappers
func Register(r grpc.ServiceRegistrar) *service {
s := &service{}

RegisterServiceServer(r, s)

return s
}

type service struct {
UnimplementedServiceServer

TestStringImplementation func(context.Context, *wrappers.StringValue) (*wrappers.StringValue, error)
TestIntegerImplementation func(context.Context, *wrappers.Int64Value) (*wrappers.Int64Value, error)
TestBooleanImplementation func(context.Context, *wrappers.BoolValue) (*wrappers.BoolValue, error)
TestDoubleImplementation func(context.Context, *wrappers.DoubleValue) (*wrappers.DoubleValue, error)
}

func (s *service) TestString(ctx context.Context, in *wrappers.StringValue) (*wrappers.StringValue, error) {
if s.TestStringImplementation != nil {
return s.TestStringImplementation(ctx, in)
}

return s.UnimplementedServiceServer.TestString(ctx, in)
}

func (s *service) TestInteger(ctx context.Context, in *wrappers.Int64Value) (*wrappers.Int64Value, error) {
if s.TestIntegerImplementation != nil {
return s.TestIntegerImplementation(ctx, in)
}

return s.UnimplementedServiceServer.TestInteger(ctx, in)
}

func (s *service) TestBoolean(ctx context.Context, in *wrappers.BoolValue) (*wrappers.BoolValue, error) {
if s.TestBooleanImplementation != nil {
return s.TestBooleanImplementation(ctx, in)
}

return s.UnimplementedServiceServer.TestBoolean(ctx, in)
}

func (s *service) TestDouble(ctx context.Context, in *wrappers.DoubleValue) (*wrappers.DoubleValue, error) {
if s.TestBooleanImplementation != nil {
return s.TestDoubleImplementation(ctx, in)
}

return s.UnimplementedServiceServer.TestDouble(ctx, in)
}
98 changes: 98 additions & 0 deletions grpc/testdata/wrappers_testing/test.pb.go

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

17 changes: 17 additions & 0 deletions grpc/testdata/wrappers_testing/test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

package grpc.wrappers.testing;

// this proto contains service that helps tests some of well-known types or wrappers
// https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/wrappers.proto

import "google/protobuf/wrappers.proto";

option go_package ="./wrappers_testing";

service Service {
rpc TestString(google.protobuf.StringValue) returns (google.protobuf.StringValue);
rpc TestInteger(google.protobuf.Int64Value) returns (google.protobuf.Int64Value);
rpc TestBoolean(google.protobuf.BoolValue) returns (google.protobuf.BoolValue);
rpc TestDouble(google.protobuf.DoubleValue) returns (google.protobuf.DoubleValue);
}
Loading

0 comments on commit 05a4176

Please sign in to comment.