Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Dedicated unit test
Browse files Browse the repository at this point in the history
This adds a dedicated unit test for checking that all responses from the
server were received even if client declared that sending is finished
(client.end) called.
  • Loading branch information
olegbespalov committed Sep 11, 2023
1 parent 4ab5962 commit afff315
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions grpc/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grpc
import (
"context"
"testing"
"time"

"github.com/dop251/goja"
"github.com/grafana/xk6-grpc/grpc/testutils/grpcservice"
Expand Down Expand Up @@ -188,6 +189,101 @@ func TestStream_ErrorHandling(t *testing.T) {
)
}

// this test case is checking that everything that server sends
// after the client finished (client.end called) is delivered to the client
// and the end event is called
func TestStream_ReceiveAllServerResponsesAfterEnd(t *testing.T) {
t.Parallel()

ts := newTestState(t)

stub := &FeatureExplorerStub{}

savedFeatures := []*grpcservice.Feature{
{
Name: "foo",
Location: &grpcservice.Point{
Latitude: 1,
Longitude: 2,
},
},
{
Name: "bar",
Location: &grpcservice.Point{
Latitude: 3,
Longitude: 4,
},
},
}

stub.listFeatures = func(rect *grpcservice.Rectangle, stream grpcservice.FeatureExplorer_ListFeaturesServer) error {
for _, feature := range savedFeatures {
// adding a delay to make server response "slower"
time.Sleep(200 * time.Millisecond)

if err := stream.Send(feature); err != nil {
return err
}
}

return nil
}

grpcservice.RegisterFeatureExplorerServer(ts.httpBin.ServerGRPC, stub)

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

initString := codeBlock{
code: `
var client = new grpc.Client();
client.load([], "../grpc/testutils/grpcservice/route_guide.proto");`,
}
vuString := codeBlock{
code: `
client.connect("GRPCBIN_ADDR");
let stream = new grpc.Stream(client, "main.FeatureExplorer/ListFeatures")
stream.on('data', function (data) {
call('Feature:' + data.name);
});
stream.on('end', function () {
call('End called');
});
stream.write({
lo: {
latitude: 1,
longitude: 2,
},
hi: {
latitude: 1,
longitude: 2,
},
});
stream.end();
`,
}

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

ts.ToVUContext()

val, err = replace(vuString.code)

ts.EventLoop.WaitOnRegistered()

assertResponse(t, vuString, err, val, ts)

assert.Equal(t, ts.callRecorder.Recorded(), []string{
"Feature:foo",
"Feature:bar",
"End called",
},
)
}

// FeatureExplorerStub is a stub for FeatureExplorerServer
// it has ability to override methods
type FeatureExplorerStub struct {
Expand Down

0 comments on commit afff315

Please sign in to comment.