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 <grpc/status.h>
22
23 #include "src/core/lib/gprpp/time.h"
24 #include "src/core/lib/slice/slice.h"
25 #include "test/core/end2end/end2end_tests.h"
26
27 namespace grpc_core {
28 // Client pings and server pongs. Repeat messages rounds before finishing.
PingPongStreaming(CoreEnd2endTest & test,int num_messages)29 void PingPongStreaming(CoreEnd2endTest& test, int num_messages) {
30 auto request_slice = RandomSlice(20);
31 auto response_slice = RandomSlice(15);
32 auto c = test.NewClientCall("/foo").Timeout(Duration::Seconds(5)).Create();
33 CoreEnd2endTest::IncomingMetadata server_initial_md;
34 CoreEnd2endTest::IncomingStatusOnClient server_status;
35 c.NewBatch(1)
36 .SendInitialMetadata({})
37 .RecvInitialMetadata(server_initial_md)
38 .RecvStatusOnClient(server_status);
39 auto s = test.RequestCall(100);
40 test.Expect(100, true);
41 test.Step();
42 CoreEnd2endTest::IncomingCloseOnServer client_close;
43 s.NewBatch(101).SendInitialMetadata({}).RecvCloseOnServer(client_close);
44 for (int i = 0; i < num_messages; i++) {
45 CoreEnd2endTest::IncomingMessage server_message;
46 c.NewBatch(2).SendMessage(request_slice.Ref()).RecvMessage(server_message);
47 CoreEnd2endTest::IncomingMessage client_message;
48 s.NewBatch(102).RecvMessage(client_message);
49 test.Expect(102, true);
50 test.Step();
51 s.NewBatch(103).SendMessage(response_slice.Ref());
52 test.Expect(2, true);
53 test.Expect(103, true);
54 test.Step();
55 }
56 c.NewBatch(3).SendCloseFromClient();
57 s.NewBatch(104).SendStatusFromServer(GRPC_STATUS_UNIMPLEMENTED, "xyz", {});
58 test.Expect(1, true);
59 test.Expect(3, true);
60 test.Expect(101, true);
61 test.Expect(104, true);
62 test.Step();
63 }
64
CORE_END2END_TEST(CoreEnd2endTest,PingPongStreaming1)65 CORE_END2END_TEST(CoreEnd2endTest, PingPongStreaming1) {
66 PingPongStreaming(*this, 1);
67 }
68
CORE_END2END_TEST(CoreEnd2endTest,PingPongStreaming3)69 CORE_END2END_TEST(CoreEnd2endTest, PingPongStreaming3) {
70 PingPongStreaming(*this, 3);
71 }
72
CORE_END2END_TEST(CoreEnd2endTest,PingPongStreaming10)73 CORE_END2END_TEST(CoreEnd2endTest, PingPongStreaming10) {
74 PingPongStreaming(*this, 10);
75 }
76
CORE_END2END_TEST(CoreEnd2endTest,PingPongStreaming30)77 CORE_END2END_TEST(CoreEnd2endTest, PingPongStreaming30) {
78 PingPongStreaming(*this, 30);
79 }
80 } // namespace grpc_core
81