Skip to content

Commit 6477252

Browse files
test: Fix flaky test TestServerStreaming_ClientCallSendMsgTwice in end2end_test.go. (grpc#9004)
Fixes: grpc#8993 This PR will fix the flaky test `TestServerStreaming_ClientCallSendMsgTwice` introduced in PR grpc#8385 . The test expected a single call to `stream.SendMsg()` to return a `codes.Canceled` error immediately after the stream's context was marked as done. Solution: Introduced a retry loop with a timeout for the `stream.SendMsg()` call. Instead of testing a single invocation, the server handler now continuously attempts to send a message until it either receives an error or timeout expired. Successfully run the test on forge for 1 million times without any flake. RELEASE NOTES: N/A
1 parent 2eade05 commit 6477252

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

test/end2end_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3960,7 +3960,18 @@ func (s) TestServerStreaming_ClientCallSendMsgTwice(t *testing.T) {
39603960
// Block until the stream’s context is done. Second call to client.SendMsg
39613961
// triggers a RST_STREAM which cancels the stream context on the server.
39623962
<-stream.Context().Done()
3963-
if err := stream.SendMsg(&testpb.StreamingOutputCallRequest{}); status.Code(err) != codes.Canceled {
3963+
var err error
3964+
waitCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
3965+
defer cancel()
3966+
for ; waitCtx.Err() == nil; <-time.After(time.Millisecond) {
3967+
if err = stream.SendMsg(&testpb.StreamingOutputCallRequest{}); err != nil {
3968+
break
3969+
}
3970+
}
3971+
if waitCtx.Err() != nil {
3972+
t.Fatalf("Context timed out waiting for stream.SendMsg() to return an error")
3973+
}
3974+
if status.Code(err) != codes.Canceled {
39643975
t.Errorf("stream.SendMsg() = %v, want error %v", err, codes.Canceled)
39653976
}
39663977
close(handlerDone)

0 commit comments

Comments
 (0)