Debian Codegraph @

test/cpp/util/cli_call.cc

raw
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>
30 #include <grpcpp/support/byte_buffer.h>
31
32 namespace grpc {
33 namespace testing {
34 namespace {
35 void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
36 } // namespace
37
38 Status CliCall::Call(const std::string& request, std::string* response,
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,
51 const OutgoingMetadataContainer& metadata, CliArgs args)
52 : stub_(new grpc::GenericStub(channel)) {
53 gpr_mu_init(&write_mu_);
54 gpr_cv_init(&write_cv_);
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 =
68 gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
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
85 CliCall::~CliCall() {
86 gpr_cv_destroy(&write_cv_);
87 gpr_mu_destroy(&write_mu_);
88 }
89
90 void CliCall::Write(const std::string& request) {
91 void* got_tag;
92 bool ok;
93
94 gpr_slice s = gpr_slice_from_copied_buffer(request.data(), request.size());
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
102 bool CliCall::Read(std::string* response,
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
127 void CliCall::WritesDone() {
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
136 void CliCall::WriteAndWait(const std::string& request) {
137 grpc::Slice req_slice(request);
138 grpc::ByteBuffer send_buffer(&req_slice, 1);
139
140 gpr_mu_lock(&write_mu_);
141 call_->Write(send_buffer, tag(2));
142 write_done_ = false;
143 while (!write_done_) {
144 gpr_cv_wait(&write_cv_, &write_mu_, gpr_inf_future(GPR_CLOCK_MONOTONIC));
145 }
146 gpr_mu_unlock(&write_mu_);
147 }
148
149 void CliCall::WritesDoneAndWait() {
150 gpr_mu_lock(&write_mu_);
151 call_->WritesDone(tag(4));
152 write_done_ = false;
153 while (!write_done_) {
154 gpr_cv_wait(&write_cv_, &write_mu_, gpr_inf_future(GPR_CLOCK_MONOTONIC));
155 }
156 gpr_mu_unlock(&write_mu_);
157 }
158
159 bool CliCall::ReadAndMaybeNotifyWrite(
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)) {
169 gpr_mu_lock(&write_mu_);
170 write_done_ = true;
171 gpr_cv_signal(&write_cv_);
172 gpr_mu_unlock(&write_mu_);
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) {
184 gpr_mu_lock(&write_mu_);
185 if (!write_done_) {
186 cq_.Next(&got_tag, &ok);
187 GPR_ASSERT(got_tag != tag(2));
188 write_done_ = true;
189 gpr_cv_signal(&write_cv_);
190 }
191 gpr_mu_unlock(&write_mu_);
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
209 Status CliCall::Finish(IncomingMetadataContainer* server_trailing_metadata) {
210 void* got_tag;
211 bool ok;
212 grpc::Status status;
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