3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <grpc/grpc.h>
23 #include <grpc/support/time.h>
24 #include <grpcpp/channel.h>
25 #include <grpcpp/client_context.h>
26 #include <grpcpp/create_channel.h>
27 #include <grpcpp/generic/async_generic_service.h>
28 #include <grpcpp/generic/generic_stub.h>
29 #include <grpcpp/impl/codegen/proto_utils.h>
30 #include <grpcpp/server.h>
31 #include <grpcpp/server_builder.h>
32 #include <grpcpp/server_context.h>
33 #include <grpcpp/support/config.h>
34 #include <grpcpp/support/slice.h>
36 #include "src/cpp/common/channel_filter.h"
37 #include "src/proto/grpc/testing/echo.grpc.pb.h"
38 #include "test/core/util/port.h"
39 #include "test/core/util/test_config.h"
40 #include "test/cpp/util/byte_buffer_proto_helper.h"
42 #include <gtest/gtest.h>
44 using grpc::testing::EchoRequest;
45 using grpc::testing::EchoResponse;
46 using std::chrono::system_clock;
52 void* tag(int i) { return (void*)static_cast<intptr_t>(i); }
54 void verify_ok(CompletionQueue* cq, int i, bool expect_ok) {
57 EXPECT_TRUE(cq->Next(&got_tag, &ok));
58 EXPECT_EQ(expect_ok, ok);
59 EXPECT_EQ(tag(i), got_tag);
64 int global_num_connections = 0;
65 int global_num_calls = 0;
68 void IncrementConnectionCounter() {
69 std::unique_lock<std::mutex> lock(global_mu);
70 ++global_num_connections;
73 void ResetConnectionCounter() {
74 std::unique_lock<std::mutex> lock(global_mu);
75 global_num_connections = 0;
78 int GetConnectionCounterValue() {
79 std::unique_lock<std::mutex> lock(global_mu);
80 return global_num_connections;
83 void IncrementCallCounter() {
84 std::unique_lock<std::mutex> lock(global_mu);
88 void ResetCallCounter() {
89 std::unique_lock<std::mutex> lock(global_mu);
93 int GetCallCounterValue() {
94 std::unique_lock<std::mutex> lock(global_mu);
95 return global_num_calls;
100 class ChannelDataImpl : public ChannelData {
102 grpc_error* Init(grpc_channel_element* elem,
103 grpc_channel_element_args* args) {
104 IncrementConnectionCounter();
105 return GRPC_ERROR_NONE;
109 class CallDataImpl : public CallData {
111 void StartTransportStreamOpBatch(grpc_call_element* elem,
112 TransportStreamOpBatch* op) override {
113 // Incrementing the counter could be done from Init(), but we want
114 // to test that the individual methods are actually called correctly.
115 if (op->recv_initial_metadata() != nullptr) IncrementCallCounter();
116 grpc_call_next_op(elem, op->op());
120 class FilterEnd2endTest : public ::testing::Test {
122 FilterEnd2endTest() : server_host_("localhost") {}
124 static void SetUpTestCase() {
125 gpr_log(GPR_ERROR, "In SetUpTestCase");
126 grpc::RegisterChannelFilter<ChannelDataImpl, CallDataImpl>(
127 "test-filter", GRPC_SERVER_CHANNEL, INT_MAX, nullptr);
130 void SetUp() override {
131 int port = grpc_pick_unused_port_or_die();
132 server_address_ << server_host_ << ":" << port;
134 ServerBuilder builder;
135 builder.AddListeningPort(server_address_.str(),
136 InsecureServerCredentials());
137 builder.RegisterAsyncGenericService(&generic_service_);
138 srv_cq_ = builder.AddCompletionQueue();
139 server_ = builder.BuildAndStart();
142 void TearDown() override {
148 while (cli_cq_.Next(&ignored_tag, &ignored_ok))
150 while (srv_cq_->Next(&ignored_tag, &ignored_ok))
155 std::shared_ptr<Channel> channel = grpc::CreateChannel(
156 server_address_.str(), InsecureChannelCredentials());
157 generic_stub_.reset(new GenericStub(channel));
158 ResetConnectionCounter();
162 void server_ok(int i) { verify_ok(srv_cq_.get(), i, true); }
163 void client_ok(int i) { verify_ok(&cli_cq_, i, true); }
164 void server_fail(int i) { verify_ok(srv_cq_.get(), i, false); }
165 void client_fail(int i) { verify_ok(&cli_cq_, i, false); }
167 void SendRpc(int num_rpcs) {
168 const grpc::string kMethodName("/grpc.cpp.test.util.EchoTestService/Echo");
169 for (int i = 0; i < num_rpcs; i++) {
170 EchoRequest send_request;
171 EchoRequest recv_request;
172 EchoResponse send_response;
173 EchoResponse recv_response;
176 ClientContext cli_ctx;
177 GenericServerContext srv_ctx;
178 GenericServerAsyncReaderWriter stream(&srv_ctx);
180 // The string needs to be long enough to test heap-based slice.
181 send_request.set_message("Hello world. Hello world. Hello world.");
182 std::unique_ptr<GenericClientAsyncReaderWriter> call =
183 generic_stub_->PrepareCall(&cli_ctx, kMethodName, &cli_cq_);
184 call->StartCall(tag(1));
186 std::unique_ptr<ByteBuffer> send_buffer =
187 SerializeToByteBuffer(&send_request);
188 call->Write(*send_buffer, tag(2));
189 // Send ByteBuffer can be destroyed after calling Write.
192 call->WritesDone(tag(3));
195 generic_service_.RequestCall(&srv_ctx, &stream, srv_cq_.get(),
196 srv_cq_.get(), tag(4));
198 verify_ok(srv_cq_.get(), 4, true);
199 EXPECT_EQ(server_host_, srv_ctx.host().substr(0, server_host_.length()));
200 EXPECT_EQ(kMethodName, srv_ctx.method());
201 ByteBuffer recv_buffer;
202 stream.Read(&recv_buffer, tag(5));
204 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_request));
205 EXPECT_EQ(send_request.message(), recv_request.message());
207 send_response.set_message(recv_request.message());
208 send_buffer = SerializeToByteBuffer(&send_response);
209 stream.Write(*send_buffer, tag(6));
213 stream.Finish(Status::OK, tag(7));
217 call->Read(&recv_buffer, tag(8));
219 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_response));
221 call->Finish(&recv_status, tag(9));
224 EXPECT_EQ(send_response.message(), recv_response.message());
225 EXPECT_TRUE(recv_status.ok());
229 CompletionQueue cli_cq_;
230 std::unique_ptr<ServerCompletionQueue> srv_cq_;
231 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
232 std::unique_ptr<grpc::GenericStub> generic_stub_;
233 std::unique_ptr<Server> server_;
234 AsyncGenericService generic_service_;
235 const grpc::string server_host_;
236 std::ostringstream server_address_;
239 TEST_F(FilterEnd2endTest, SimpleRpc) {
241 EXPECT_EQ(0, GetConnectionCounterValue());
242 EXPECT_EQ(0, GetCallCounterValue());
244 EXPECT_EQ(1, GetConnectionCounterValue());
245 EXPECT_EQ(1, GetCallCounterValue());
248 TEST_F(FilterEnd2endTest, SequentialRpcs) {
250 EXPECT_EQ(0, GetConnectionCounterValue());
251 EXPECT_EQ(0, GetCallCounterValue());
253 EXPECT_EQ(1, GetConnectionCounterValue());
254 EXPECT_EQ(10, GetCallCounterValue());
257 // One ping, one pong.
258 TEST_F(FilterEnd2endTest, SimpleBidiStreaming) {
260 EXPECT_EQ(0, GetConnectionCounterValue());
261 EXPECT_EQ(0, GetCallCounterValue());
263 const grpc::string kMethodName(
264 "/grpc.cpp.test.util.EchoTestService/BidiStream");
265 EchoRequest send_request;
266 EchoRequest recv_request;
267 EchoResponse send_response;
268 EchoResponse recv_response;
270 ClientContext cli_ctx;
271 GenericServerContext srv_ctx;
272 GenericServerAsyncReaderWriter srv_stream(&srv_ctx);
274 cli_ctx.set_compression_algorithm(GRPC_COMPRESS_GZIP);
275 send_request.set_message("Hello");
276 std::unique_ptr<GenericClientAsyncReaderWriter> cli_stream =
277 generic_stub_->PrepareCall(&cli_ctx, kMethodName, &cli_cq_);
278 cli_stream->StartCall(tag(1));
281 generic_service_.RequestCall(&srv_ctx, &srv_stream, srv_cq_.get(),
282 srv_cq_.get(), tag(2));
284 verify_ok(srv_cq_.get(), 2, true);
285 EXPECT_EQ(server_host_, srv_ctx.host().substr(0, server_host_.length()));
286 EXPECT_EQ(kMethodName, srv_ctx.method());
288 std::unique_ptr<ByteBuffer> send_buffer =
289 SerializeToByteBuffer(&send_request);
290 cli_stream->Write(*send_buffer, tag(3));
294 ByteBuffer recv_buffer;
295 srv_stream.Read(&recv_buffer, tag(4));
297 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_request));
298 EXPECT_EQ(send_request.message(), recv_request.message());
300 send_response.set_message(recv_request.message());
301 send_buffer = SerializeToByteBuffer(&send_response);
302 srv_stream.Write(*send_buffer, tag(5));
306 cli_stream->Read(&recv_buffer, tag(6));
308 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_response));
309 EXPECT_EQ(send_response.message(), recv_response.message());
311 cli_stream->WritesDone(tag(7));
314 srv_stream.Read(&recv_buffer, tag(8));
317 srv_stream.Finish(Status::OK, tag(9));
320 cli_stream->Finish(&recv_status, tag(10));
323 EXPECT_EQ(send_response.message(), recv_response.message());
324 EXPECT_TRUE(recv_status.ok());
326 EXPECT_EQ(1, GetCallCounterValue());
327 EXPECT_EQ(1, GetConnectionCounterValue());
331 } // namespace testing
334 int main(int argc, char** argv) {
335 grpc::testing::TestEnvironment env(argc, argv);
336 ::testing::InitGoogleTest(&argc, argv);
337 return RUN_ALL_TESTS();