| 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 |
|
| 19 |
#include <condition_variable> |
| 20 |
|
| 21 |
#include <gtest/gtest.h> |
| 22 |
|
| 23 |
#include <grpcpp/channel.h> |
| 24 |
|
| 25 |
#include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 26 |
#include "test/cpp/util/string_ref_helper.h" |
| 27 |
|
| 28 |
namespace grpc { |
| 29 |
namespace testing { |
| 30 |
/* This interceptor does nothing. Just keeps a global count on the number of |
| 31 |
* times it was invoked. */ |
| 32 |
class PhonyInterceptor : public experimental::Interceptor { |
| 33 |
public: |
| 34 |
PhonyInterceptor() {} |
| 35 |
|
| 36 |
void Intercept(experimental::InterceptorBatchMethods* methods) override { |
| 37 |
if (methods->QueryInterceptionHookPoint( |
| 38 |
experimental::InterceptionHookPoints::PRE_SEND_INITIAL_METADATA)) { |
| 39 |
num_times_run_++; |
| 40 |
} else if (methods->QueryInterceptionHookPoint( |
| 41 |
experimental::InterceptionHookPoints:: |
| 42 |
POST_RECV_INITIAL_METADATA)) { |
| 43 |
num_times_run_reverse_++; |
| 44 |
} else if (methods->QueryInterceptionHookPoint( |
| 45 |
experimental::InterceptionHookPoints::PRE_SEND_CANCEL)) { |
| 46 |
num_times_cancel_++; |
| 47 |
} |
| 48 |
methods->Proceed(); |
| 49 |
} |
| 50 |
|
| 51 |
static void Reset() { |
| 52 |
num_times_run_.store(0); |
| 53 |
num_times_run_reverse_.store(0); |
| 54 |
num_times_cancel_.store(0); |
| 55 |
} |
| 56 |
|
| 57 |
static int GetNumTimesRun() { |
| 58 |
EXPECT_EQ(num_times_run_.load(), num_times_run_reverse_.load()); |
| 59 |
return num_times_run_.load(); |
| 60 |
} |
| 61 |
|
| 62 |
static int GetNumTimesCancel() { return num_times_cancel_.load(); } |
| 63 |
|
| 64 |
private: |
| 65 |
static std::atomic<int> num_times_run_; |
| 66 |
static std::atomic<int> num_times_run_reverse_; |
| 67 |
static std::atomic<int> num_times_cancel_; |
| 68 |
}; |
| 69 |
|
| 70 |
class PhonyInterceptorFactory |
| 71 |
: public experimental::ClientInterceptorFactoryInterface, |
| 72 |
public experimental::ServerInterceptorFactoryInterface { |
| 73 |
public: |
| 74 |
experimental::Interceptor* CreateClientInterceptor( |
| 75 |
experimental::ClientRpcInfo* /*info*/) override { |
| 76 |
return new PhonyInterceptor(); |
| 77 |
} |
| 78 |
|
| 79 |
experimental::Interceptor* CreateServerInterceptor( |
| 80 |
experimental::ServerRpcInfo* /*info*/) override { |
| 81 |
return new PhonyInterceptor(); |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
/* This interceptor can be used to test the interception mechanism. */ |
| 86 |
class TestInterceptor : public experimental::Interceptor { |
| 87 |
public: |
| 88 |
TestInterceptor(const std::string& method, const char* suffix_for_stats, |
| 89 |
experimental::ClientRpcInfo* info) { |
| 90 |
EXPECT_EQ(info->method(), method); |
| 91 |
|
| 92 |
if (suffix_for_stats == nullptr || info->suffix_for_stats() == nullptr) { |
| 93 |
EXPECT_EQ(info->suffix_for_stats(), suffix_for_stats); |
| 94 |
} else { |
| 95 |
EXPECT_EQ(strcmp(info->suffix_for_stats(), suffix_for_stats), 0); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void Intercept(experimental::InterceptorBatchMethods* methods) override { |
| 100 |
methods->Proceed(); |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
class TestInterceptorFactory |
| 105 |
: public experimental::ClientInterceptorFactoryInterface { |
| 106 |
public: |
| 107 |
TestInterceptorFactory(const std::string& method, |
| 108 |
const char* suffix_for_stats) |
| 109 |
: method_(method), suffix_for_stats_(suffix_for_stats) {} |
| 110 |
|
| 111 |
experimental::Interceptor* CreateClientInterceptor( |
| 112 |
experimental::ClientRpcInfo* info) override { |
| 113 |
return new TestInterceptor(method_, suffix_for_stats_, info); |
| 114 |
} |
| 115 |
|
| 116 |
private: |
| 117 |
std::string method_; |
| 118 |
const char* suffix_for_stats_; |
| 119 |
}; |
| 120 |
|
| 121 |
/* This interceptor factory returns nullptr on interceptor creation */ |
| 122 |
class NullInterceptorFactory |
| 123 |
: public experimental::ClientInterceptorFactoryInterface, |
| 124 |
public experimental::ServerInterceptorFactoryInterface { |
| 125 |
public: |
| 126 |
experimental::Interceptor* CreateClientInterceptor( |
| 127 |
experimental::ClientRpcInfo* /*info*/) override { |
| 128 |
return nullptr; |
| 129 |
} |
| 130 |
|
| 131 |
experimental::Interceptor* CreateServerInterceptor( |
| 132 |
experimental::ServerRpcInfo* /*info*/) override { |
| 133 |
return nullptr; |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
class EchoTestServiceStreamingImpl : public EchoTestService::Service { |
| 138 |
public: |
| 139 |
~EchoTestServiceStreamingImpl() override {} |
| 140 |
|
| 141 |
Status Echo(ServerContext* context, const EchoRequest* request, |
| 142 |
EchoResponse* response) override { |
| 143 |
auto client_metadata = context->client_metadata(); |
| 144 |
for (const auto& pair : client_metadata) { |
| 145 |
context->AddTrailingMetadata(ToString(pair.first), ToString(pair.second)); |
| 146 |
} |
| 147 |
response->set_message(request->message()); |
| 148 |
return Status::OK; |
| 149 |
} |
| 150 |
|
| 151 |
Status BidiStream( |
| 152 |
ServerContext* context, |
| 153 |
grpc::ServerReaderWriter<EchoResponse, EchoRequest>* stream) override { |
| 154 |
EchoRequest req; |
| 155 |
EchoResponse resp; |
| 156 |
auto client_metadata = context->client_metadata(); |
| 157 |
for (const auto& pair : client_metadata) { |
| 158 |
context->AddTrailingMetadata(ToString(pair.first), ToString(pair.second)); |
| 159 |
} |
| 160 |
|
| 161 |
while (stream->Read(&req)) { |
| 162 |
resp.set_message(req.message()); |
| 163 |
EXPECT_TRUE(stream->Write(resp, grpc::WriteOptions())); |
| 164 |
} |
| 165 |
return Status::OK; |
| 166 |
} |
| 167 |
|
| 168 |
Status RequestStream(ServerContext* context, |
| 169 |
ServerReader<EchoRequest>* reader, |
| 170 |
EchoResponse* resp) override { |
| 171 |
auto client_metadata = context->client_metadata(); |
| 172 |
for (const auto& pair : client_metadata) { |
| 173 |
context->AddTrailingMetadata(ToString(pair.first), ToString(pair.second)); |
| 174 |
} |
| 175 |
|
| 176 |
EchoRequest req; |
| 177 |
string response_str = ""; |
| 178 |
while (reader->Read(&req)) { |
| 179 |
response_str += req.message(); |
| 180 |
} |
| 181 |
resp->set_message(response_str); |
| 182 |
return Status::OK; |
| 183 |
} |
| 184 |
|
| 185 |
Status ResponseStream(ServerContext* context, const EchoRequest* req, |
| 186 |
ServerWriter<EchoResponse>* writer) override { |
| 187 |
auto client_metadata = context->client_metadata(); |
| 188 |
for (const auto& pair : client_metadata) { |
| 189 |
context->AddTrailingMetadata(ToString(pair.first), ToString(pair.second)); |
| 190 |
} |
| 191 |
|
| 192 |
EchoResponse resp; |
| 193 |
resp.set_message(req->message()); |
| 194 |
for (int i = 0; i < 10; i++) { |
| 195 |
EXPECT_TRUE(writer->Write(resp)); |
| 196 |
} |
| 197 |
return Status::OK; |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
constexpr int kNumStreamingMessages = 10; |
| 202 |
|
| 203 |
void MakeCall(const std::shared_ptr<Channel>& channel, |
| 204 |
const StubOptions& options = StubOptions()); |
| 205 |
|
| 206 |
void MakeClientStreamingCall(const std::shared_ptr<Channel>& channel); |
| 207 |
|
| 208 |
void MakeServerStreamingCall(const std::shared_ptr<Channel>& channel); |
| 209 |
|
| 210 |
void MakeBidiStreamingCall(const std::shared_ptr<Channel>& channel); |
| 211 |
|
| 212 |
void MakeAsyncCQCall(const std::shared_ptr<Channel>& channel); |
| 213 |
|
| 214 |
void MakeAsyncCQClientStreamingCall(const std::shared_ptr<Channel>& channel); |
| 215 |
|
| 216 |
void MakeAsyncCQServerStreamingCall(const std::shared_ptr<Channel>& channel); |
| 217 |
|
| 218 |
void MakeAsyncCQBidiStreamingCall(const std::shared_ptr<Channel>& channel); |
| 219 |
|
| 220 |
void MakeCallbackCall(const std::shared_ptr<Channel>& channel); |
| 221 |
|
| 222 |
bool CheckMetadata(const std::multimap<grpc::string_ref, grpc::string_ref>& map, |
| 223 |
const string& key, const string& value); |
| 224 |
|
| 225 |
bool CheckMetadata(const std::multimap<std::string, std::string>& map, |
| 226 |
const string& key, const string& value); |
| 227 |
|
| 228 |
std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> |
| 229 |
CreatePhonyClientInterceptors(); |
| 230 |
|
| 231 |
inline void* tag(int i) { return reinterpret_cast<void*>(i); } |
| 232 |
inline int detag(void* p) { |
| 233 |
return static_cast<int>(reinterpret_cast<intptr_t>(p)); |
| 234 |
} |
| 235 |
|
| 236 |
class Verifier { |
| 237 |
public: |
| 238 |
Verifier() : lambda_run_(false) {} |
| 239 |
// Expect sets the expected ok value for a specific tag |
| 240 |
Verifier& Expect(int i, bool expect_ok) { |
| 241 |
return ExpectUnless(i, expect_ok, false); |
| 242 |
} |
| 243 |
// ExpectUnless sets the expected ok value for a specific tag |
| 244 |
// unless the tag was already marked seen (as a result of ExpectMaybe) |
| 245 |
Verifier& ExpectUnless(int i, bool expect_ok, bool seen) { |
| 246 |
if (!seen) { |
| 247 |
expectations_[tag(i)] = expect_ok; |
| 248 |
} |
| 249 |
return *this; |
| 250 |
} |
| 251 |
// ExpectMaybe sets the expected ok value for a specific tag, but does not |
| 252 |
// require it to appear |
| 253 |
// If it does, sets *seen to true |
| 254 |
Verifier& ExpectMaybe(int i, bool expect_ok, bool* seen) { |
| 255 |
if (!*seen) { |
| 256 |
maybe_expectations_[tag(i)] = MaybeExpect{expect_ok, seen}; |
| 257 |
} |
| 258 |
return *this; |
| 259 |
} |
| 260 |
|
| 261 |
// Next waits for 1 async tag to complete, checks its |
| 262 |
// expectations, and returns the tag |
| 263 |
int Next(CompletionQueue* cq, bool ignore_ok) { |
| 264 |
bool ok; |
| 265 |
void* got_tag; |
| 266 |
EXPECT_TRUE(cq->Next(&got_tag, &ok)); |
| 267 |
GotTag(got_tag, ok, ignore_ok); |
| 268 |
return detag(got_tag); |
| 269 |
} |
| 270 |
|
| 271 |
template <typename T> |
| 272 |
CompletionQueue::NextStatus DoOnceThenAsyncNext( |
| 273 |
CompletionQueue* cq, void** got_tag, bool* ok, T deadline, |
| 274 |
std::function<void(void)> lambda) { |
| 275 |
if (lambda_run_) { |
| 276 |
return cq->AsyncNext(got_tag, ok, deadline); |
| 277 |
} else { |
| 278 |
lambda_run_ = true; |
| 279 |
return cq->DoThenAsyncNext(lambda, got_tag, ok, deadline); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// Verify keeps calling Next until all currently set |
| 284 |
// expected tags are complete |
| 285 |
void Verify(CompletionQueue* cq) { Verify(cq, false); } |
| 286 |
|
| 287 |
// This version of Verify allows optionally ignoring the |
| 288 |
// outcome of the expectation |
| 289 |
void Verify(CompletionQueue* cq, bool ignore_ok) { |
| 290 |
GPR_ASSERT(!expectations_.empty() || !maybe_expectations_.empty()); |
| 291 |
while (!expectations_.empty()) { |
| 292 |
Next(cq, ignore_ok); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
// This version of Verify stops after a certain deadline, and uses the |
| 297 |
// DoThenAsyncNext API |
| 298 |
// to call the lambda |
| 299 |
void Verify(CompletionQueue* cq, |
| 300 |
std::chrono::system_clock::time_point deadline, |
| 301 |
const std::function<void(void)>& lambda) { |
| 302 |
if (expectations_.empty()) { |
| 303 |
bool ok; |
| 304 |
void* got_tag; |
| 305 |
EXPECT_EQ(DoOnceThenAsyncNext(cq, &got_tag, &ok, deadline, lambda), |
| 306 |
CompletionQueue::TIMEOUT); |
| 307 |
} else { |
| 308 |
while (!expectations_.empty()) { |
| 309 |
bool ok; |
| 310 |
void* got_tag; |
| 311 |
EXPECT_EQ(DoOnceThenAsyncNext(cq, &got_tag, &ok, deadline, lambda), |
| 312 |
CompletionQueue::GOT_EVENT); |
| 313 |
GotTag(got_tag, ok, false); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
private: |
| 319 |
void GotTag(void* got_tag, bool ok, bool ignore_ok) { |
| 320 |
auto it = expectations_.find(got_tag); |
| 321 |
if (it != expectations_.end()) { |
| 322 |
if (!ignore_ok) { |
| 323 |
EXPECT_EQ(it->second, ok); |
| 324 |
} |
| 325 |
expectations_.erase(it); |
| 326 |
} else { |
| 327 |
auto it2 = maybe_expectations_.find(got_tag); |
| 328 |
if (it2 != maybe_expectations_.end()) { |
| 329 |
if (it2->second.seen != nullptr) { |
| 330 |
EXPECT_FALSE(*it2->second.seen); |
| 331 |
*it2->second.seen = true; |
| 332 |
} |
| 333 |
if (!ignore_ok) { |
| 334 |
EXPECT_EQ(it2->second.ok, ok); |
| 335 |
} |
| 336 |
} else { |
| 337 |
gpr_log(GPR_ERROR, "Unexpected tag: %p", got_tag); |
| 338 |
abort(); |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
struct MaybeExpect { |
| 344 |
bool ok; |
| 345 |
bool* seen; |
| 346 |
}; |
| 347 |
|
| 348 |
std::map<void*, bool> expectations_; |
| 349 |
std::map<void*, MaybeExpect> maybe_expectations_; |
| 350 |
bool lambda_run_; |
| 351 |
}; |
| 352 |
|
| 353 |
} // namespace testing |
| 354 |
} // namespace grpc |