client.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <chrono>
  34. #include <memory>
  35. #include <string>
  36. #include <thread>
  37. #include <grpc/grpc.h>
  38. #include <grpc/support/log.h>
  39. #include <google/gflags.h>
  40. #include <grpc++/channel_interface.h>
  41. #include <grpc++/client_context.h>
  42. #include <grpc++/create_channel.h>
  43. #include <grpc++/status.h>
  44. #include <grpc++/stream.h>
  45. #include "test/cpp/util/test_ssl_channel.h"
  46. #include "test/cpp/interop/test.pb.h"
  47. #include "test/cpp/interop/empty.pb.h"
  48. #include "test/cpp/interop/messages.pb.h"
  49. DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls.");
  50. DEFINE_int32(server_port, 0, "Server port.");
  51. DEFINE_string(server_host, "127.0.0.1", "Server host.");
  52. DEFINE_string(test_case, "large_unary",
  53. "Configure different test cases. Valid options are: "
  54. "empty_unary : empty (zero bytes) request and response; "
  55. "large_unary : single request and (large) response; "
  56. "client_streaming : request streaming with single response; "
  57. "server_streaming : single request with response streaming; "
  58. "slow_consumer : single request with response"
  59. " streaming with slow client consumer; "
  60. "half_duplex : half-duplex streaming;"
  61. "ping_pong : full-duplex streaming;"
  62. "all : all of above.");
  63. using grpc::ChannelInterface;
  64. using grpc::ClientContext;
  65. using grpc::CreateChannel;
  66. using grpc::TestSslChannel;
  67. using grpc::testing::ResponseParameters;
  68. using grpc::testing::SimpleRequest;
  69. using grpc::testing::SimpleResponse;
  70. using grpc::testing::StreamingInputCallRequest;
  71. using grpc::testing::StreamingInputCallResponse;
  72. using grpc::testing::StreamingOutputCallRequest;
  73. using grpc::testing::StreamingOutputCallResponse;
  74. using grpc::testing::TestService;
  75. namespace {
  76. // The same value is defined by the Java client.
  77. const std::vector<int> request_stream_sizes = {27182, 8, 1828, 45904};
  78. const std::vector<int> response_stream_sizes = {31415, 9, 2653, 58979};
  79. const int kNumResponseMessages = 2000;
  80. const int kResponseMessageSize = 1030;
  81. const int kReceiveDelayMilliSeconds = 20;
  82. } // namespace
  83. std::shared_ptr<ChannelInterface> CreateTestChannel(
  84. const grpc::string& server) {
  85. std::shared_ptr<ChannelInterface> channel;
  86. if (FLAGS_enable_ssl) {
  87. channel.reset(new TestSslChannel(server));
  88. } else {
  89. channel = CreateChannel(server);
  90. }
  91. return channel;
  92. }
  93. void DoEmpty(std::shared_ptr<ChannelInterface> channel) {
  94. gpr_log(GPR_INFO, "Sending an empty rpc...");
  95. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel));
  96. grpc::testing::Empty request = grpc::testing::Empty::default_instance();
  97. grpc::testing::Empty response = grpc::testing::Empty::default_instance();
  98. ClientContext context;
  99. grpc::Status s = stub->EmptyCall(&context, request, &response);
  100. GPR_ASSERT(s.IsOk());
  101. gpr_log(GPR_INFO, "Empty rpc done.");
  102. }
  103. void DoLargeUnary(std::shared_ptr<ChannelInterface> channel) {
  104. gpr_log(GPR_INFO, "Sending a large unary rpc...");
  105. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel));
  106. SimpleRequest request;
  107. SimpleResponse response;
  108. ClientContext context;
  109. request.set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
  110. request.set_response_size(314159);
  111. grpc::string payload(271828, '\0');
  112. request.mutable_payload()->set_body(payload.c_str(), 271828);
  113. grpc::Status s = stub->UnaryCall(&context, request, &response);
  114. GPR_ASSERT(s.IsOk());
  115. GPR_ASSERT(response.payload().type() ==
  116. grpc::testing::PayloadType::COMPRESSABLE);
  117. GPR_ASSERT(response.payload().body() == grpc::string(314159, '\0'));
  118. gpr_log(GPR_INFO, "Large unary done.");
  119. }
  120. int main(int argc, char** argv) {
  121. grpc_init();
  122. google::ParseCommandLineFlags(&argc, &argv, true);
  123. GPR_ASSERT(FLAGS_server_port);
  124. const int host_port_buf_size = 1024;
  125. char host_port[host_port_buf_size];
  126. snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(),
  127. FLAGS_server_port);
  128. if (FLAGS_test_case == "empty_unary") {
  129. DoEmpty(CreateTestChannel(host_port));
  130. } else if (FLAGS_test_case == "large_unary") {
  131. DoLargeUnary(CreateTestChannel(host_port));
  132. } else {
  133. gpr_log(
  134. GPR_ERROR,
  135. "Unsupported test case %s. Valid options are all|empty_unary|"
  136. "large_unary|client_streaming|server_streaming|half_duplex|ping_pong",
  137. FLAGS_test_case.c_str());
  138. }
  139. grpc_shutdown();
  140. return 0;
  141. }