xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/payload.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 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 "src/core/lib/slice/slice.h"
27 #include "test/core/end2end/end2end_tests.h"
28 
29 namespace grpc_core {
30 
31 namespace {
RequestResponseWithPayload(CoreEnd2endTest & test)32 void RequestResponseWithPayload(CoreEnd2endTest& test) {
33   // Create large request and response bodies. These are big enough to require
34   // multiple round trips to deliver to the peer, and their exact contents of
35   // will be verified on completion.
36   auto request_slice = RandomSlice(1024 * 1024);
37   auto response_slice = RandomSlice(1024 * 1024);
38 
39   auto c = test.NewClientCall("/foo").Timeout(Duration::Seconds(60)).Create();
40 
41   CoreEnd2endTest::IncomingMetadata server_initial_md;
42   CoreEnd2endTest::IncomingMessage server_message;
43   CoreEnd2endTest::IncomingStatusOnClient server_status;
44   c.NewBatch(1)
45       .SendInitialMetadata({})
46       .SendMessage(request_slice.Ref())
47       .SendCloseFromClient()
48       .RecvInitialMetadata(server_initial_md)
49       .RecvMessage(server_message)
50       .RecvStatusOnClient(server_status);
51 
52   CoreEnd2endTest::IncomingCall s = test.RequestCall(101);
53   test.Expect(101, true);
54   test.Step();
55 
56   CoreEnd2endTest::IncomingMessage client_message;
57   s.NewBatch(102).SendInitialMetadata({}).RecvMessage(client_message);
58   test.Expect(102, true);
59   test.Step();
60 
61   CoreEnd2endTest::IncomingCloseOnServer client_close;
62   s.NewBatch(103)
63       .RecvCloseOnServer(client_close)
64       .SendMessage(response_slice.Ref())
65       .SendStatusFromServer(GRPC_STATUS_OK, "xyz", {});
66   test.Expect(103, true);
67   test.Expect(1, true);
68   test.Step();
69 
70   EXPECT_EQ(server_status.status(), GRPC_STATUS_OK);
71   EXPECT_EQ(server_status.message(), "xyz");
72   EXPECT_EQ(s.method(), "/foo");
73   EXPECT_FALSE(client_close.was_cancelled());
74   EXPECT_EQ(client_message.payload(), request_slice);
75   EXPECT_EQ(server_message.payload(), response_slice);
76 }
77 }  // namespace
78 
79 // Client sends a request with payload, server reads then returns a response
80 // payload and status.
CORE_END2END_TEST(CoreLargeSendTest,RequestResponseWithPayload)81 CORE_END2END_TEST(CoreLargeSendTest, RequestResponseWithPayload) {
82   RequestResponseWithPayload(*this);
83 }
84 
CORE_END2END_TEST(CoreLargeSendTest,RequestResponseWithPayload10Times)85 CORE_END2END_TEST(CoreLargeSendTest, RequestResponseWithPayload10Times) {
86   for (int i = 0; i < 10; i++) {
87     RequestResponseWithPayload(*this);
88   }
89 }
90 
91 }  // namespace grpc_core
92