xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/write_buffering.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2017 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/grpc.h>
24 #include <grpc/status.h>
25 
26 #include "src/core/lib/gprpp/time.h"
27 #include "test/core/end2end/end2end_tests.h"
28 
29 namespace grpc_core {
30 // Client sends a request with payload, server reads then returns status.
CORE_END2END_TEST(WriteBufferingTest,WriteBufferingWorks)31 CORE_END2END_TEST(WriteBufferingTest, WriteBufferingWorks) {
32   auto c = NewClientCall("/foo").Timeout(Duration::Minutes(1)).Create();
33   c.NewBatch(1).SendInitialMetadata({});
34   CoreEnd2endTest::IncomingMetadata server_initial_metadata;
35   c.NewBatch(2).RecvInitialMetadata(server_initial_metadata);
36   auto s = RequestCall(101);
37   Expect(1, true);  // send message is buffered
38   Expect(101, true);
39   Step();
40   c.NewBatch(3).SendMessage("hello world", GRPC_WRITE_BUFFER_HINT);
41   s.NewBatch(102).SendInitialMetadata({});
42 
43   // recv message should not succeed yet - it's buffered at the client still
44   CoreEnd2endTest::IncomingMessage request_payload_recv1;
45   s.NewBatch(103).RecvMessage(request_payload_recv1);
46   Expect(2, true);
47   Expect(3, true);
48   Expect(102, true);
49   Step();
50 
51   // send another message, this time not buffered
52   c.NewBatch(4).SendMessage("abc123");
53   // now the first send should match up with the first recv
54   Expect(103, true);
55   Expect(4, true);
56   Step();
57 
58   // and the next recv should be ready immediately also
59   CoreEnd2endTest::IncomingMessage request_payload_recv2;
60   s.NewBatch(104).RecvMessage(request_payload_recv2);
61   Expect(104, true);
62   Step();
63 
64   CoreEnd2endTest::IncomingStatusOnClient server_status;
65   c.NewBatch(5).SendCloseFromClient().RecvStatusOnClient(server_status);
66 
67   CoreEnd2endTest::IncomingCloseOnServer client_close;
68   s.NewBatch(105)
69       .RecvCloseOnServer(client_close)
70       .SendStatusFromServer(GRPC_STATUS_OK, "xyz", {});
71 
72   Expect(105, true);
73   Expect(5, true);
74   Step();
75 
76   EXPECT_EQ(server_status.status(), GRPC_STATUS_OK);
77   EXPECT_EQ(server_status.message(), "xyz");
78   EXPECT_EQ(s.method(), "/foo");
79   EXPECT_FALSE(client_close.was_cancelled());
80   EXPECT_EQ(request_payload_recv1.payload(), "hello world");
81   EXPECT_EQ(request_payload_recv2.payload(), "abc123");
82 }
83 }  // namespace grpc_core
84