Skip to content

Commit

Permalink
Google wrappers support test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Sep 20, 2023
1 parent aabd398 commit e8a756b
Show file tree
Hide file tree
Showing 8 changed files with 547 additions and 0 deletions.
88 changes: 88 additions & 0 deletions js/modules/k6/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
"google.golang.org/grpc/reflection"
"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"
"github.com/stretchr/testify/require"
Expand All @@ -38,6 +40,7 @@ import (
"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"
"go.k6.io/k6/lib/testutils/httpmultibin/grpc_wrappers_testing"
"go.k6.io/k6/metrics"
)

Expand Down Expand Up @@ -842,6 +845,91 @@ func TestClient(t *testing.T) {
err: "no gRPC connection",
},
},
{
name: "Wrappers",
setup: func(hb *httpmultibin.HTTPMultiBin) {
srv := grpc_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([], "../../../../lib/testutils/httpmultibin/grpc_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 := grpc_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)
}
`,
},
},
}

assertResponse := func(t *testing.T, cb codeBlock, err error, val goja.Value, ts testState) {
Expand Down
4 changes: 4 additions & 0 deletions lib/testutils/httpmultibin/grpc_wrappers_testing/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Package grpc_wrappers_testing contains the test service that helps to test gRPC wrappers.
package grpc_wrappers_testing //nolint:revive,stylecheck

//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative test.proto
58 changes: 58 additions & 0 deletions lib/testutils/httpmultibin/grpc_wrappers_testing/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package grpc_wrappers_testing

Check warning on line 1 in lib/testutils/httpmultibin/grpc_wrappers_testing/service.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use an underscore in package name (revive)

import (
"context"

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

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

Check warning on line 11 in lib/testutils/httpmultibin/grpc_wrappers_testing/service.go

View workflow job for this annotation

GitHub Actions / lint

unexported-return: exported func Register returns unexported type *grpc_wrappers_testing.service, which can be annoying to use (revive)
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.TestDoubleImplementation != nil {
return s.TestDoubleImplementation(ctx, in)
}

return s.UnimplementedServiceServer.TestDouble(ctx, in)
}
98 changes: 98 additions & 0 deletions lib/testutils/httpmultibin/grpc_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 lib/testutils/httpmultibin/grpc_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 ="./grpc_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 e8a756b

Please sign in to comment.