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