From 82a83313db78563e32aadfb457198c512749d3f3 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Fri, 17 Apr 2015 13:30:51 -0700 Subject: [PATCH 1/4] Added test case demonstrating cancel on bidi stream --- test/cpp/end2end/end2end_test.cc | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index f96051cafac..9d61428d245 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -491,6 +491,41 @@ TEST_F(End2endTest, ServerCancelsRpc) { EXPECT_TRUE(s.details().empty()); } +// Client cancels bidi stream after sending some messages +TEST_F(End2endTest, ClientCancelsBidi) { + ResetStub(); + EchoRequest request; + EchoResponse response; + ClientContext context; + grpc::string msg("hello"); + + auto stream = stub_->BidiStream(&context); + + request.set_message(msg + "0"); + EXPECT_TRUE(stream->Write(request)); + EXPECT_TRUE(stream->Read(&response)); + EXPECT_EQ(response.message(), request.message()); + + request.set_message(msg + "1"); + EXPECT_TRUE(stream->Write(request)); + + context.TryCancel(); + + // The cancellation races with responses, so there might be zero or + // one responses pending, read till failure + + if (stream->Read(&response)) { + EXPECT_EQ(response.message(), request.message()); + // Since we have cancelled, we expect the next attempt to read to fail + EXPECT_FALSE(stream->Read(&response)); + } + + Status s = stream->Finish(); + EXPECT_EQ(grpc::StatusCode::CANCELLED, s.code()); +} + + + } // namespace testing } // namespace grpc From e41d0402ba727d5f89bdf39e90e406227cc23601 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Fri, 17 Apr 2015 14:12:33 -0700 Subject: [PATCH 2/4] Added end2end test for server streaming rpc cancellation. --- test/cpp/end2end/end2end_test.cc | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index 9d61428d245..3193a2af58c 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -491,6 +491,36 @@ TEST_F(End2endTest, ServerCancelsRpc) { EXPECT_TRUE(s.details().empty()); } +// Client cancels server stream after sending some messages + TEST_F(End2endTest, ClientCancelsResponseStream) { + ResetStub(); + EchoRequest request; + EchoResponse response; + ClientContext context; + request.set_message("hello"); + + auto stream = stub_->ResponseStream(&context, request); + + EXPECT_TRUE(stream->Read(&response)); + EXPECT_EQ(response.message(), request.message() + "0"); + EXPECT_TRUE(stream->Read(&response)); + EXPECT_EQ(response.message(), request.message() + "1"); + + context.TryCancel(); + + // The cancellation races with responses, so there might be zero or + // one responses pending, read till failure + + if (stream->Read(&response)) { + EXPECT_EQ(response.message(), request.message() + "2"); + // Since we have cancelled, we expect the next attempt to read to fail + EXPECT_FALSE(stream->Read(&response)); + } + + Status s = stream->Finish(); + EXPECT_EQ(grpc::StatusCode::CANCELLED, s.code()); +} + // Client cancels bidi stream after sending some messages TEST_F(End2endTest, ClientCancelsBidi) { ResetStub(); @@ -525,7 +555,6 @@ TEST_F(End2endTest, ClientCancelsBidi) { } - } // namespace testing } // namespace grpc From 18298a74401c8ba87bd60cb8aa609eb380a84a1f Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Fri, 17 Apr 2015 15:00:25 -0700 Subject: [PATCH 3/4] Fixed flakes due to OK racing with cancel --- test/cpp/end2end/end2end_test.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index 3193a2af58c..505984008ae 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -518,7 +518,9 @@ TEST_F(End2endTest, ServerCancelsRpc) { } Status s = stream->Finish(); - EXPECT_EQ(grpc::StatusCode::CANCELLED, s.code()); + // The final status could be either of CANCELLED or OK depending on + // who won the race. + EXPECT_GE(grpc::StatusCode::CANCELLED, s.code()); } // Client cancels bidi stream after sending some messages From a1d3a72930ea00c8f6cc9fcb3468bf2325c44afa Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Thu, 23 Apr 2015 10:24:04 -0700 Subject: [PATCH 4/4] fixed indentation --- test/cpp/end2end/end2end_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index 505984008ae..684357d60bb 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -492,7 +492,7 @@ TEST_F(End2endTest, ServerCancelsRpc) { } // Client cancels server stream after sending some messages - TEST_F(End2endTest, ClientCancelsResponseStream) { +TEST_F(End2endTest, ClientCancelsResponseStream) { ResetStub(); EchoRequest request; EchoResponse response;