cli_call.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. *
  3. * Copyright 2015 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/util/cli_call.h"
  19. #include <cmath>
  20. #include <iostream>
  21. #include <utility>
  22. #include <grpc/grpc.h>
  23. #include <grpc/slice.h>
  24. #include <grpc/support/log.h>
  25. #include <grpcpp/channel.h>
  26. #include <grpcpp/client_context.h>
  27. #include <grpcpp/support/byte_buffer.h>
  28. namespace grpc {
  29. namespace testing {
  30. namespace {
  31. void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  32. } // namespace
  33. Status CliCall::Call(const std::string& request, std::string* response,
  34. IncomingMetadataContainer* server_initial_metadata,
  35. IncomingMetadataContainer* server_trailing_metadata) {
  36. Write(request);
  37. WritesDone();
  38. if (!Read(response, server_initial_metadata)) {
  39. fprintf(stderr, "Failed to read response.\n");
  40. }
  41. return Finish(server_trailing_metadata);
  42. }
  43. CliCall::CliCall(const std::shared_ptr<grpc::Channel>& channel,
  44. const std::string& method,
  45. const OutgoingMetadataContainer& metadata, CliArgs args)
  46. : stub_(new grpc::GenericStub(channel)) {
  47. gpr_mu_init(&write_mu_);
  48. gpr_cv_init(&write_cv_);
  49. if (!metadata.empty()) {
  50. for (OutgoingMetadataContainer::const_iterator iter = metadata.begin();
  51. iter != metadata.end(); ++iter) {
  52. ctx_.AddMetadata(iter->first, iter->second);
  53. }
  54. }
  55. // Set deadline if timeout > 0 (default value -1 if no timeout specified)
  56. if (args.timeout > 0) {
  57. int64_t timeout_in_ns = ceil(args.timeout * 1e9);
  58. // Convert timeout (in nanoseconds) to a deadline
  59. auto deadline =
  60. gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
  61. gpr_time_from_nanos(timeout_in_ns, GPR_TIMESPAN));
  62. ctx_.set_deadline(deadline);
  63. } else if (args.timeout != -1) {
  64. fprintf(
  65. stderr,
  66. "WARNING: Non-positive timeout value, skipping setting deadline.\n");
  67. }
  68. call_ = stub_->PrepareCall(&ctx_, method, &cq_);
  69. call_->StartCall(tag(1));
  70. void* got_tag;
  71. bool ok;
  72. cq_.Next(&got_tag, &ok);
  73. GPR_ASSERT(ok);
  74. }
  75. CliCall::~CliCall() {
  76. gpr_cv_destroy(&write_cv_);
  77. gpr_mu_destroy(&write_mu_);
  78. }
  79. void CliCall::Write(const std::string& request) {
  80. void* got_tag;
  81. bool ok;
  82. gpr_slice s = gpr_slice_from_copied_buffer(request.data(), request.size());
  83. grpc::Slice req_slice(s, grpc::Slice::STEAL_REF);
  84. grpc::ByteBuffer send_buffer(&req_slice, 1);
  85. call_->Write(send_buffer, tag(2));
  86. cq_.Next(&got_tag, &ok);
  87. GPR_ASSERT(ok);
  88. }
  89. bool CliCall::Read(std::string* response,
  90. IncomingMetadataContainer* server_initial_metadata) {
  91. void* got_tag;
  92. bool ok;
  93. grpc::ByteBuffer recv_buffer;
  94. call_->Read(&recv_buffer, tag(3));
  95. if (!cq_.Next(&got_tag, &ok) || !ok) {
  96. return false;
  97. }
  98. std::vector<grpc::Slice> slices;
  99. GPR_ASSERT(recv_buffer.Dump(&slices).ok());
  100. response->clear();
  101. for (size_t i = 0; i < slices.size(); i++) {
  102. response->append(reinterpret_cast<const char*>(slices[i].begin()),
  103. slices[i].size());
  104. }
  105. if (server_initial_metadata) {
  106. *server_initial_metadata = ctx_.GetServerInitialMetadata();
  107. }
  108. return true;
  109. }
  110. void CliCall::WritesDone() {
  111. void* got_tag;
  112. bool ok;
  113. call_->WritesDone(tag(4));
  114. cq_.Next(&got_tag, &ok);
  115. GPR_ASSERT(ok);
  116. }
  117. void CliCall::WriteAndWait(const std::string& request) {
  118. grpc::Slice req_slice(request);
  119. grpc::ByteBuffer send_buffer(&req_slice, 1);
  120. gpr_mu_lock(&write_mu_);
  121. call_->Write(send_buffer, tag(2));
  122. write_done_ = false;
  123. while (!write_done_) {
  124. gpr_cv_wait(&write_cv_, &write_mu_, gpr_inf_future(GPR_CLOCK_MONOTONIC));
  125. }
  126. gpr_mu_unlock(&write_mu_);
  127. }
  128. void CliCall::WritesDoneAndWait() {
  129. gpr_mu_lock(&write_mu_);
  130. call_->WritesDone(tag(4));
  131. write_done_ = false;
  132. while (!write_done_) {
  133. gpr_cv_wait(&write_cv_, &write_mu_, gpr_inf_future(GPR_CLOCK_MONOTONIC));
  134. }
  135. gpr_mu_unlock(&write_mu_);
  136. }
  137. bool CliCall::ReadAndMaybeNotifyWrite(
  138. std::string* response, IncomingMetadataContainer* server_initial_metadata) {
  139. void* got_tag;
  140. bool ok;
  141. grpc::ByteBuffer recv_buffer;
  142. call_->Read(&recv_buffer, tag(3));
  143. bool cq_result = cq_.Next(&got_tag, &ok);
  144. while (got_tag != tag(3)) {
  145. gpr_mu_lock(&write_mu_);
  146. write_done_ = true;
  147. gpr_cv_signal(&write_cv_);
  148. gpr_mu_unlock(&write_mu_);
  149. cq_result = cq_.Next(&got_tag, &ok);
  150. if (got_tag == tag(2)) {
  151. GPR_ASSERT(ok);
  152. }
  153. }
  154. if (!cq_result || !ok) {
  155. // If the RPC is ended on the server side, we should still wait for the
  156. // pending write on the client side to be done.
  157. if (!ok) {
  158. gpr_mu_lock(&write_mu_);
  159. if (!write_done_) {
  160. cq_.Next(&got_tag, &ok);
  161. GPR_ASSERT(got_tag != tag(2));
  162. write_done_ = true;
  163. gpr_cv_signal(&write_cv_);
  164. }
  165. gpr_mu_unlock(&write_mu_);
  166. }
  167. return false;
  168. }
  169. std::vector<grpc::Slice> slices;
  170. GPR_ASSERT(recv_buffer.Dump(&slices).ok());
  171. response->clear();
  172. for (size_t i = 0; i < slices.size(); i++) {
  173. response->append(reinterpret_cast<const char*>(slices[i].begin()),
  174. slices[i].size());
  175. }
  176. if (server_initial_metadata) {
  177. *server_initial_metadata = ctx_.GetServerInitialMetadata();
  178. }
  179. return true;
  180. }
  181. Status CliCall::Finish(IncomingMetadataContainer* server_trailing_metadata) {
  182. void* got_tag;
  183. bool ok;
  184. grpc::Status status;
  185. call_->Finish(&status, tag(5));
  186. cq_.Next(&got_tag, &ok);
  187. GPR_ASSERT(ok);
  188. if (server_trailing_metadata) {
  189. *server_trailing_metadata = ctx_.GetServerTrailingMetadata();
  190. }
  191. return status;
  192. }
  193. } // namespace testing
  194. } // namespace grpc