Debian Codegraph @

test/cpp/end2end/filter_end2end_test.cc

raw
1 /*
2 *
3 * Copyright 2016 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 #include <mutex>
21 #include <thread>
22
23 #include <gtest/gtest.h>
24
25 #include "absl/memory/memory.h"
26
27 #include <grpc/grpc.h>
28 #include <grpc/support/time.h>
29 #include <grpcpp/channel.h>
30 #include <grpcpp/client_context.h>
31 #include <grpcpp/create_channel.h>
32 #include <grpcpp/generic/async_generic_service.h>
33 #include <grpcpp/generic/generic_stub.h>
34 #include <grpcpp/impl/codegen/proto_utils.h>
35 #include <grpcpp/server.h>
36 #include <grpcpp/server_builder.h>
37 #include <grpcpp/server_context.h>
38 #include <grpcpp/support/config.h>
39 #include <grpcpp/support/slice.h>
40
41 #include "src/cpp/common/channel_filter.h"
42 #include "src/proto/grpc/testing/echo.grpc.pb.h"
43 #include "test/core/util/port.h"
44 #include "test/core/util/test_config.h"
45 #include "test/cpp/util/byte_buffer_proto_helper.h"
46
47 namespace grpc {
48 namespace testing {
49 namespace {
50
51 void* tag(int i) { return reinterpret_cast<void*>(i); }
52
53 void verify_ok(CompletionQueue* cq, int i, bool expect_ok) {
54 bool ok;
55 void* got_tag;
56 EXPECT_TRUE(cq->Next(&got_tag, &ok));
57 EXPECT_EQ(expect_ok, ok);
58 EXPECT_EQ(tag(i), got_tag);
59 }
60
61 namespace {
62
63 int global_num_connections = 0;
64 int global_num_calls = 0;
65 std::mutex global_mu;
66
67 void IncrementConnectionCounter() {
68 std::unique_lock<std::mutex> lock(global_mu);
69 ++global_num_connections;
70 }
71
72 void ResetConnectionCounter() {
73 std::unique_lock<std::mutex> lock(global_mu);
74 global_num_connections = 0;
75 }
76
77 int GetConnectionCounterValue() {
78 std::unique_lock<std::mutex> lock(global_mu);
79 return global_num_connections;
80 }
81
82 void IncrementCallCounter() {
83 std::unique_lock<std::mutex> lock(global_mu);
84 ++global_num_calls;
85 }
86
87 void ResetCallCounter() {
88 std::unique_lock<std::mutex> lock(global_mu);
89 global_num_calls = 0;
90 }
91
92 int GetCallCounterValue() {
93 std::unique_lock<std::mutex> lock(global_mu);
94 return global_num_calls;
95 }
96
97 } // namespace
98
99 class ChannelDataImpl : public ChannelData {
100 public:
101 grpc_error_handle Init(grpc_channel_element* /*elem*/,
102 grpc_channel_element_args* /*args*/) override {
103 IncrementConnectionCounter();
104 return absl::OkStatus();
105 }
106 };
107
108 class CallDataImpl : public CallData {
109 public:
110 void StartTransportStreamOpBatch(grpc_call_element* elem,
111 TransportStreamOpBatch* op) override {
112 // Incrementing the counter could be done from Init(), but we want
113 // to test that the individual methods are actually called correctly.
114 if (op->recv_initial_metadata() != nullptr) IncrementCallCounter();
115 grpc_call_next_op(elem, op->op());
116 }
117 };
118
119 class FilterEnd2endTest : public ::testing::Test {
120 protected:
121 FilterEnd2endTest() : server_host_("localhost") {}
122
123 static void SetUpTestCase() {
124 // Workaround for
125 // https://github.com/google/google-toolbox-for-mac/issues/242
126 static bool setup_done = false;
127 if (!setup_done) {
128 setup_done = true;
129 grpc::RegisterChannelFilter<ChannelDataImpl, CallDataImpl>(
130 "test-filter", GRPC_SERVER_CHANNEL, INT_MAX, nullptr);
131 }
132 }
133
134 void SetUp() override {
135 int port = grpc_pick_unused_port_or_die();
136 server_address_ << server_host_ << ":" << port;
137 // Setup server
138 ServerBuilder builder;
139 builder.AddListeningPort(server_address_.str(),
140 InsecureServerCredentials());
141 builder.RegisterAsyncGenericService(&generic_service_);
142 srv_cq_ = builder.AddCompletionQueue();
143 server_ = builder.BuildAndStart();
144 }
145
146 void TearDown() override {
147 server_->Shutdown();
148 void* ignored_tag;
149 bool ignored_ok;
150 cli_cq_.Shutdown();
151 srv_cq_->Shutdown();
152 while (cli_cq_.Next(&ignored_tag, &ignored_ok)) {
153 }
154 while (srv_cq_->Next(&ignored_tag, &ignored_ok)) {
155 }
156 }
157
158 void ResetStub() {
159 std::shared_ptr<Channel> channel = grpc::CreateChannel(
160 server_address_.str(), InsecureChannelCredentials());
161 generic_stub_ = std::make_unique<GenericStub>(channel);
162 ResetConnectionCounter();
163 ResetCallCounter();
164 }
165
166 void server_ok(int i) { verify_ok(srv_cq_.get(), i, true); }
167 void client_ok(int i) { verify_ok(&cli_cq_, i, true); }
168 void server_fail(int i) { verify_ok(srv_cq_.get(), i, false); }
169 void client_fail(int i) { verify_ok(&cli_cq_, i, false); }
170
171 void SendRpc(int num_rpcs) {
172 const std::string kMethodName("/grpc.cpp.test.util.EchoTestService/Echo");
173 for (int i = 0; i < num_rpcs; i++) {
174 EchoRequest send_request;
175 EchoRequest recv_request;
176 EchoResponse send_response;
177 EchoResponse recv_response;
178 Status recv_status;
179
180 ClientContext cli_ctx;
181 GenericServerContext srv_ctx;
182 GenericServerAsyncReaderWriter stream(&srv_ctx);
183
184 // The string needs to be long enough to test heap-based slice.
185 send_request.set_message("Hello world. Hello world. Hello world.");
186 std::thread request_call([this]() { server_ok(4); });
187 std::unique_ptr<GenericClientAsyncReaderWriter> call =
188 generic_stub_->PrepareCall(&cli_ctx, kMethodName, &cli_cq_);
189 call->StartCall(tag(1));
190 client_ok(1);
191 std::unique_ptr<ByteBuffer> send_buffer =
192 SerializeToByteBuffer(&send_request);
193 call->Write(*send_buffer, tag(2));
194 // Send ByteBuffer can be destroyed after calling Write.
195 send_buffer.reset();
196 client_ok(2);
197 call->WritesDone(tag(3));
198 client_ok(3);
199
200 generic_service_.RequestCall(&srv_ctx, &stream, srv_cq_.get(),
201 srv_cq_.get(), tag(4));
202
203 request_call.join();
204 EXPECT_EQ(server_host_, srv_ctx.host().substr(0, server_host_.length()));
205 EXPECT_EQ(kMethodName, srv_ctx.method());
206 ByteBuffer recv_buffer;
207 stream.Read(&recv_buffer, tag(5));
208 server_ok(5);
209 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_request));
210 EXPECT_EQ(send_request.message(), recv_request.message());
211
212 send_response.set_message(recv_request.message());
213 send_buffer = SerializeToByteBuffer(&send_response);
214 stream.Write(*send_buffer, tag(6));
215 send_buffer.reset();
216 server_ok(6);
217
218 stream.Finish(Status::OK, tag(7));
219 server_ok(7);
220
221 recv_buffer.Clear();
222 call->Read(&recv_buffer, tag(8));
223 client_ok(8);
224 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_response));
225
226 call->Finish(&recv_status, tag(9));
227 client_ok(9);
228
229 EXPECT_EQ(send_response.message(), recv_response.message());
230 EXPECT_TRUE(recv_status.ok());
231 }
232 }
233
234 CompletionQueue cli_cq_;
235 std::unique_ptr<ServerCompletionQueue> srv_cq_;
236 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
237 std::unique_ptr<grpc::GenericStub> generic_stub_;
238 std::unique_ptr<Server> server_;
239 AsyncGenericService generic_service_;
240 const std::string server_host_;
241 std::ostringstream server_address_;
242 };
243
244 TEST_F(FilterEnd2endTest, SimpleRpc) {
245 ResetStub();
246 EXPECT_EQ(0, GetConnectionCounterValue());
247 EXPECT_EQ(0, GetCallCounterValue());
248 SendRpc(1);
249 EXPECT_EQ(1, GetConnectionCounterValue());
250 EXPECT_EQ(1, GetCallCounterValue());
251 }
252
253 TEST_F(FilterEnd2endTest, SequentialRpcs) {
254 ResetStub();
255 EXPECT_EQ(0, GetConnectionCounterValue());
256 EXPECT_EQ(0, GetCallCounterValue());
257 SendRpc(10);
258 EXPECT_EQ(1, GetConnectionCounterValue());
259 EXPECT_EQ(10, GetCallCounterValue());
260 }
261
262 // One ping, one pong.
263 TEST_F(FilterEnd2endTest, SimpleBidiStreaming) {
264 ResetStub();
265 EXPECT_EQ(0, GetConnectionCounterValue());
266 EXPECT_EQ(0, GetCallCounterValue());
267
268 const std::string kMethodName(
269 "/grpc.cpp.test.util.EchoTestService/BidiStream");
270 EchoRequest send_request;
271 EchoRequest recv_request;
272 EchoResponse send_response;
273 EchoResponse recv_response;
274 Status recv_status;
275 ClientContext cli_ctx;
276 GenericServerContext srv_ctx;
277 GenericServerAsyncReaderWriter srv_stream(&srv_ctx);
278
279 cli_ctx.set_compression_algorithm(GRPC_COMPRESS_GZIP);
280 send_request.set_message("Hello");
281 std::thread request_call([this]() { server_ok(2); });
282 std::unique_ptr<GenericClientAsyncReaderWriter> cli_stream =
283 generic_stub_->PrepareCall(&cli_ctx, kMethodName, &cli_cq_);
284 cli_stream->StartCall(tag(1));
285 client_ok(1);
286
287 generic_service_.RequestCall(&srv_ctx, &srv_stream, srv_cq_.get(),
288 srv_cq_.get(), tag(2));
289
290 request_call.join();
291 EXPECT_EQ(server_host_, srv_ctx.host().substr(0, server_host_.length()));
292 EXPECT_EQ(kMethodName, srv_ctx.method());
293
294 std::unique_ptr<ByteBuffer> send_buffer =
295 SerializeToByteBuffer(&send_request);
296 cli_stream->Write(*send_buffer, tag(3));
297 send_buffer.reset();
298 client_ok(3);
299
300 ByteBuffer recv_buffer;
301 srv_stream.Read(&recv_buffer, tag(4));
302 server_ok(4);
303 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_request));
304 EXPECT_EQ(send_request.message(), recv_request.message());
305
306 send_response.set_message(recv_request.message());
307 send_buffer = SerializeToByteBuffer(&send_response);
308 srv_stream.Write(*send_buffer, tag(5));
309 send_buffer.reset();
310 server_ok(5);
311
312 cli_stream->Read(&recv_buffer, tag(6));
313 client_ok(6);
314 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_response));
315 EXPECT_EQ(send_response.message(), recv_response.message());
316
317 cli_stream->WritesDone(tag(7));
318 client_ok(7);
319
320 srv_stream.Read(&recv_buffer, tag(8));
321 server_fail(8);
322
323 srv_stream.Finish(Status::OK, tag(9));
324 server_ok(9);
325
326 cli_stream->Finish(&recv_status, tag(10));
327 client_ok(10);
328
329 EXPECT_EQ(send_response.message(), recv_response.message());
330 EXPECT_TRUE(recv_status.ok());
331
332 EXPECT_EQ(1, GetCallCounterValue());
333 EXPECT_EQ(1, GetConnectionCounterValue());
334 }
335
336 } // namespace
337 } // namespace testing
338 } // namespace grpc
339
340 int main(int argc, char** argv) {
341 grpc::testing::TestEnvironment env(&argc, argv);
342 ::testing::InitGoogleTest(&argc, argv);
343 return RUN_ALL_TESTS();
344 }