xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/client_streaming.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2020 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <memory>
20 
21 #include "gtest/gtest.h"
22 
23 #include <grpc/status.h>
24 
25 #include "src/core/lib/gprpp/time.h"
26 #include "test/core/end2end/end2end_tests.h"
27 
28 namespace grpc_core {
29 namespace {
30 
31 // Client streaming test where the client sends a bunch of messages and the
32 // server reads them. After reading some messages, the server sends the status.
33 // Client writes fail after that due to the end of stream and the client
34 // subsequently requests and receives the status.
ClientStreaming(CoreEnd2endTest & test,int messages)35 void ClientStreaming(CoreEnd2endTest& test, int messages) {
36   auto c = test.NewClientCall("/foo").Timeout(Duration::Seconds(30)).Create();
37 
38   CoreEnd2endTest::IncomingMetadata server_initial_metadata;
39   c.NewBatch(1).SendInitialMetadata({}).RecvInitialMetadata(
40       server_initial_metadata);
41   auto s = test.RequestCall(100);
42   test.Expect(100, true);
43   test.Step();
44   s.NewBatch(101).SendInitialMetadata({});
45   test.Expect(101, true);
46   test.Expect(1, true);
47   test.Step();
48   // Client writes bunch of messages and server reads them
49   for (int i = 0; i < messages; i++) {
50     c.NewBatch(2).SendMessage("hello world");
51     CoreEnd2endTest::IncomingMessage client_message;
52     s.NewBatch(102).RecvMessage(client_message);
53     test.Expect(2, true);
54     test.Expect(102, true);
55     test.Step();
56     EXPECT_EQ(client_message.payload(), "hello world");
57   }
58 
59   // Server sends status denoting end of stream
60   s.NewBatch(103).SendStatusFromServer(GRPC_STATUS_UNIMPLEMENTED, "xyz", {});
61   test.Expect(103, true);
62   test.Step();
63   // Do an empty verify to make sure that the client receives the status
64   test.Step();
65 
66   // Client tries sending another message which should fail
67   c.NewBatch(3).SendMessage("hello world");
68   test.Expect(3, false);
69   test.Step();
70 
71   // Client sends close and requests status
72   CoreEnd2endTest::IncomingStatusOnClient server_status;
73   c.NewBatch(4).SendCloseFromClient().RecvStatusOnClient(server_status);
74   test.Expect(4, true);
75   test.Step();
76   EXPECT_EQ(server_status.status(), GRPC_STATUS_UNIMPLEMENTED);
77   EXPECT_EQ(server_status.message(), "xyz");
78 }
79 
CORE_END2END_TEST(CoreEnd2endTest,ClientStreaming0)80 CORE_END2END_TEST(CoreEnd2endTest, ClientStreaming0) {
81   ClientStreaming(*this, 0);
82 }
CORE_END2END_TEST(CoreEnd2endTest,ClientStreaming1)83 CORE_END2END_TEST(CoreEnd2endTest, ClientStreaming1) {
84   ClientStreaming(*this, 1);
85 }
CORE_END2END_TEST(CoreEnd2endTest,ClientStreaming3)86 CORE_END2END_TEST(CoreEnd2endTest, ClientStreaming3) {
87   ClientStreaming(*this, 3);
88 }
CORE_END2END_TEST(CoreEnd2endTest,ClientStreaming10)89 CORE_END2END_TEST(CoreEnd2endTest, ClientStreaming10) {
90   ClientStreaming(*this, 10);
91 }
CORE_END2END_TEST(CoreEnd2endTest,ClientStreaming30)92 CORE_END2END_TEST(CoreEnd2endTest, ClientStreaming30) {
93   ClientStreaming(*this, 30);
94 }
95 
96 }  // namespace
97 }  // namespace grpc_core
98