22 #include <gtest/gtest.h>
24 #include "absl/memory/memory.h"
25 #include "absl/strings/match.h"
37 #include "src/proto/grpc/testing/echo.grpc.pb.h"
48 class LoggingInterceptor :
public experimental::Interceptor {
50 explicit LoggingInterceptor(experimental::ServerRpcInfo* info) {
52 const char*
method = info->method();
63 (strcmp(
method,
"/grpc.testing.EchoTestService/Echo") == 0 &&
66 (strcmp(
method,
"/grpc.testing.EchoTestService/RequestStream") == 0 &&
68 (strcmp(
method,
"/grpc.testing.EchoTestService/ResponseStream") == 0 &&
70 (strcmp(
method,
"/grpc.testing.EchoTestService/BidiStream") == 0 &&
72 strcmp(
method,
"/grpc.testing.EchoTestService/Unimplemented") == 0 ||
73 (strcmp(
method,
"") == 0 &&
77 void Intercept(experimental::InterceptorBatchMethods* methods)
override {
78 if (methods->QueryInterceptionHookPoint(
80 auto*
map = methods->GetSendInitialMetadata();
84 if (methods->QueryInterceptionHookPoint(
87 auto*
buffer = methods->GetSerializedSendMessage();
88 auto copied_buffer = *
buffer;
90 SerializationTraits<EchoRequest>::Deserialize(&copied_buffer, &
req)
94 if (methods->QueryInterceptionHookPoint(
96 auto*
map = methods->GetSendTrailingMetadata();
105 auto status = methods->GetSendStatus();
108 if (methods->QueryInterceptionHookPoint(
110 auto*
map = methods->GetRecvInitialMetadata();
113 for (
const auto&
pair : *
map) {
114 found =
pair.first.find(
"testkey") == 0 &&
115 pair.second.find(
"testvalue") == 0;
120 if (methods->QueryInterceptionHookPoint(
123 static_cast<EchoResponse*
>(methods->GetRecvMessage());
124 if (
resp !=
nullptr) {
128 if (methods->QueryInterceptionHookPoint(
136 class LoggingInterceptorFactory
137 :
public experimental::ServerInterceptorFactoryInterface {
139 experimental::Interceptor* CreateServerInterceptor(
140 experimental::ServerRpcInfo* info)
override {
141 return new LoggingInterceptor(info);
146 class SyncSendMessageTester :
public experimental::Interceptor {
148 explicit SyncSendMessageTester(experimental::ServerRpcInfo* ) {}
150 void Intercept(experimental::InterceptorBatchMethods* methods)
override {
151 if (methods->QueryInterceptionHookPoint(
154 static_cast<const EchoRequest*
>(methods->GetSendMessage())->
message();
156 new_msg_.set_message(
"World" + old_msg);
157 methods->ModifySendMessage(&
new_msg_);
166 class SyncSendMessageTesterFactory
167 :
public experimental::ServerInterceptorFactoryInterface {
169 experimental::Interceptor* CreateServerInterceptor(
170 experimental::ServerRpcInfo* info)
override {
171 return new SyncSendMessageTester(info);
176 class SyncSendMessageVerifier :
public experimental::Interceptor {
178 explicit SyncSendMessageVerifier(experimental::ServerRpcInfo* ) {}
180 void Intercept(experimental::InterceptorBatchMethods* methods)
override {
181 if (methods->QueryInterceptionHookPoint(
185 static_cast<const EchoRequest*
>(methods->GetSendMessage())->
message();
189 new_msg_.set_message(old_msg.erase(0, 5));
190 methods->ModifySendMessage(&
new_msg_);
201 class SyncSendMessageVerifierFactory
202 :
public experimental::ServerInterceptorFactoryInterface {
204 experimental::Interceptor* CreateServerInterceptor(
205 experimental::ServerRpcInfo* info)
override {
206 return new SyncSendMessageVerifier(info);
211 auto stub = grpc::testing::EchoTestService::NewStub(
channel);
215 ctx.AddMetadata(
"testkey",
"testvalue");
217 for (
auto i = 0;
i < 10;
i++) {
228 class ServerInterceptorsEnd2endSyncUnaryTest :
public ::testing::Test {
230 ServerInterceptorsEnd2endSyncUnaryTest() {
239 std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
242 std::unique_ptr<experimental::ServerInterceptorFactoryInterface>(
243 new SyncSendMessageTesterFactory()));
245 std::unique_ptr<experimental::ServerInterceptorFactoryInterface>(
246 new SyncSendMessageVerifierFactory()));
248 std::unique_ptr<experimental::ServerInterceptorFactoryInterface>(
249 new LoggingInterceptorFactory()));
251 for (
auto i = 0;
i < 20;
i++) {
252 creators.push_back(absl::make_unique<PhonyInterceptorFactory>());
253 creators.push_back(absl::make_unique<NullInterceptorFactory>());
263 TEST_F(ServerInterceptorsEnd2endSyncUnaryTest, UnaryTest) {
264 ChannelArguments
args;
273 class ServerInterceptorsEnd2endSyncStreamingTest :
public ::testing::Test {
275 ServerInterceptorsEnd2endSyncStreamingTest() {
284 std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
287 std::unique_ptr<experimental::ServerInterceptorFactoryInterface>(
288 new SyncSendMessageTesterFactory()));
290 std::unique_ptr<experimental::ServerInterceptorFactoryInterface>(
291 new SyncSendMessageVerifierFactory()));
293 std::unique_ptr<experimental::ServerInterceptorFactoryInterface>(
294 new LoggingInterceptorFactory()));
295 for (
auto i = 0;
i < 20;
i++) {
296 creators.push_back(absl::make_unique<PhonyInterceptorFactory>());
302 EchoTestServiceStreamingImpl
service_;
303 std::unique_ptr<Server>
server_;
306 TEST_F(ServerInterceptorsEnd2endSyncStreamingTest, ClientStreamingTest) {
307 ChannelArguments
args;
316 TEST_F(ServerInterceptorsEnd2endSyncStreamingTest, ServerStreamingTest) {
317 ChannelArguments
args;
326 TEST_F(ServerInterceptorsEnd2endSyncStreamingTest, BidiStreamingTest) {
327 ChannelArguments
args;
338 TEST_F(ServerInterceptorsAsyncEnd2endTest, UnaryTest) {
343 EchoTestService::AsyncService
service;
346 std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
349 std::unique_ptr<experimental::ServerInterceptorFactoryInterface>(
350 new LoggingInterceptorFactory()));
351 for (
auto i = 0;
i < 20;
i++) {
352 creators.push_back(absl::make_unique<PhonyInterceptorFactory>());
358 ChannelArguments
args;
361 auto stub = grpc::testing::EchoTestService::NewStub(
channel);
364 EchoRequest recv_request;
365 EchoResponse send_response;
366 EchoResponse recv_response;
369 ClientContext cli_ctx;
370 ServerContext srv_ctx;
374 cli_ctx.AddMetadata(
"testkey",
"testvalue");
375 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
378 service.RequestEcho(&srv_ctx, &recv_request, &response_writer,
cq.get(),
381 response_reader->Finish(&recv_response, &recv_status,
tag(4));
383 Verifier().Expect(2,
true).Verify(
cq.get());
387 srv_ctx.AddTrailingMetadata(
"testkey",
"testvalue");
389 send_response.set_message(recv_request.message());
391 Verifier().Expect(3,
true).Expect(4,
true).Verify(
cq.get());
393 EXPECT_EQ(send_response.message(), recv_response.message());
405 while (
cq->Next(&ignored_tag, &ignored_ok)) {
410 TEST_F(ServerInterceptorsAsyncEnd2endTest, BidiStreamingTest) {
415 EchoTestService::AsyncService
service;
418 std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
421 std::unique_ptr<experimental::ServerInterceptorFactoryInterface>(
422 new LoggingInterceptorFactory()));
423 for (
auto i = 0;
i < 20;
i++) {
424 creators.push_back(absl::make_unique<PhonyInterceptorFactory>());
430 ChannelArguments
args;
433 auto stub = grpc::testing::EchoTestService::NewStub(
channel);
436 EchoRequest recv_request;
437 EchoResponse send_response;
438 EchoResponse recv_response;
441 ClientContext cli_ctx;
442 ServerContext srv_ctx;
446 cli_ctx.AddMetadata(
"testkey",
"testvalue");
447 std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>>
448 cli_stream(
stub->AsyncBidiStream(&cli_ctx,
cq.get(),
tag(1)));
450 service.RequestBidiStream(&srv_ctx, &srv_stream,
cq.get(),
cq.get(),
tag(2));
452 Verifier().Expect(1,
true).Expect(2,
true).Verify(
cq.get());
455 srv_ctx.AddTrailingMetadata(
"testkey",
"testvalue");
458 srv_stream.Read(&recv_request,
tag(4));
459 Verifier().Expect(3,
true).Expect(4,
true).Verify(
cq.get());
462 send_response.set_message(recv_request.message());
463 srv_stream.Write(send_response,
tag(5));
464 cli_stream->Read(&recv_response,
tag(6));
465 Verifier().Expect(5,
true).Expect(6,
true).Verify(
cq.get());
466 EXPECT_EQ(send_response.message(), recv_response.message());
468 cli_stream->WritesDone(
tag(7));
469 srv_stream.Read(&recv_request,
tag(8));
470 Verifier().Expect(7,
true).Expect(8,
false).Verify(
cq.get());
473 cli_stream->Finish(&recv_status,
tag(10));
474 Verifier().Expect(9,
true).Expect(10,
true).Verify(
cq.get());
487 while (
cq->Next(&ignored_tag, &ignored_ok)) {
492 TEST_F(ServerInterceptorsAsyncEnd2endTest, GenericRPCTest) {
500 std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
502 creators.reserve(20);
503 for (
auto i = 0;
i < 20;
i++) {
504 creators.push_back(absl::make_unique<PhonyInterceptorFactory>());
507 auto srv_cq =
builder.AddCompletionQueue();
508 CompletionQueue cli_cq;
511 ChannelArguments
args;
516 const std::string kMethodName(
"/grpc.cpp.test.util.EchoTestService/Echo");
518 EchoRequest recv_request;
519 EchoResponse send_response;
520 EchoResponse recv_response;
523 ClientContext cli_ctx;
524 GenericServerContext srv_ctx;
529 cli_ctx.AddMetadata(
"testkey",
"testvalue");
531 CompletionQueue*
cq = srv_cq.get();
533 std::unique_ptr<GenericClientAsyncReaderWriter>
call =
534 generic_stub.PrepareCall(&cli_ctx, kMethodName, &cli_cq);
536 Verifier().Expect(1,
true).Verify(&cli_cq);
542 Verifier().Expect(2,
true).Verify(&cli_cq);
544 Verifier().Expect(3,
true).Verify(&cli_cq);
549 EXPECT_EQ(kMethodName, srv_ctx.method());
551 srv_ctx.AddTrailingMetadata(
"testkey",
"testvalue");
553 ByteBuffer recv_buffer;
555 Verifier().Expect(5,
true).Verify(srv_cq.get());
559 send_response.set_message(recv_request.message());
563 Verifier().Expect(6,
true).Verify(srv_cq.get());
570 Verifier().Expect(7,
true).Verify(srv_cq.get());
573 call->Read(&recv_buffer,
tag(8));
574 Verifier().Expect(8,
true).Verify(&cli_cq);
577 call->Finish(&recv_status,
tag(9));
579 Verifier().Expect(9,
true).Verify(&cli_cq);
581 EXPECT_EQ(send_response.message(), recv_response.message());
592 while (cli_cq.Next(&ignored_tag, &ignored_ok)) {
594 while (srv_cq->Next(&ignored_tag, &ignored_ok)) {
599 TEST_F(ServerInterceptorsAsyncEnd2endTest, UnimplementedRpcTest) {
605 std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
607 creators.reserve(20);
608 for (
auto i = 0;
i < 20;
i++) {
609 creators.push_back(absl::make_unique<PhonyInterceptorFactory>());
615 ChannelArguments
args;
616 std::shared_ptr<Channel>
channel =
618 std::unique_ptr<grpc::testing::UnimplementedEchoService::Stub>
stub;
619 stub = grpc::testing::UnimplementedEchoService::NewStub(
channel);
621 EchoResponse recv_response;
624 ClientContext cli_ctx;
626 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
629 response_reader->Finish(&recv_response, &recv_status,
tag(4));
630 Verifier().Expect(4,
true).Verify(
cq.get());
633 EXPECT_EQ(
"", recv_status.error_message());
642 while (
cq->Next(&ignored_tag, &ignored_ok)) {
647 class ServerInterceptorsSyncUnimplementedEnd2endTest :
public ::testing::Test {
650 TEST_F(ServerInterceptorsSyncUnimplementedEnd2endTest, UnimplementedRpcTest) {
658 std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
660 creators.reserve(20);
661 for (
auto i = 0;
i < 20;
i++) {
662 creators.push_back(absl::make_unique<PhonyInterceptorFactory>());
667 ChannelArguments
args;
668 std::shared_ptr<Channel>
channel =
670 std::unique_ptr<grpc::testing::UnimplementedEchoService::Stub>
stub;
671 stub = grpc::testing::UnimplementedEchoService::NewStub(
channel);
673 EchoResponse recv_response;
675 ClientContext cli_ctx;
681 EXPECT_EQ(
"", recv_status.error_message());
694 int main(
int argc,
char** argv) {