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 namespace {
31
CORE_END2END_TEST(WriteBufferingTest,WriteBufferingAtEnd)32 CORE_END2END_TEST(WriteBufferingTest, WriteBufferingAtEnd) {
33 auto c = NewClientCall("/foo").Timeout(Duration::Minutes(1)).Create();
34 c.NewBatch(1).SendInitialMetadata({});
35 CoreEnd2endTest::IncomingMetadata server_initial_metadata;
36 c.NewBatch(2).RecvInitialMetadata(server_initial_metadata);
37
38 auto s = RequestCall(101);
39 Expect(1, true); // send message is buffered
40 Expect(101, true);
41 Step();
42
43 c.NewBatch(3).SendMessage("hello world", GRPC_WRITE_BUFFER_HINT);
44 s.NewBatch(102).SendInitialMetadata({});
45 // recv message should not succeed yet - it's buffered at the client still
46 CoreEnd2endTest::IncomingMessage request_payload_recv1;
47 s.NewBatch(103).RecvMessage(request_payload_recv1);
48 Expect(2, true);
49 Expect(3, true);
50 Expect(102, true);
51 Step();
52
53 // send end of stream: should release the buffering
54 c.NewBatch(4).SendCloseFromClient();
55 // now the first send should match up with the first recv
56 Expect(103, true);
57 Expect(4, true);
58 Step();
59
60 // and the next recv should be ready immediately also (and empty)
61 CoreEnd2endTest::IncomingMessage request_payload_recv2;
62 s.NewBatch(104).RecvMessage(request_payload_recv2);
63 Expect(104, true);
64 Step();
65
66 CoreEnd2endTest::IncomingStatusOnClient server_status;
67 c.NewBatch(4).RecvStatusOnClient(server_status);
68 CoreEnd2endTest::IncomingCloseOnServer client_close;
69 s.NewBatch(105)
70 .RecvCloseOnServer(client_close)
71 .SendStatusFromServer(GRPC_STATUS_OK, "xyz", {});
72 Expect(105, true);
73 Expect(4, 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_TRUE(request_payload_recv2.is_end_of_stream());
82 }
83
84 } // namespace
85 } // namespace grpc_core
86