cli_call.cc
Go to the documentation of this file.
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 
19 #include "test/cpp/util/cli_call.h"
20 
21 #include <cmath>
22 #include <iostream>
23 #include <utility>
24 
25 #include <grpc/grpc.h>
26 #include <grpc/slice.h>
27 #include <grpc/support/log.h>
28 #include <grpcpp/channel.h>
29 #include <grpcpp/client_context.h>
31 
32 namespace grpc {
33 namespace testing {
34 namespace {
35 void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
36 } // namespace
37 
39  IncomingMetadataContainer* server_initial_metadata,
40  IncomingMetadataContainer* server_trailing_metadata) {
41  Write(request);
42  WritesDone();
43  if (!Read(response, server_initial_metadata)) {
44  fprintf(stderr, "Failed to read response.\n");
45  }
46  return Finish(server_trailing_metadata);
47 }
48 
49 CliCall::CliCall(const std::shared_ptr<grpc::Channel>& channel,
50  const std::string& method,
55  if (!metadata.empty()) {
56  for (OutgoingMetadataContainer::const_iterator iter = metadata.begin();
57  iter != metadata.end(); ++iter) {
58  ctx_.AddMetadata(iter->first, iter->second);
59  }
60  }
61 
62  // Set deadline if timeout > 0 (default value -1 if no timeout specified)
63  if (args.timeout > 0) {
64  int64_t timeout_in_ns = ceil(args.timeout * 1e9);
65 
66  // Convert timeout (in nanoseconds) to a deadline
67  auto deadline =
69  gpr_time_from_nanos(timeout_in_ns, GPR_TIMESPAN));
70  ctx_.set_deadline(deadline);
71  } else if (args.timeout != -1) {
72  fprintf(
73  stderr,
74  "WARNING: Non-positive timeout value, skipping setting deadline.\n");
75  }
76 
77  call_ = stub_->PrepareCall(&ctx_, method, &cq_);
78  call_->StartCall(tag(1));
79  void* got_tag;
80  bool ok;
81  cq_.Next(&got_tag, &ok);
82  GPR_ASSERT(ok);
83 }
84 
88 }
89 
91  void* got_tag;
92  bool ok;
93 
95  grpc::Slice req_slice(s, grpc::Slice::STEAL_REF);
96  grpc::ByteBuffer send_buffer(&req_slice, 1);
97  call_->Write(send_buffer, tag(2));
98  cq_.Next(&got_tag, &ok);
99  GPR_ASSERT(ok);
100 }
101 
103  IncomingMetadataContainer* server_initial_metadata) {
104  void* got_tag;
105  bool ok;
106 
107  grpc::ByteBuffer recv_buffer;
108  call_->Read(&recv_buffer, tag(3));
109 
110  if (!cq_.Next(&got_tag, &ok) || !ok) {
111  return false;
112  }
113  std::vector<grpc::Slice> slices;
114  GPR_ASSERT(recv_buffer.Dump(&slices).ok());
115 
116  response->clear();
117  for (size_t i = 0; i < slices.size(); i++) {
118  response->append(reinterpret_cast<const char*>(slices[i].begin()),
119  slices[i].size());
120  }
121  if (server_initial_metadata) {
122  *server_initial_metadata = ctx_.GetServerInitialMetadata();
123  }
124  return true;
125 }
126 
128  void* got_tag;
129  bool ok;
130 
131  call_->WritesDone(tag(4));
132  cq_.Next(&got_tag, &ok);
133  GPR_ASSERT(ok);
134 }
135 
137  grpc::Slice req_slice(request);
138  grpc::ByteBuffer send_buffer(&req_slice, 1);
139 
141  call_->Write(send_buffer, tag(2));
142  write_done_ = false;
143  while (!write_done_) {
145  }
147 }
148 
151  call_->WritesDone(tag(4));
152  write_done_ = false;
153  while (!write_done_) {
155  }
157 }
158 
160  std::string* response, IncomingMetadataContainer* server_initial_metadata) {
161  void* got_tag;
162  bool ok;
163  grpc::ByteBuffer recv_buffer;
164 
165  call_->Read(&recv_buffer, tag(3));
166  bool cq_result = cq_.Next(&got_tag, &ok);
167 
168  while (got_tag != tag(3)) {
170  write_done_ = true;
173 
174  cq_result = cq_.Next(&got_tag, &ok);
175  if (got_tag == tag(2)) {
176  GPR_ASSERT(ok);
177  }
178  }
179 
180  if (!cq_result || !ok) {
181  // If the RPC is ended on the server side, we should still wait for the
182  // pending write on the client side to be done.
183  if (!ok) {
185  if (!write_done_) {
186  cq_.Next(&got_tag, &ok);
187  GPR_ASSERT(got_tag != tag(2));
188  write_done_ = true;
190  }
192  }
193  return false;
194  }
195 
196  std::vector<grpc::Slice> slices;
197  GPR_ASSERT(recv_buffer.Dump(&slices).ok());
198  response->clear();
199  for (size_t i = 0; i < slices.size(); i++) {
200  response->append(reinterpret_cast<const char*>(slices[i].begin()),
201  slices[i].size());
202  }
203  if (server_initial_metadata) {
204  *server_initial_metadata = ctx_.GetServerInitialMetadata();
205  }
206  return true;
207 }
208 
210  void* got_tag;
211  bool ok;
213 
214  call_->Finish(&status, tag(5));
215  cq_.Next(&got_tag, &ok);
216  GPR_ASSERT(ok);
217  if (server_trailing_metadata) {
218  *server_trailing_metadata = ctx_.GetServerTrailingMetadata();
219  }
220 
221  return status;
222 }
223 
224 } // namespace testing
225 } // namespace grpc
gpr_cv_signal
GPRAPI void gpr_cv_signal(gpr_cv *cv)
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
testing
Definition: aws_request_signer_test.cc:25
grpc::status
auto status
Definition: cpp/client/credentials_test.cc:200
grpc::testing::CliCall::write_mu_
gpr_mu write_mu_
Definition: cli_call.h:98
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
log.h
stub_
std::unique_ptr< grpc::testing::EchoTestService::Stub > stub_
Definition: client_channel_stress_test.cc:331
metadata
Definition: cq_verifier.cc:48
grpc::testing::CliCall::ReadAndMaybeNotifyWrite
bool ReadAndMaybeNotifyWrite(std::string *response, IncomingMetadataContainer *server_initial_metadata)
Definition: cli_call.cc:159
send_buffer
static char * send_buffer
Definition: test-tcp-writealot.c:38
grpc
Definition: grpcpp/alarm.h:33
begin
char * begin
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1007
grpc::Status::ok
bool ok() const
Is the status OK?
Definition: include/grpcpp/impl/codegen/status.h:126
slice.h
benchmark.request
request
Definition: benchmark.py:77
grpc::testing::CliCall::write_done_
bool write_done_
Definition: cli_call.h:100
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
cli_call.h
grpc::ClientContext::set_deadline
void set_deadline(const T &deadline)
Definition: grpcpp/impl/codegen/client_context.h:274
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
env.new
def new
Definition: env.py:51
grpc::CliArgs
Definition: cli_call.h:34
grpc::ClientContext::AddMetadata
void AddMetadata(const std::string &meta_key, const std::string &meta_value)
Definition: client_context.cc:121
grpc::testing::CliCall::~CliCall
~CliCall()
Definition: cli_call.cc:85
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
grpc::Slice::STEAL_REF
@ STEAL_REF
Definition: include/grpcpp/impl/codegen/slice.h:48
gpr_time_from_nanos
GPRAPI gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:111
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
grpc::testing::CliCall::CliCall
CliCall(const std::shared_ptr< grpc::Channel > &channel, const std::string &method, const OutgoingMetadataContainer &metadata, CliArgs args)
Definition: cli_call.cc:49
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
gpr_cv_destroy
GPRAPI void gpr_cv_destroy(gpr_cv *cv)
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
grpc.h
grpc::testing::CliCall::IncomingMetadataContainer
std::multimap< grpc::string_ref, grpc::string_ref > IncomingMetadataContainer
Definition: cli_call.h:47
grpc::testing::CliCall::WriteAndWait
void WriteAndWait(const std::string &request)
Definition: cli_call.cc:136
channel.h
gpr_cv_wait
GPRAPI int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)
grpc::ByteBuffer
A sequence of bytes.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:61
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
grpc::testing::CliCall::Write
void Write(const std::string &request)
Definition: cli_call.cc:90
grpc::ByteBuffer::Dump
Status Dump(std::vector< Slice > *slices) const
Dump (read) the buffer contents into slices.
Definition: byte_buffer_cc.cc:66
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
grpc::testing::tag
static void * tag(intptr_t t)
Definition: h2_ssl_cert_test.cc:263
grpc::testing::CliCall::Finish
Status Finish(IncomingMetadataContainer *server_trailing_metadata)
Definition: cli_call.cc:209
grpc::testing::CliCall::ctx_
grpc::ClientContext ctx_
Definition: cli_call.h:95
grpc::ClientContext::GetServerTrailingMetadata
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerTrailingMetadata() const
Definition: grpcpp/impl/codegen/client_context.h:262
grpc::testing::CliCall::call_
std::unique_ptr< grpc::GenericClientAsyncReaderWriter > call_
Definition: cli_call.h:96
grpc::CompletionQueue::Next
bool Next(void **tag, bool *ok)
Definition: include/grpcpp/impl/codegen/completion_queue.h:179
client_context.h
grpc::testing::CliCall::WritesDone
void WritesDone()
Definition: cli_call.cc:127
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
grpc::testing::CliCall::write_cv_
gpr_cv write_cv_
Definition: cli_call.h:99
gpr_slice_from_copied_buffer
#define gpr_slice_from_copied_buffer
Definition: gpr_slice.h:45
absl::str_format_internal::LengthMod::t
@ t
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::CliCall::cq_
grpc::CompletionQueue cq_
Definition: cli_call.h:97
slices
SliceBuffer * slices
Definition: retry_filter.cc:631
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
ok
bool ok
Definition: async_end2end_test.cc:197
grpc::testing::CliCall::OutgoingMetadataContainer
std::multimap< std::string, std::string > OutgoingMetadataContainer
Definition: cli_call.h:45
gpr_slice
#define gpr_slice
Definition: gpr_slice.h:35
grpc::TemplatedGenericStub
Definition: grpcpp/generic/generic_stub.h:45
iter
Definition: test_winkernel.cpp:47
grpc::ClientContext::GetServerInitialMetadata
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerInitialMetadata() const
Definition: grpcpp/impl/codegen/client_context.h:250
grpc::testing::CliCall::Read
bool Read(std::string *response, IncomingMetadataContainer *server_initial_metadata)
Definition: cli_call.cc:102
grpc::Slice
Definition: include/grpcpp/impl/codegen/slice.h:36
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
method
NSString * method
Definition: ProtoMethod.h:28
grpc::testing::CliCall::WritesDoneAndWait
void WritesDoneAndWait()
Definition: cli_call.cc:149
grpc::testing::CliCall::stub_
std::unique_ptr< grpc::GenericStub > stub_
Definition: cli_call.h:94
grpc::testing::CliCall::Call
Status Call(const std::string &request, std::string *response, IncomingMetadataContainer *server_initial_metadata, IncomingMetadataContainer *server_trailing_metadata)
Definition: cli_call.cc:38
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
gpr_cv_init
GPRAPI void gpr_cv_init(gpr_cv *cv)
byte_buffer.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:46