-
Notifications
You must be signed in to change notification settings - Fork 4.5k
grpc_test: add tests for cardinality violation #8120
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #8120 +/- ##
==========================================
- Coverage 82.29% 82.19% -0.10%
==========================================
Files 387 417 +30
Lines 38967 41344 +2377
==========================================
+ Hits 32066 33983 +1917
- Misses 5586 5939 +353
- Partials 1315 1422 +107 🚀 New features to boost your workflow:
|
test/end2end_test.go
Outdated
earlyNil bool // whether to return nil without calling SendAndClose(). | ||
recvAfterClose bool // whether to call Recv() after calling SendAndClose(). |
There was a problem hiding this comment.
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:
Line 4485 in d48317f
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!
There was a problem hiding this 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.
This looks like the test I have written already , the |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like I had not published the earlier comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@dfawley for second review |
test/end2end_test.go
Outdated
// Tests to verify that client receive a cardinality violation error for | ||
// client-streaming RPCs if the server doesn't send a message before returning | ||
// status OK. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Tests to verify that client receive a cardinality violation error for | |
// client-streaming RPCs if the server doesn't send a message before returning | |
// status OK. | |
// Tests that a client receives a cardinality violation error for | |
// client-streaming RPCs if the server doesn't send a message before returning | |
// status OK. |
(Please rewrap)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/end2end_test.go
Outdated
}) | ||
_, err := stream.Recv() | ||
if err == nil { | ||
logger.Fatalf("stream.Recv() = %v, want an error", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You want t.Errorf
not logger.Fatalf
here. logger.Fatalf
kills the whole process. t.Fatalf
kills the current goroutine. This won't run in the main test goroutine, so t.Fatalf
would just cause problems, not terminate the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right!
test/end2end_test.go
Outdated
}) | ||
_, err := stream.Recv() | ||
if err == nil { | ||
logger.Fatalf("stream.Recv() = %v, want an error", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: "= %v", err
-- err
is nil
. Just put nil
in the string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/end2end_test.go
Outdated
// Test to verify for client-streaming RPCs, when SendAndClose is called, the server | ||
// should reset stream after sending the response message successfully. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't appear to be validating RST_STREAM is written to the connection. You'll need to use the serverTester
for that.
test/end2end_test.go
Outdated
if status.Code(err) != codes.OK { | ||
t.Fatalf("stream.CloseAndRecv() = %v, want error %s", err, codes.OK) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
status.Code(nil)
is always codes.OK
so this doesn't do anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
test/end2end_test.go
Outdated
// TODO : https://github.com/grpc/grpc-go/issues/8119 - remove `t.Skip()` | ||
// after this is fixed. | ||
t.Skip() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the test description above...why does this fail today? I don't think this should be a problem -- I was expecting that it's impossible for the server to do anything else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure what you mean , because if I send an error code , example Unimplemented
, according to the test , it is being reflected in the client when CloseAndRecv()
is called by client.
Also in the the code, SendAndCLose()
doesnt look like its doing anything to close , just calling SendMsg()
. It also says to return nil after calling SendAndCLose()
or return an error ,look here so I would think that returning an error after SendAndClose()
would surely propogate to client and this is the intended behavior and this test is not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. As mentioned offline, I'm worried that we won't be able to ever change the behavior of SendAndClose
to end the RPC, even though that is what it claims to do. So we should instead confirm that SendAndClose
doesn't send the TRAILERS on success. And that returning an error from the handler after SendAndClose
results in a TRAILERS frame being sent that contains the error.
How the client handles that may depend on the type of stream from the client's view. E.g. if the client is using it as a bidi streaming RPC, then it should see the error trailers. But if it's a client streaming call, then it might get some other kind of error, since a message and an error is not expected for client streaming RPCs.
test/end2end_test.go
Outdated
sum := 0 | ||
in, _ := stream.Recv() | ||
p := in.GetPayload().GetBody() | ||
sum += len(p) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sum := len(p)
? But why is it a sum
? Probably this was left over from an old iteration. You can just delete it and return len(p)
directly in the response message, too. Or, better, just return 0 in the response. The value is unimportant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/end2end_test.go
Outdated
payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 1) | ||
if err != nil { | ||
t.Fatal(err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems easier to just delete this. Send empty requests instead...
Less code is usually better code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/end2end_test.go
Outdated
select { | ||
case <-ctx.Done(): | ||
t.Fatal("timed out waiting for error from server") | ||
default: | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be replaced by doing:
for ctx.Err() == nil {
if err := stream.Send()....
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/end2end_test.go
Outdated
} | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you use an else if
here, then you can use err :=
in the if
. That's more natural unless it's important to keep err
beyond the loop -- but it isn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks! Just a bunch of little things.
t.Skip() | ||
ss := &stubserver.StubServer{ | ||
StreamingInputCallF: func(_ testgrpc.TestService_StreamingInputCallServer) error { | ||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be comments here and there explaining why the test is doing what it is. Like "// Immediately return a success without first sending a response message, which is illegal."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/end2end_test.go
Outdated
if resp, err := stream.Recv(); err != nil || resp == nil { | ||
t.Fatalf("stream.Recv() = %v, %v, want non-nil empty response, <nil>", resp, err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you assert that the response matches some expectation, not just "not nil"? I.e. proto.Equal(&testpb.StreamingInputCallRequest{})
?
test/end2end_test.go
Outdated
if err := stream.Send(&testpb.StreamingInputCallRequest{}); err != nil { | ||
t.Fatalf("stream.Send(_) = %v, want <nil>", err) | ||
} | ||
if resp, err := stream.CloseAndRecv(); err != nil || resp == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar here.
test/end2end_test.go
Outdated
// Tests the behavior where Recv() can be called after SendAndClose(). Although | ||
// this is unexpected, we retain it for backward compatibility. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test description shouldn't simply say "tests this scenario", it should say "tests that in this scenario this thing happens". Here it should say something like "Tests that the server can continue to receive messages after calling SendAndClose".
test/end2end_test.go
Outdated
ss := stubserver.StubServer{ | ||
StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error { | ||
if err := stream.SendAndClose(&testpb.StreamingInputCallResponse{}); err != nil { | ||
t.Fatalf("stream.SendAndClose(_) = %v, want <nil>", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be Errorf
since it's not in the main test goroutine. Otherwise it just kills the current goroutine, and keeps the test running. (I see the same thing above.)
test/end2end_test.go
Outdated
if err != nil { | ||
t.Fatalf(".StreamingInputCall(_) = _, %v, want <nil>", err) | ||
} | ||
if resp, err := stream.CloseAndRecv(); status.Code(err) != codes.Internal || resp != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also check status.Message(err) == status.Message(wantError)
. And it would be better to use status.Code(wantError)
instead of repeating the code in it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks! Just a bunch of little things.
Fixes: #7570
Added the following tests for client streaming RPC :
internal
error is returned for client-streaming RPCs if the server doesn't send a message before returning status OK. This is a cardinality violation. Skipping this test for now till the issues are fixed: Cardinaltiy violation in stream interface implementation #8119RELEASE NOTES: N/A