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

grpc_test: add tests for cardinality violation #8120

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

eshitachandwani
Copy link
Member

Fixes: #7570

Added tests for cardinality violations :

  1. Client should ensure a status internal error is returned for client-streaming RPCs if the server doesn't send a message before returning status OK.
  2. Also for client-streaming RPCs, when SendAndClose is called, the server should perform a RST_STREAM() after sending the response message successfully.

Skipping the tests for now till the issues are fixed: #8119
RELEASE NOTES: N/A

Copy link

codecov bot commented Feb 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.34%. Comparing base (ae2a04f) to head (ad7ff22).
Report is 21 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8120      +/-   ##
==========================================
+ Coverage   82.29%   82.34%   +0.05%     
==========================================
  Files         387      389       +2     
  Lines       38967    39103     +136     
==========================================
+ Hits        32066    32201     +135     
- Misses       5586     5588       +2     
+ Partials     1315     1314       -1     

see 50 files with indirect coverage changes

Comment on lines 148 to 149
earlyNil bool // whether to return nil without calling SendAndClose().
recvAfterClose bool // whether to call Recv() after calling SendAndClose().
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really really don't want to extend testServer. Could you use the stubserver package instead? testServer has a lot of bad properties (especially: it splits the testing code into two different places: the test and the testServer), and this only complicates it further by adding more ways to conditionalize its behavior.

You can follow this kind of example to use stubserver instead:

ss := &stubserver.StubServer{

Also please add a comment on testServer that it should not be used for any new tests so that this doesn't happen again.

Sorry!

@dfawley dfawley assigned eshitachandwani and unassigned dfawley Feb 27, 2025
Copy link
Contributor

@purnesh42H purnesh42H left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we need one more test case when "Server Doesn't RST_STREAM"

Server-Side:

  • Create a client-streaming RPC handler.
  • The handler calls SendAndClose with a valid response message.
  • The handler then attempts to call Recv.

Client-Side:

  • Make a client-streaming RPC call.
  • Continuously send messages.

Expected Outcome:

  • The server's Recv call should return an error indicating that the stream is closed.
  • The client's Send calls should eventually return an error indicating that the stream has been reset.

@@ -3585,6 +3586,90 @@ func testClientStreamingError(t *testing.T, e env) {
}
}

func (s) TestClientStreamingMissingSendAndCloseError(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add docstring for this explaining what its verifying i.e. what is the expected behavior

testClientStreamingRecvAfterCloseError(t)
}

func testClientStreamingRecvAfterCloseError(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add docstring for this explaining what its verifying i.e. what is the expected behavior

// TODO : https://github.com/grpc/grpc-go/issues/8119 - remove `t.Skip()`
// after this is fixed.
t.Skip()
testClientStreamingMissingSendAndCloseError(t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are moving the test logic to helper? Can't we just write within this test?

t.Fatalf(".StreamingInputCall(_) = _, %v, want <nil>", err)
}

if _, err := stream.CloseAndRecv(); status.Code(err) != codes.Internal {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Client streaming RPCs must terminate with either no message and a non-OK status, or a message and an OK status.

Based on #7570, since we are not sending message from handler, i think we need to verify if we are getting the error and the status is not-ok?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in this case, we should receive Error log messages indicating what happened and that it is illegal behavior.

how do we verify this? I think there are some existing tests which parse the error logs and verify

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it might be good to have separate if condition for both error and status violation. Although they are always supposed to happen in conjuction but checking them separately make it easier to understand.

if _, err := stream.CloseAndRecv(); err == nil {
      t.Fatalf("CloseAndRecv should have returned an error")
}

if  status.Code(err) == status.ok {
    t.Fatalf("CloseAndRecv should have returned a non-ok")
}

@@ -3585,6 +3586,90 @@ func testClientStreamingError(t *testing.T, e env) {
}
}

func (s) TestClientStreamingMissingSendAndCloseError(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion for name to be more explicit TestClientStreamingCardinalityViolation_ServerHandlerMissingSendAndClose

}
}

func (s) TestClientStreamingRecvAfterCloseError(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion for name to be more explicit TestClientStreamingCardinalityViolation_IgnoreServerHandlerErrorAfterSendAndClose

stream.SendAndClose(&testpb.StreamingInputCallResponse{
AggregatedPayloadSize: int32(sum),
})
_, err := stream.Recv()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: do we need to do this? What if just return a static error with status code like status.Error(codes.Internal, "something went wrong")). ? Because calling stream.Recv() adding one more possibility to the test case

if err = stream.Send(req); err == nil {
continue
}
if _, err := stream.CloseAndRecv(); status.Code(err) != codes.Internal {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We should probably just break out of loop when server closes the stream and call the CloseAndRecv() outside

continue
}
if _, err := stream.CloseAndRecv(); status.Code(err) != codes.Internal {
t.Fatalf("%v.CloseAndRecv() = %v, want error %s", stream, err, codes.Internal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment here as above, probably good to write 2 separate ifs but the opposite way in this case. 1) raise t.Fatalf if error is not nil. 2) raise t.Fatalf if status is not ok

@purnesh42H purnesh42H removed their assignment Mar 4, 2025
@eshitachandwani
Copy link
Member Author

Looks like we need one more test case when "Server Doesn't RST_STREAM"

Server-Side:

  • Create a client-streaming RPC handler.
  • The handler calls SendAndClose with a valid response message.
  • The handler then attempts to call Recv.

Client-Side:

  • Make a client-streaming RPC call.
  • Continuously send messages.

Expected Outcome:

  • The server's Recv call should return an error indicating that the stream is closed.
  • The client's Send calls should eventually return an error indicating that the stream has been reset.

This looks like the test I have written already , the TestClientStreamingRecvAfterCloseError test , can you tell me what is the difference between the two?

@purnesh42H
Copy link
Contributor

Looks like we need one more test case when "Server Doesn't RST_STREAM"
Server-Side:

  • Create a client-streaming RPC handler.
  • The handler calls SendAndClose with a valid response message.
  • The handler then attempts to call Recv.

Client-Side:

  • Make a client-streaming RPC call.
  • Continuously send messages.

Expected Outcome:

  • The server's Recv call should return an error indicating that the stream is closed.
  • The client's Send calls should eventually return an error indicating that the stream has been reset.

This looks like the test I have written already , the TestClientStreamingRecvAfterCloseError test , can you tell me what is the difference between the two?

yeah looks close. I think the for loop is not clear. We should do t.LogF when receiving error from client.Send indicating rst_stream. Is it possible to verify if rst stream was received in trailers. If its too complicated we can skip but let's see if we can. Other thing is the for loop shouldn't be indefinite. We should also do t.Fatalf if context is done. That way its clear that we are expecting an error for this test.

Having said that, then I think then we need a separate test case where client ignores the error after SendAndClose. It will slightly shorter where the handler return a static error and client just ignores it and status is ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants