interceptors_util.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "test/cpp/end2end/interceptors_util.h"
  19. namespace grpc {
  20. namespace testing {
  21. std::atomic<int> DummyInterceptor::num_times_run_;
  22. std::atomic<int> DummyInterceptor::num_times_run_reverse_;
  23. void MakeCall(const std::shared_ptr<Channel>& channel) {
  24. auto stub = grpc::testing::EchoTestService::NewStub(channel);
  25. ClientContext ctx;
  26. EchoRequest req;
  27. req.mutable_param()->set_echo_metadata(true);
  28. ctx.AddMetadata("testkey", "testvalue");
  29. req.set_message("Hello");
  30. EchoResponse resp;
  31. Status s = stub->Echo(&ctx, req, &resp);
  32. EXPECT_EQ(s.ok(), true);
  33. EXPECT_EQ(resp.message(), "Hello");
  34. }
  35. void MakeClientStreamingCall(const std::shared_ptr<Channel>& channel) {
  36. auto stub = grpc::testing::EchoTestService::NewStub(channel);
  37. ClientContext ctx;
  38. EchoRequest req;
  39. req.mutable_param()->set_echo_metadata(true);
  40. ctx.AddMetadata("testkey", "testvalue");
  41. req.set_message("Hello");
  42. EchoResponse resp;
  43. string expected_resp = "";
  44. auto writer = stub->RequestStream(&ctx, &resp);
  45. for (int i = 0; i < 10; i++) {
  46. writer->Write(req);
  47. expected_resp += "Hello";
  48. }
  49. writer->WritesDone();
  50. Status s = writer->Finish();
  51. EXPECT_EQ(s.ok(), true);
  52. EXPECT_EQ(resp.message(), expected_resp);
  53. }
  54. void MakeServerStreamingCall(const std::shared_ptr<Channel>& channel) {
  55. auto stub = grpc::testing::EchoTestService::NewStub(channel);
  56. ClientContext ctx;
  57. EchoRequest req;
  58. req.mutable_param()->set_echo_metadata(true);
  59. ctx.AddMetadata("testkey", "testvalue");
  60. req.set_message("Hello");
  61. EchoResponse resp;
  62. string expected_resp = "";
  63. auto reader = stub->ResponseStream(&ctx, req);
  64. int count = 0;
  65. while (reader->Read(&resp)) {
  66. EXPECT_EQ(resp.message(), "Hello");
  67. count++;
  68. }
  69. ASSERT_EQ(count, 10);
  70. Status s = reader->Finish();
  71. EXPECT_EQ(s.ok(), true);
  72. }
  73. void MakeBidiStreamingCall(const std::shared_ptr<Channel>& channel) {
  74. auto stub = grpc::testing::EchoTestService::NewStub(channel);
  75. ClientContext ctx;
  76. EchoRequest req;
  77. EchoResponse resp;
  78. ctx.AddMetadata("testkey", "testvalue");
  79. auto stream = stub->BidiStream(&ctx);
  80. for (auto i = 0; i < 10; i++) {
  81. req.set_message("Hello" + std::to_string(i));
  82. stream->Write(req);
  83. stream->Read(&resp);
  84. EXPECT_EQ(req.message(), resp.message());
  85. }
  86. ASSERT_TRUE(stream->WritesDone());
  87. Status s = stream->Finish();
  88. EXPECT_EQ(s.ok(), true);
  89. }
  90. void MakeCallbackCall(const std::shared_ptr<Channel>& channel) {
  91. auto stub = grpc::testing::EchoTestService::NewStub(channel);
  92. ClientContext ctx;
  93. EchoRequest req;
  94. std::mutex mu;
  95. std::condition_variable cv;
  96. bool done = false;
  97. req.mutable_param()->set_echo_metadata(true);
  98. ctx.AddMetadata("testkey", "testvalue");
  99. req.set_message("Hello");
  100. EchoResponse resp;
  101. stub->experimental_async()->Echo(&ctx, &req, &resp,
  102. [&resp, &mu, &done, &cv](Status s) {
  103. // gpr_log(GPR_ERROR, "got the callback");
  104. EXPECT_EQ(s.ok(), true);
  105. EXPECT_EQ(resp.message(), "Hello");
  106. std::lock_guard<std::mutex> l(mu);
  107. done = true;
  108. cv.notify_one();
  109. });
  110. std::unique_lock<std::mutex> l(mu);
  111. while (!done) {
  112. cv.wait(l);
  113. }
  114. }
  115. bool CheckMetadata(const std::multimap<grpc::string_ref, grpc::string_ref>& map,
  116. const string& key, const string& value) {
  117. for (const auto& pair : map) {
  118. if (pair.first.starts_with(key) && pair.second.starts_with(value)) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. } // namespace testing
  125. } // namespace grpc