diff --git a/runner/data.go b/runner/data.go index 365b2230..4305611c 100644 --- a/runner/data.go +++ b/runner/data.go @@ -117,3 +117,13 @@ func createPayloadsFromBinCountDelimited(binData []byte, mtd *desc.MethodDescrip return &inputs, nil } + +func createPayloadsFromBin(binData []byte, mtd *desc.MethodDescriptor) (*[]*dynamic.Message, error) { + inputs, err := createPayloadsFromBinCountDelimited(binData, mtd) + + if err == nil && len(*inputs) > 0 { + return inputs, err + } + + return createPayloadsFromBinSingleMessage(binData, mtd) +} \ No newline at end of file diff --git a/runner/data_test.go b/runner/data_test.go index 7bc5eee8..913be363 100644 --- a/runner/data_test.go +++ b/runner/data_test.go @@ -209,7 +209,7 @@ func TestData_createPayloads(t *testing.T) { binData, err := proto.Marshal(msg1) - inputs, err := createPayloadsFromBinSingleMessage(binData, mtdUnary) + inputs, err := createPayloadsFromBin(binData, mtdUnary) assert.NoError(t, err) assert.NotNil(t, inputs) @@ -227,7 +227,7 @@ func TestData_createPayloads(t *testing.T) { _ = buf.EncodeMessage(msg1) _ = buf.EncodeMessage(msg2) - inputs, err := createPayloadsFromBinCountDelimited(buf.Bytes(), mtdUnary) + inputs, err := createPayloadsFromBin(buf.Bytes(), mtdUnary) assert.NoError(t, err) assert.NotNil(t, inputs) @@ -235,4 +235,13 @@ func TestData_createPayloads(t *testing.T) { assert.EqualValues(t, msg1.GetName(), (*inputs)[0].GetFieldByName("name")) assert.EqualValues(t, msg2.GetName(), (*inputs)[1].GetFieldByName("name")) }) + + t.Run("on empty binary data returns empty slice", func(t *testing.T) { + buf := make([]byte, 0) + inputs, err := createPayloadsFromBin(buf, mtdUnary) + + assert.NoError(t, err) + assert.NotNil(t, inputs) + assert.Len(t, *inputs, 0) + }) } diff --git a/runner/run_test.go b/runner/run_test.go index ff765da4..9c3cfe25 100644 --- a/runner/run_test.go +++ b/runner/run_test.go @@ -350,49 +350,48 @@ func TestRunUnary(t *testing.T) { assert.Equal(t, []string {"0", "1", "2", "0", "1", "2"}, names) }) - // todo fix this test - //t.Run("test round-robin binary", func(t *testing.T) { - // gs.ResetCounters() - // - // buf := proto.Buffer{} - // for i := 0; i < 3; i++ { - // msg := &helloworld.HelloRequest{} - // msg.Name = strconv.Itoa(i) - // err = buf.EncodeMessage(msg) - // assert.NoError(t, err) - // } - // binData := buf.Bytes() - // - // report, err := Run( - // "helloworld.Greeter.SayHello", - // internal.TestLocalhost, - // WithProtoFile("../testdata/greeter.proto", []string{}), - // WithTotalRequests(6), - // WithConcurrency(1), - // WithTimeout(time.Duration(20*time.Second)), - // WithDialTimeout(time.Duration(20*time.Second)), - // WithInsecure(true), - // WithBinaryDataCountDelimited(binData), - // ) - // - // assert.NoError(t, err) - // assert.NotNil(t, report) - // - // count := gs.GetCount(callType) - // assert.Equal(t, 6, count) - // - // calls := gs.GetCalls(callType) - // assert.NotNil(t, calls) - // assert.Len(t, calls, 6) - // names := make([]string, 0) - // for _, msgs := range calls { - // for _, msg := range msgs { - // names = append(names, msg.GetName()) - // } - // } - // - // assert.Equal(t, []string {"0", "1", "2", "0", "1", "2"}, names) - //}) + t.Run("test round-robin binary", func(t *testing.T) { + gs.ResetCounters() + + buf := proto.Buffer{} + for i := 0; i < 3; i++ { + msg := &helloworld.HelloRequest{} + msg.Name = strconv.Itoa(i) + err = buf.EncodeMessage(msg) + assert.NoError(t, err) + } + binData := buf.Bytes() + + report, err := Run( + "helloworld.Greeter.SayHello", + internal.TestLocalhost, + WithProtoFile("../testdata/greeter.proto", []string{}), + WithTotalRequests(6), + WithConcurrency(1), + WithTimeout(time.Duration(20*time.Second)), + WithDialTimeout(time.Duration(20*time.Second)), + WithInsecure(true), + WithBinaryData(binData), + ) + + assert.NoError(t, err) + assert.NotNil(t, report) + + count := gs.GetCount(callType) + assert.Equal(t, 6, count) + + calls := gs.GetCalls(callType) + assert.NotNil(t, calls) + assert.Len(t, calls, 6) + names := make([]string, 0) + for _, msgs := range calls { + for _, msg := range msgs { + names = append(names, msg.GetName()) + } + } + + assert.Equal(t, []string {"0", "1", "2", "0", "1", "2"}, names) + }) } func TestRunServerStreaming(t *testing.T) { diff --git a/runner/worker.go b/runner/worker.go index 642809c4..12d969fe 100644 --- a/runner/worker.go +++ b/runner/worker.go @@ -71,8 +71,7 @@ func (w *Worker) makeRequest() error { } } else { var err error - // todo we need an explicit way to choose between binary formats, it's impossible to distinguish from data itself - inputs, err = createPayloadsFromBinSingleMessage(w.config.data, w.mtd) + inputs, err = createPayloadsFromBin(w.config.data, w.mtd) if err != nil { return err }