Skip to content

Commit

Permalink
Fixes undefined handler passing in gRPC module
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Jun 7, 2024
1 parent c5f57cc commit 80ceb35
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
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")
}

0 comments on commit 80ceb35

Please sign in to comment.