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

Fixes undefined handler passing to the gRPC stream #3779

Merged
merged 1 commit into from
Jun 7, 2024
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
10 changes: 7 additions & 3 deletions js/modules/k6/grpc/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,13 @@ func (s *stream) processSendError(err error) {
})
}

// on registers a listener for a certain event type
func (s *stream) on(event string, listener func(sobek.Value) (sobek.Value, error)) {
if err := s.eventListeners.add(event, listener); err != nil {
// on registers a handler for a certain event type
func (s *stream) on(event string, handler func(sobek.Value) (sobek.Value, error)) {
if handler == nil {
common.Throw(s.vu.Runtime(), fmt.Errorf("handler for %q event isn't a callable function", event))
}

if err := s.eventListeners.add(event, handler); err != nil {
s.vu.State().Logger.Warnf("can't register %s event handler: %s", event, err)
}
}
Expand Down
43 changes: 43 additions & 0 deletions js/modules/k6/grpc/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/grafana/sobek"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -363,3 +364,45 @@ func TestStream_Wrappers(t *testing.T) {
},
)
}

func TestStream_UndefinedHandler(t *testing.T) {
t.Parallel()

ts := newTestState(t)

stub := grpc_wrappers_testing.Register(ts.httpBin.ServerGRPC)
stub.TestStreamImplementation = func(stream grpc_wrappers_testing.Service_TestStreamServer) error {
return stream.SendAndClose(&wrappers.StringValue{
Value: "test",
})
}

replace := func(code string) (sobek.Value, error) {
return ts.VU.Runtime().RunString(ts.httpBin.Replacer.Replace(code))
}

initString := codeBlock{
code: `
var client = new grpc.Client();
client.load([], "../../../../lib/testutils/httpmultibin/grpc_wrappers_testing/test.proto");`,
}
vuString := codeBlock{
code: `
client.connect("GRPCBIN_ADDR");
let stream = new grpc.Stream(client, "grpc.wrappers.testing.Service/TestStream");
stream.on('data', undefined);

stream.end();
`,
}

val, err := replace(initString.code)
assertResponse(t, initString, err, val, ts)

ts.ToVUContext()

_, err = replace(vuString.code)
ts.EventLoop.WaitOnRegistered()

require.ErrorContains(t, err, "handler for \"data\" event isn't a callable function")
}