Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix grpc any.proto marshal error #2523

Merged
merged 8 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions js/modules/k6/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/dynamicpb"
)

// Client represents a gRPC client that can be used to make RPC requests
Expand Down Expand Up @@ -257,8 +259,22 @@ func (c *Client) convertToMethodInfo(fdset *descriptorpb.FileDescriptorSet) ([]M
appendMethodInfo(fd, sd, md)
}
}
messages := fd.Messages()
for i := 0; i < messages.Len(); i++ {
message := messages.Get(i)
_, errFind := protoregistry.GlobalTypes.FindMessageByName(message.FullName())
if errors.Is(errFind, protoregistry.NotFound) {
err = protoregistry.GlobalTypes.RegisterMessage(dynamicpb.NewMessageType(message))
if err != nil {
return false
}
}
}
return true
})
if err != nil {
return nil, err
}
return rtn, nil
}

Expand Down
50 changes: 50 additions & 0 deletions js/modules/k6/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"google.golang.org/grpc/reflection"

"github.com/dop251/goja"
"github.com/golang/protobuf/ptypes/any"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
Expand All @@ -34,6 +35,7 @@ import (
"go.k6.io/k6/lib/netext/grpcext"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/testutils/httpmultibin"
grpcanytesting "go.k6.io/k6/lib/testutils/httpmultibin/grpc_any_testing"
"go.k6.io/k6/metrics"
)

Expand Down Expand Up @@ -372,6 +374,54 @@ func TestClient(t *testing.T) {
},
},
},
{
name: "InvokeAnyProto",
initString: codeBlock{code: `
var client = new grpc.Client();
client.load([], "../../../../lib/testutils/httpmultibin/grpc_any_testing/any_test.proto");`},
setup: func(tb *httpmultibin.HTTPMultiBin) {
tb.GRPCAnyStub.SumFunc = func(ctx context.Context, req *grpcanytesting.SumRequest) (*grpcanytesting.SumReply, error) {
var sumRequestData grpcanytesting.SumRequestData
if err := req.Data.UnmarshalTo(&sumRequestData); err != nil {
return nil, err
}

sumReplyData := &grpcanytesting.SumReplyData{
V: sumRequestData.A + sumRequestData.B,
Err: "",
}
sumReply := &grpcanytesting.SumReply{
Data: &any.Any{},
}
if err := sumReply.Data.MarshalFrom(sumReplyData); err != nil {
return nil, err
}

return sumReply, nil
}
},
vuString: codeBlock{
code: `
client.connect("GRPCBIN_ADDR");
var resp = client.invoke("grpc.any.testing.AnyTestService/Sum", {
data: {
"@type": "type.googleapis.com/grpc.any.testing.SumRequestData",
"a": 1,
"b": 2,
},
})
if (resp.status !== grpc.StatusOK) {
throw new Error("unexpected error: " + JSON.stringify(resp.error) + "or status: " + resp.status)
}
if (resp.message.data.v !== "3") {
throw new Error("unexpected resp message data")
}`,
asserts: func(t *testing.T, rb *httpmultibin.HTTPMultiBin, samples chan metrics.SampleContainer, _ error) {
samplesBuf := metrics.GetBufferedSamples(samples)
assertMetricEmitted(t, metrics.GRPCReqDurationName, samplesBuf, rb.Replacer.Replace("GRPCBIN_ADDR/grpc.any.testing.AnyTestService/Sum"))
},
},
},
{
name: "RequestMessage",
initString: codeBlock{
Expand Down
Loading