client_callback_end2end_test.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 <functional>
  19. #include <mutex>
  20. #include <thread>
  21. #include <grpcpp/channel.h>
  22. #include <grpcpp/client_context.h>
  23. #include <grpcpp/create_channel.h>
  24. #include <grpcpp/generic/generic_stub.h>
  25. #include <grpcpp/impl/codegen/proto_utils.h>
  26. #include <grpcpp/server.h>
  27. #include <grpcpp/server_builder.h>
  28. #include <grpcpp/server_context.h>
  29. #include <grpcpp/support/client_callback.h>
  30. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  31. #include "test/core/util/test_config.h"
  32. #include "test/cpp/end2end/test_service_impl.h"
  33. #include "test/cpp/util/byte_buffer_proto_helper.h"
  34. #include <gtest/gtest.h>
  35. namespace grpc {
  36. namespace testing {
  37. namespace {
  38. class ClientCallbackEnd2endTest : public ::testing::Test {
  39. protected:
  40. ClientCallbackEnd2endTest() {}
  41. void SetUp() override {
  42. ServerBuilder builder;
  43. builder.RegisterService(&service_);
  44. server_ = builder.BuildAndStart();
  45. is_server_started_ = true;
  46. }
  47. void ResetStub() {
  48. ChannelArguments args;
  49. channel_ = server_->InProcessChannel(args);
  50. stub_ = grpc::testing::EchoTestService::NewStub(channel_);
  51. generic_stub_.reset(new GenericStub(channel_));
  52. }
  53. void TearDown() override {
  54. if (is_server_started_) {
  55. server_->Shutdown();
  56. }
  57. }
  58. void SendRpcs(int num_rpcs, bool with_binary_metadata) {
  59. grpc::string test_string("");
  60. for (int i = 0; i < num_rpcs; i++) {
  61. EchoRequest request;
  62. EchoResponse response;
  63. ClientContext cli_ctx;
  64. test_string += "Hello world. ";
  65. request.set_message(test_string);
  66. if (with_binary_metadata) {
  67. char bytes[8] = {'\0', '\1', '\2', '\3',
  68. '\4', '\5', '\6', static_cast<char>(i)};
  69. cli_ctx.AddMetadata("custom-bin", grpc::string(bytes, 8));
  70. }
  71. cli_ctx.set_compression_algorithm(GRPC_COMPRESS_GZIP);
  72. std::mutex mu;
  73. std::condition_variable cv;
  74. bool done = false;
  75. stub_->experimental_async()->Echo(
  76. &cli_ctx, &request, &response,
  77. [&request, &response, &done, &mu, &cv](Status s) {
  78. GPR_ASSERT(s.ok());
  79. EXPECT_EQ(request.message(), response.message());
  80. std::lock_guard<std::mutex> l(mu);
  81. done = true;
  82. cv.notify_one();
  83. });
  84. std::unique_lock<std::mutex> l(mu);
  85. while (!done) {
  86. cv.wait(l);
  87. }
  88. }
  89. }
  90. void SendRpcsGeneric(int num_rpcs, bool maybe_except) {
  91. const grpc::string kMethodName("/grpc.testing.EchoTestService/Echo");
  92. grpc::string test_string("");
  93. for (int i = 0; i < num_rpcs; i++) {
  94. EchoRequest request;
  95. std::unique_ptr<ByteBuffer> send_buf;
  96. ByteBuffer recv_buf;
  97. ClientContext cli_ctx;
  98. test_string += "Hello world. ";
  99. request.set_message(test_string);
  100. send_buf = SerializeToByteBuffer(&request);
  101. std::mutex mu;
  102. std::condition_variable cv;
  103. bool done = false;
  104. generic_stub_->experimental().UnaryCall(
  105. &cli_ctx, kMethodName, send_buf.get(), &recv_buf,
  106. [&request, &recv_buf, &done, &mu, &cv, maybe_except](Status s) {
  107. GPR_ASSERT(s.ok());
  108. EchoResponse response;
  109. EXPECT_TRUE(ParseFromByteBuffer(&recv_buf, &response));
  110. EXPECT_EQ(request.message(), response.message());
  111. std::lock_guard<std::mutex> l(mu);
  112. done = true;
  113. cv.notify_one();
  114. #if GRPC_ALLOW_EXCEPTIONS
  115. if (maybe_except) {
  116. throw - 1;
  117. }
  118. #endif
  119. });
  120. std::unique_lock<std::mutex> l(mu);
  121. while (!done) {
  122. cv.wait(l);
  123. }
  124. }
  125. }
  126. bool is_server_started_;
  127. std::shared_ptr<Channel> channel_;
  128. std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
  129. std::unique_ptr<grpc::GenericStub> generic_stub_;
  130. TestServiceImpl service_;
  131. std::unique_ptr<Server> server_;
  132. };
  133. TEST_F(ClientCallbackEnd2endTest, SimpleRpc) {
  134. ResetStub();
  135. SendRpcs(1, false);
  136. }
  137. TEST_F(ClientCallbackEnd2endTest, SequentialRpcs) {
  138. ResetStub();
  139. SendRpcs(10, false);
  140. }
  141. TEST_F(ClientCallbackEnd2endTest, SequentialRpcsWithVariedBinaryMetadataValue) {
  142. ResetStub();
  143. SendRpcs(10, true);
  144. }
  145. TEST_F(ClientCallbackEnd2endTest, SequentialGenericRpcs) {
  146. ResetStub();
  147. SendRpcsGeneric(10, false);
  148. }
  149. #if GRPC_ALLOW_EXCEPTIONS
  150. TEST_F(ClientCallbackEnd2endTest, ExceptingRpc) {
  151. ResetStub();
  152. SendRpcsGeneric(10, true);
  153. }
  154. #endif
  155. TEST_F(ClientCallbackEnd2endTest, MultipleRpcsWithVariedBinaryMetadataValue) {
  156. ResetStub();
  157. std::vector<std::thread> threads;
  158. threads.reserve(10);
  159. for (int i = 0; i < 10; ++i) {
  160. threads.emplace_back([this] { SendRpcs(10, true); });
  161. }
  162. for (int i = 0; i < 10; ++i) {
  163. threads[i].join();
  164. }
  165. }
  166. TEST_F(ClientCallbackEnd2endTest, MultipleRpcs) {
  167. ResetStub();
  168. std::vector<std::thread> threads;
  169. threads.reserve(10);
  170. for (int i = 0; i < 10; ++i) {
  171. threads.emplace_back([this] { SendRpcs(10, false); });
  172. }
  173. for (int i = 0; i < 10; ++i) {
  174. threads[i].join();
  175. }
  176. }
  177. TEST_F(ClientCallbackEnd2endTest, CancelRpcBeforeStart) {
  178. ResetStub();
  179. EchoRequest request;
  180. EchoResponse response;
  181. ClientContext context;
  182. request.set_message("hello");
  183. context.TryCancel();
  184. std::mutex mu;
  185. std::condition_variable cv;
  186. bool done = false;
  187. stub_->experimental_async()->Echo(
  188. &context, &request, &response, [&response, &done, &mu, &cv](Status s) {
  189. EXPECT_EQ("", response.message());
  190. EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
  191. std::lock_guard<std::mutex> l(mu);
  192. done = true;
  193. cv.notify_one();
  194. });
  195. std::unique_lock<std::mutex> l(mu);
  196. while (!done) {
  197. cv.wait(l);
  198. }
  199. }
  200. } // namespace
  201. } // namespace testing
  202. } // namespace grpc
  203. int main(int argc, char** argv) {
  204. grpc_test_init(argc, argv);
  205. ::testing::InitGoogleTest(&argc, argv);
  206. return RUN_ALL_TESTS();
  207. }