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 void SetUp() override {
125 int port = grpc_pick_unused_port_or_die();
126 server_address_ << server_host_ << ":" << port;
128 ServerBuilder builder;
129 builder.AddListeningPort(server_address_.str(),
130 InsecureServerCredentials());
131 builder.RegisterAsyncGenericService(&generic_service_);
132 srv_cq_ = builder.AddCompletionQueue();
133 server_ = builder.BuildAndStart();
136 void TearDown() override {
142 while (cli_cq_.Next(&ignored_tag, &ignored_ok))
144 while (srv_cq_->Next(&ignored_tag, &ignored_ok))
149 std::shared_ptr<Channel> channel =
150 CreateChannel(server_address_.str(), InsecureChannelCredentials());
151 generic_stub_.reset(new GenericStub(channel));
152 ResetConnectionCounter();
156 void server_ok(int i) { verify_ok(srv_cq_.get(), i, true); }
157 void client_ok(int i) { verify_ok(&cli_cq_, i, true); }
158 void server_fail(int i) { verify_ok(srv_cq_.get(), i, false); }
159 void client_fail(int i) { verify_ok(&cli_cq_, i, false); }
161 void SendRpc(int num_rpcs) {
162 const grpc::string kMethodName("/grpc.cpp.test.util.EchoTestService/Echo");
163 for (int i = 0; i < num_rpcs; i++) {
164 EchoRequest send_request;
165 EchoRequest recv_request;
166 EchoResponse send_response;
167 EchoResponse recv_response;
170 ClientContext cli_ctx;
171 GenericServerContext srv_ctx;
172 GenericServerAsyncReaderWriter stream(&srv_ctx);
174 // The string needs to be long enough to test heap-based slice.
175 send_request.set_message("Hello world. Hello world. Hello world.");
176 std::unique_ptr<GenericClientAsyncReaderWriter> call =
177 generic_stub_->PrepareCall(&cli_ctx, kMethodName, &cli_cq_);
178 call->StartCall(tag(1));
180 std::unique_ptr<ByteBuffer> send_buffer =
181 SerializeToByteBuffer(&send_request);
182 call->Write(*send_buffer, tag(2));
183 // Send ByteBuffer can be destroyed after calling Write.
186 call->WritesDone(tag(3));
189 generic_service_.RequestCall(&srv_ctx, &stream, srv_cq_.get(),
190 srv_cq_.get(), tag(4));
192 verify_ok(srv_cq_.get(), 4, true);
193 EXPECT_EQ(server_host_, srv_ctx.host().substr(0, server_host_.length()));
194 EXPECT_EQ(kMethodName, srv_ctx.method());
195 ByteBuffer recv_buffer;
196 stream.Read(&recv_buffer, tag(5));
198 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_request));
199 EXPECT_EQ(send_request.message(), recv_request.message());
201 send_response.set_message(recv_request.message());
202 send_buffer = SerializeToByteBuffer(&send_response);
203 stream.Write(*send_buffer, tag(6));
207 stream.Finish(Status::OK, tag(7));
211 call->Read(&recv_buffer, tag(8));
213 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_response));
215 call->Finish(&recv_status, tag(9));
218 EXPECT_EQ(send_response.message(), recv_response.message());
219 EXPECT_TRUE(recv_status.ok());
223 CompletionQueue cli_cq_;
224 std::unique_ptr<ServerCompletionQueue> srv_cq_;
225 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
226 std::unique_ptr<grpc::GenericStub> generic_stub_;
227 std::unique_ptr<Server> server_;
228 AsyncGenericService generic_service_;
229 const grpc::string server_host_;
230 std::ostringstream server_address_;
233 TEST_F(FilterEnd2endTest, SimpleRpc) {
235 EXPECT_EQ(0, GetConnectionCounterValue());
236 EXPECT_EQ(0, GetCallCounterValue());
238 EXPECT_EQ(1, GetConnectionCounterValue());
239 EXPECT_EQ(1, GetCallCounterValue());
242 TEST_F(FilterEnd2endTest, SequentialRpcs) {
244 EXPECT_EQ(0, GetConnectionCounterValue());
245 EXPECT_EQ(0, GetCallCounterValue());
247 EXPECT_EQ(1, GetConnectionCounterValue());
248 EXPECT_EQ(10, GetCallCounterValue());
251 // One ping, one pong.
252 TEST_F(FilterEnd2endTest, SimpleBidiStreaming) {
254 EXPECT_EQ(0, GetConnectionCounterValue());
255 EXPECT_EQ(0, GetCallCounterValue());
257 const grpc::string kMethodName(
258 "/grpc.cpp.test.util.EchoTestService/BidiStream");
259 EchoRequest send_request;
260 EchoRequest recv_request;
261 EchoResponse send_response;
262 EchoResponse recv_response;
264 ClientContext cli_ctx;
265 GenericServerContext srv_ctx;
266 GenericServerAsyncReaderWriter srv_stream(&srv_ctx);
268 cli_ctx.set_compression_algorithm(GRPC_COMPRESS_GZIP);
269 send_request.set_message("Hello");
270 std::unique_ptr<GenericClientAsyncReaderWriter> cli_stream =
271 generic_stub_->PrepareCall(&cli_ctx, kMethodName, &cli_cq_);
272 cli_stream->StartCall(tag(1));
275 generic_service_.RequestCall(&srv_ctx, &srv_stream, srv_cq_.get(),
276 srv_cq_.get(), tag(2));
278 verify_ok(srv_cq_.get(), 2, true);
279 EXPECT_EQ(server_host_, srv_ctx.host().substr(0, server_host_.length()));
280 EXPECT_EQ(kMethodName, srv_ctx.method());
282 std::unique_ptr<ByteBuffer> send_buffer =
283 SerializeToByteBuffer(&send_request);
284 cli_stream->Write(*send_buffer, tag(3));
288 ByteBuffer recv_buffer;
289 srv_stream.Read(&recv_buffer, tag(4));
291 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_request));
292 EXPECT_EQ(send_request.message(), recv_request.message());
294 send_response.set_message(recv_request.message());
295 send_buffer = SerializeToByteBuffer(&send_response);
296 srv_stream.Write(*send_buffer, tag(5));
300 cli_stream->Read(&recv_buffer, tag(6));
302 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_response));
303 EXPECT_EQ(send_response.message(), recv_response.message());
305 cli_stream->WritesDone(tag(7));
308 srv_stream.Read(&recv_buffer, tag(8));
311 srv_stream.Finish(Status::OK, tag(9));
314 cli_stream->Finish(&recv_status, tag(10));
317 EXPECT_EQ(send_response.message(), recv_response.message());
318 EXPECT_TRUE(recv_status.ok());
320 EXPECT_EQ(1, GetCallCounterValue());
321 EXPECT_EQ(1, GetConnectionCounterValue());
324 void RegisterFilter() {
325 grpc::RegisterChannelFilter<ChannelDataImpl, CallDataImpl>(
326 "test-filter", GRPC_SERVER_CHANNEL, INT_MAX, nullptr);
330 } // namespace testing
333 int main(int argc, char** argv) {
334 grpc::testing::TestEnvironment env(argc, argv);
335 ::testing::InitGoogleTest(&argc, argv);
336 grpc::testing::RegisterFilter();
337 return RUN_ALL_TESTS();