1 /*
2 *
3 * Copyright 2018 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 "test/cpp/end2end/interceptors_util.h"
20
21 namespace grpc {
22 namespace testing {
23
24 std::atomic<int> DummyInterceptor::num_times_run_;
25 std::atomic<int> DummyInterceptor::num_times_run_reverse_;
26 std::atomic<int> DummyInterceptor::num_times_cancel_;
27
MakeCall(const std::shared_ptr<Channel> & channel)28 void MakeCall(const std::shared_ptr<Channel>& channel) {
29 auto stub = grpc::testing::EchoTestService::NewStub(channel);
30 ClientContext ctx;
31 EchoRequest req;
32 req.mutable_param()->set_echo_metadata(true);
33 ctx.AddMetadata("testkey", "testvalue");
34 req.set_message("Hello");
35 EchoResponse resp;
36 Status s = stub->Echo(&ctx, req, &resp);
37 EXPECT_EQ(s.ok(), true);
38 EXPECT_EQ(resp.message(), "Hello");
39 }
40
MakeClientStreamingCall(const std::shared_ptr<Channel> & channel)41 void MakeClientStreamingCall(const std::shared_ptr<Channel>& channel) {
42 auto stub = grpc::testing::EchoTestService::NewStub(channel);
43 ClientContext ctx;
44 EchoRequest req;
45 req.mutable_param()->set_echo_metadata(true);
46 ctx.AddMetadata("testkey", "testvalue");
47 req.set_message("Hello");
48 EchoResponse resp;
49 string expected_resp = "";
50 auto writer = stub->RequestStream(&ctx, &resp);
51 for (int i = 0; i < kNumStreamingMessages; i++) {
52 writer->Write(req);
53 expected_resp += "Hello";
54 }
55 writer->WritesDone();
56 Status s = writer->Finish();
57 EXPECT_EQ(s.ok(), true);
58 EXPECT_EQ(resp.message(), expected_resp);
59 }
60
MakeServerStreamingCall(const std::shared_ptr<Channel> & channel)61 void MakeServerStreamingCall(const std::shared_ptr<Channel>& channel) {
62 auto stub = grpc::testing::EchoTestService::NewStub(channel);
63 ClientContext ctx;
64 EchoRequest req;
65 req.mutable_param()->set_echo_metadata(true);
66 ctx.AddMetadata("testkey", "testvalue");
67 req.set_message("Hello");
68 EchoResponse resp;
69 auto reader = stub->ResponseStream(&ctx, req);
70 int count = 0;
71 while (reader->Read(&resp)) {
72 EXPECT_EQ(resp.message(), "Hello");
73 count++;
74 }
75 ASSERT_EQ(count, kNumStreamingMessages);
76 Status s = reader->Finish();
77 EXPECT_EQ(s.ok(), true);
78 }
79
MakeBidiStreamingCall(const std::shared_ptr<Channel> & channel)80 void MakeBidiStreamingCall(const std::shared_ptr<Channel>& channel) {
81 auto stub = grpc::testing::EchoTestService::NewStub(channel);
82 ClientContext ctx;
83 EchoRequest req;
84 EchoResponse resp;
85 ctx.AddMetadata("testkey", "testvalue");
86 req.mutable_param()->set_echo_metadata(true);
87 auto stream = stub->BidiStream(&ctx);
88 for (auto i = 0; i < kNumStreamingMessages; i++) {
89 req.set_message("Hello" + std::to_string(i));
90 stream->Write(req);
91 stream->Read(&resp);
92 EXPECT_EQ(req.message(), resp.message());
93 }
94 ASSERT_TRUE(stream->WritesDone());
95 Status s = stream->Finish();
96 EXPECT_EQ(s.ok(), true);
97 }
98
MakeAsyncCQCall(const std::shared_ptr<Channel> & channel)99 void MakeAsyncCQCall(const std::shared_ptr<Channel>& channel) {
100 auto stub = grpc::testing::EchoTestService::NewStub(channel);
101 CompletionQueue cq;
102 EchoRequest send_request;
103 EchoResponse recv_response;
104 Status recv_status;
105 ClientContext cli_ctx;
106
107 send_request.set_message("Hello");
108 cli_ctx.AddMetadata("testkey", "testvalue");
109 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
110 stub->AsyncEcho(&cli_ctx, send_request, &cq));
111 response_reader->Finish(&recv_response, &recv_status, tag(1));
112 Verifier().Expect(1, true).Verify(&cq);
113 EXPECT_EQ(send_request.message(), recv_response.message());
114 EXPECT_TRUE(recv_status.ok());
115 }
116
MakeAsyncCQClientStreamingCall(const std::shared_ptr<Channel> & channel)117 void MakeAsyncCQClientStreamingCall(const std::shared_ptr<Channel>& channel) {
118 // TODO(yashykt) : Fill this out
119 }
120
MakeAsyncCQServerStreamingCall(const std::shared_ptr<Channel> & channel)121 void MakeAsyncCQServerStreamingCall(const std::shared_ptr<Channel>& channel) {
122 auto stub = grpc::testing::EchoTestService::NewStub(channel);
123 CompletionQueue cq;
124 EchoRequest send_request;
125 EchoResponse recv_response;
126 Status recv_status;
127 ClientContext cli_ctx;
128
129 cli_ctx.AddMetadata("testkey", "testvalue");
130 send_request.set_message("Hello");
131 std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream(
132 stub->AsyncResponseStream(&cli_ctx, send_request, &cq, tag(1)));
133 Verifier().Expect(1, true).Verify(&cq);
134 // Read the expected number of messages
135 for (int i = 0; i < kNumStreamingMessages; i++) {
136 cli_stream->Read(&recv_response, tag(2));
137 Verifier().Expect(2, true).Verify(&cq);
138 ASSERT_EQ(recv_response.message(), send_request.message());
139 }
140 // The next read should fail
141 cli_stream->Read(&recv_response, tag(3));
142 Verifier().Expect(3, false).Verify(&cq);
143 // Get the status
144 cli_stream->Finish(&recv_status, tag(4));
145 Verifier().Expect(4, true).Verify(&cq);
146 EXPECT_TRUE(recv_status.ok());
147 }
148
MakeAsyncCQBidiStreamingCall(const std::shared_ptr<Channel> & channel)149 void MakeAsyncCQBidiStreamingCall(const std::shared_ptr<Channel>& channel) {
150 // TODO(yashykt) : Fill this out
151 }
152
MakeCallbackCall(const std::shared_ptr<Channel> & channel)153 void MakeCallbackCall(const std::shared_ptr<Channel>& channel) {
154 auto stub = grpc::testing::EchoTestService::NewStub(channel);
155 ClientContext ctx;
156 EchoRequest req;
157 std::mutex mu;
158 std::condition_variable cv;
159 bool done = false;
160 req.mutable_param()->set_echo_metadata(true);
161 ctx.AddMetadata("testkey", "testvalue");
162 req.set_message("Hello");
163 EchoResponse resp;
164 stub->experimental_async()->Echo(&ctx, &req, &resp,
165 [&resp, &mu, &done, &cv](Status s) {
166 EXPECT_EQ(s.ok(), true);
167 EXPECT_EQ(resp.message(), "Hello");
168 std::lock_guard<std::mutex> l(mu);
169 done = true;
170 cv.notify_one();
171 });
172 std::unique_lock<std::mutex> l(mu);
173 while (!done) {
174 cv.wait(l);
175 }
176 }
177
CheckMetadata(const std::multimap<grpc::string_ref,grpc::string_ref> & map,const string & key,const string & value)178 bool CheckMetadata(const std::multimap<grpc::string_ref, grpc::string_ref>& map,
179 const string& key, const string& value) {
180 for (const auto& pair : map) {
181 if (pair.first.starts_with(key) && pair.second.starts_with(value)) {
182 return true;
183 }
184 }
185 return false;
186 }
187
CheckMetadata(const std::multimap<std::string,std::string> & map,const string & key,const string & value)188 bool CheckMetadata(const std::multimap<std::string, std::string>& map,
189 const string& key, const string& value) {
190 for (const auto& pair : map) {
191 if (pair.first == key && pair.second == value) {
192 return true;
193 }
194 }
195 return false;
196 }
197
198 std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
CreateDummyClientInterceptors()199 CreateDummyClientInterceptors() {
200 std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
201 creators;
202 // Add 20 dummy interceptors before hijacking interceptor
203 creators.reserve(20);
204 for (auto i = 0; i < 20; i++) {
205 creators.push_back(std::unique_ptr<DummyInterceptorFactory>(
206 new DummyInterceptorFactory()));
207 }
208 return creators;
209 }
210
211 } // namespace testing
212 } // namespace grpc
213