| 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 <cinttypes> |
| 20 |
#include <memory> |
| 21 |
#include <thread> |
| 22 |
|
| 23 |
#include "absl/memory/memory.h" |
| 24 |
#include "absl/strings/str_cat.h" |
| 25 |
|
| 26 |
#include <grpc/grpc.h> |
| 27 |
#include <grpc/support/alloc.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
#include <grpc/support/time.h> |
| 30 |
#include <grpcpp/channel.h> |
| 31 |
#include <grpcpp/client_context.h> |
| 32 |
#include <grpcpp/create_channel.h> |
| 33 |
#include <grpcpp/ext/health_check_service_server_builder_option.h> |
| 34 |
#include <grpcpp/server.h> |
| 35 |
#include <grpcpp/server_builder.h> |
| 36 |
#include <grpcpp/server_context.h> |
| 37 |
|
| 38 |
#include "src/core/ext/filters/client_channel/backup_poller.h" |
| 39 |
#include "src/core/lib/gprpp/debug_location.h" |
| 40 |
#include "src/core/lib/iomgr/port.h" |
| 41 |
#include "src/proto/grpc/health/v1/health.grpc.pb.h" |
| 42 |
#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 43 |
#include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 44 |
#include "test/core/util/build.h" |
| 45 |
#include "test/core/util/port.h" |
| 46 |
#include "test/core/util/test_config.h" |
| 47 |
#include "test/cpp/util/string_ref_helper.h" |
| 48 |
#include "test/cpp/util/test_credentials_provider.h" |
| 49 |
|
| 50 |
#ifdef GRPC_POSIX_SOCKET_EV |
| 51 |
#include "src/core/lib/iomgr/ev_posix.h" |
| 52 |
#endif // GRPC_POSIX_SOCKET_EV |
| 53 |
|
| 54 |
#include <gtest/gtest.h> |
| 55 |
|
| 56 |
using grpc::testing::EchoRequest; |
| 57 |
using grpc::testing::EchoResponse; |
| 58 |
using std::chrono::system_clock; |
| 59 |
|
| 60 |
namespace grpc { |
| 61 |
namespace testing { |
| 62 |
|
| 63 |
namespace { |
| 64 |
|
| 65 |
void* tag(int t) { return reinterpret_cast<void*>(t); } |
| 66 |
int detag(void* p) { return static_cast<int>(reinterpret_cast<intptr_t>(p)); } |
| 67 |
|
| 68 |
class Verifier { |
| 69 |
public: |
| 70 |
Verifier() : lambda_run_(false) {} |
| 71 |
// Expect sets the expected ok value for a specific tag |
| 72 |
Verifier& Expect( |
| 73 |
int i, bool expect_ok, |
| 74 |
grpc_core::SourceLocation whence = grpc_core::SourceLocation()) { |
| 75 |
return ExpectUnless(i, expect_ok, false, whence); |
| 76 |
} |
| 77 |
// ExpectUnless sets the expected ok value for a specific tag |
| 78 |
// unless the tag was already marked seen (as a result of ExpectMaybe) |
| 79 |
Verifier& ExpectUnless( |
| 80 |
int i, bool expect_ok, bool seen, |
| 81 |
grpc_core::SourceLocation whence = grpc_core::SourceLocation()) { |
| 82 |
if (!seen) { |
| 83 |
expectations_[tag(i)] = {expect_ok, whence}; |
| 84 |
} |
| 85 |
return *this; |
| 86 |
} |
| 87 |
// ExpectMaybe sets the expected ok value for a specific tag, but does not |
| 88 |
// require it to appear |
| 89 |
// If it does, sets *seen to true |
| 90 |
Verifier& ExpectMaybe( |
| 91 |
int i, bool expect_ok, bool* seen, |
| 92 |
grpc_core::SourceLocation whence = grpc_core::SourceLocation()) { |
| 93 |
if (!*seen) { |
| 94 |
maybe_expectations_[tag(i)] = MaybeExpect{expect_ok, seen, whence}; |
| 95 |
} |
| 96 |
return *this; |
| 97 |
} |
| 98 |
|
| 99 |
// Next waits for 1 async tag to complete, checks its |
| 100 |
// expectations, and returns the tag |
| 101 |
int Next(CompletionQueue* cq, bool ignore_ok) { |
| 102 |
bool ok; |
| 103 |
void* got_tag; |
| 104 |
EXPECT_TRUE(cq->Next(&got_tag, &ok)); |
| 105 |
GotTag(got_tag, ok, ignore_ok); |
| 106 |
return detag(got_tag); |
| 107 |
} |
| 108 |
|
| 109 |
template <typename T> |
| 110 |
CompletionQueue::NextStatus DoOnceThenAsyncNext( |
| 111 |
CompletionQueue* cq, void** got_tag, bool* ok, T deadline, |
| 112 |
std::function<void(void)> lambda) { |
| 113 |
if (lambda_run_) { |
| 114 |
return cq->AsyncNext(got_tag, ok, deadline); |
| 115 |
} else { |
| 116 |
lambda_run_ = true; |
| 117 |
return cq->DoThenAsyncNext(lambda, got_tag, ok, deadline); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
// Verify keeps calling Next until all currently set |
| 122 |
// expected tags are complete |
| 123 |
void Verify(CompletionQueue* cq) { Verify(cq, false); } |
| 124 |
|
| 125 |
// This version of Verify allows optionally ignoring the |
| 126 |
// outcome of the expectation |
| 127 |
void Verify(CompletionQueue* cq, bool ignore_ok) { |
| 128 |
GPR_ASSERT(!expectations_.empty() || !maybe_expectations_.empty()); |
| 129 |
while (!expectations_.empty()) { |
| 130 |
Next(cq, ignore_ok); |
| 131 |
} |
| 132 |
maybe_expectations_.clear(); |
| 133 |
} |
| 134 |
|
| 135 |
// This version of Verify stops after a certain deadline |
| 136 |
void Verify(CompletionQueue* cq, |
| 137 |
std::chrono::system_clock::time_point deadline) { |
| 138 |
if (expectations_.empty()) { |
| 139 |
bool ok; |
| 140 |
void* got_tag; |
| 141 |
EXPECT_EQ(cq->AsyncNext(&got_tag, &ok, deadline), |
| 142 |
CompletionQueue::TIMEOUT); |
| 143 |
} else { |
| 144 |
while (!expectations_.empty()) { |
| 145 |
bool ok; |
| 146 |
void* got_tag; |
| 147 |
EXPECT_EQ(cq->AsyncNext(&got_tag, &ok, deadline), |
| 148 |
CompletionQueue::GOT_EVENT); |
| 149 |
GotTag(got_tag, ok, false); |
| 150 |
} |
| 151 |
} |
| 152 |
maybe_expectations_.clear(); |
| 153 |
} |
| 154 |
|
| 155 |
// This version of Verify stops after a certain deadline, and uses the |
| 156 |
// DoThenAsyncNext API |
| 157 |
// to call the lambda |
| 158 |
void Verify(CompletionQueue* cq, |
| 159 |
std::chrono::system_clock::time_point deadline, |
| 160 |
const std::function<void(void)>& lambda) { |
| 161 |
if (expectations_.empty()) { |
| 162 |
bool ok; |
| 163 |
void* got_tag; |
| 164 |
EXPECT_EQ(DoOnceThenAsyncNext(cq, &got_tag, &ok, deadline, lambda), |
| 165 |
CompletionQueue::TIMEOUT); |
| 166 |
} else { |
| 167 |
while (!expectations_.empty()) { |
| 168 |
bool ok; |
| 169 |
void* got_tag; |
| 170 |
EXPECT_EQ(DoOnceThenAsyncNext(cq, &got_tag, &ok, deadline, lambda), |
| 171 |
CompletionQueue::GOT_EVENT); |
| 172 |
GotTag(got_tag, ok, false); |
| 173 |
} |
| 174 |
} |
| 175 |
maybe_expectations_.clear(); |
| 176 |
} |
| 177 |
|
| 178 |
private: |
| 179 |
void GotTag(void* got_tag, bool ok, bool ignore_ok) { |
| 180 |
auto it = expectations_.find(got_tag); |
| 181 |
if (it != expectations_.end()) { |
| 182 |
if (!ignore_ok) { |
| 183 |
EXPECT_EQ(it->second.ok, ok) << it->second.ToString(it->first); |
| 184 |
} |
| 185 |
expectations_.erase(it); |
| 186 |
} else { |
| 187 |
auto it2 = maybe_expectations_.find(got_tag); |
| 188 |
if (it2 != maybe_expectations_.end()) { |
| 189 |
if (it2->second.seen != nullptr) { |
| 190 |
EXPECT_FALSE(*it2->second.seen); |
| 191 |
*it2->second.seen = true; |
| 192 |
} |
| 193 |
if (!ignore_ok) { |
| 194 |
EXPECT_EQ(it2->second.ok, ok) << it->second.ToString(it->first); |
| 195 |
} |
| 196 |
maybe_expectations_.erase(it2); |
| 197 |
} else { |
| 198 |
gpr_log(GPR_ERROR, "Unexpected tag: %p", got_tag); |
| 199 |
abort(); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
struct MaybeExpect { |
| 205 |
bool ok; |
| 206 |
bool* seen; |
| 207 |
grpc_core::SourceLocation whence; |
| 208 |
std::string ToString(void* tag) const { |
| 209 |
return absl::StrCat( |
| 210 |
"[MaybeExpect] tag=", reinterpret_cast<uintptr_t>(tag), |
| 211 |
" expect_ok=", ok, " whence=", whence.file(), ":", whence.line()); |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
struct DefinitelyExpect { |
| 216 |
bool ok; |
| 217 |
grpc_core::SourceLocation whence; |
| 218 |
std::string ToString(void* tag) const { |
| 219 |
return absl::StrCat("[Expect] tag=", reinterpret_cast<uintptr_t>(tag), |
| 220 |
" expect_ok=", ok, " whence=", whence.file(), ":", |
| 221 |
whence.line()); |
| 222 |
} |
| 223 |
}; |
| 224 |
|
| 225 |
std::map<void*, DefinitelyExpect> expectations_; |
| 226 |
std::map<void*, MaybeExpect> maybe_expectations_; |
| 227 |
bool lambda_run_; |
| 228 |
}; |
| 229 |
|
| 230 |
bool plugin_has_sync_methods(std::unique_ptr<ServerBuilderPlugin>& plugin) { |
| 231 |
return plugin->has_sync_methods(); |
| 232 |
} |
| 233 |
|
| 234 |
// This class disables the server builder plugins that may add sync services to |
| 235 |
// the server. If there are sync services, UnimplementedRpc test will triger |
| 236 |
// the sync unknown rpc routine on the server side, rather than the async one |
| 237 |
// that needs to be tested here. |
| 238 |
class ServerBuilderSyncPluginDisabler : public grpc::ServerBuilderOption { |
| 239 |
public: |
| 240 |
void UpdateArguments(ChannelArguments* /*arg*/) override {} |
| 241 |
|
| 242 |
void UpdatePlugins( |
| 243 |
std::vector<std::unique_ptr<ServerBuilderPlugin>>* plugins) override { |
| 244 |
plugins->erase(std::remove_if(plugins->begin(), plugins->end(), |
| 245 |
plugin_has_sync_methods), |
| 246 |
plugins->end()); |
| 247 |
} |
| 248 |
}; |
| 249 |
|
| 250 |
class TestScenario { |
| 251 |
public: |
| 252 |
TestScenario(bool inproc_stub, const std::string& creds_type, bool hcs, |
| 253 |
const std::string& content) |
| 254 |
: inproc(inproc_stub), |
| 255 |
health_check_service(hcs), |
| 256 |
credentials_type(creds_type), |
| 257 |
message_content(content) {} |
| 258 |
void Log() const; |
| 259 |
bool inproc; |
| 260 |
bool health_check_service; |
| 261 |
const std::string credentials_type; |
| 262 |
const std::string message_content; |
| 263 |
}; |
| 264 |
|
| 265 |
std::ostream& operator<<(std::ostream& out, const TestScenario& scenario) { |
| 266 |
return out << "TestScenario{inproc=" << (scenario.inproc ? "true" : "false") |
| 267 |
<< ", credentials='" << scenario.credentials_type |
| 268 |
<< ", health_check_service=" |
| 269 |
<< (scenario.health_check_service ? "true" : "false") |
| 270 |
<< "', message_size=" << scenario.message_content.size() << "}"; |
| 271 |
} |
| 272 |
|
| 273 |
void TestScenario::Log() const { |
| 274 |
std::ostringstream out; |
| 275 |
out << *this; |
| 276 |
gpr_log(GPR_DEBUG, "%s", out.str().c_str()); |
| 277 |
} |
| 278 |
|
| 279 |
class HealthCheck : public health::v1::Health::Service {}; |
| 280 |
|
| 281 |
class AsyncEnd2endTest : public ::testing::TestWithParam<TestScenario> { |
| 282 |
protected: |
| 283 |
AsyncEnd2endTest() { GetParam().Log(); } |
| 284 |
|
| 285 |
void SetUp() override { |
| 286 |
port_ = grpc_pick_unused_port_or_die(); |
| 287 |
server_address_ << "localhost:" << port_; |
| 288 |
|
| 289 |
// Setup server |
| 290 |
BuildAndStartServer(); |
| 291 |
} |
| 292 |
|
| 293 |
void TearDown() override { |
| 294 |
stub_.reset(); |
| 295 |
ServerShutdown(); |
| 296 |
grpc_recycle_unused_port(port_); |
| 297 |
} |
| 298 |
|
| 299 |
void ServerShutdown() { |
| 300 |
std::thread t([this]() { |
| 301 |
void* ignored_tag; |
| 302 |
bool ignored_ok; |
| 303 |
while (cq_->Next(&ignored_tag, &ignored_ok)) { |
| 304 |
} |
| 305 |
}); |
| 306 |
server_->Shutdown(); |
| 307 |
cq_->Shutdown(); |
| 308 |
t.join(); |
| 309 |
} |
| 310 |
|
| 311 |
void BuildAndStartServer() { |
| 312 |
ServerBuilder builder; |
| 313 |
auto server_creds = GetCredentialsProvider()->GetServerCredentials( |
| 314 |
GetParam().credentials_type); |
| 315 |
builder.AddListeningPort(server_address_.str(), server_creds); |
| 316 |
service_ = std::make_unique<grpc::testing::EchoTestService::AsyncService>(); |
| 317 |
builder.RegisterService(service_.get()); |
| 318 |
if (GetParam().health_check_service) { |
| 319 |
builder.RegisterService(&health_check_); |
| 320 |
} |
| 321 |
cq_ = builder.AddCompletionQueue(); |
| 322 |
|
| 323 |
// TODO(zyc): make a test option to choose wheather sync plugins should be |
| 324 |
// deleted |
| 325 |
std::unique_ptr<ServerBuilderOption> sync_plugin_disabler( |
| 326 |
new ServerBuilderSyncPluginDisabler()); |
| 327 |
builder.SetOption(move(sync_plugin_disabler)); |
| 328 |
server_ = builder.BuildAndStart(); |
| 329 |
} |
| 330 |
|
| 331 |
void ResetStub() { |
| 332 |
ChannelArguments args; |
| 333 |
auto channel_creds = GetCredentialsProvider()->GetChannelCredentials( |
| 334 |
GetParam().credentials_type, &args); |
| 335 |
std::shared_ptr<Channel> channel = |
| 336 |
!(GetParam().inproc) ? grpc::CreateCustomChannel(server_address_.str(), |
| 337 |
channel_creds, args) |
| 338 |
: server_->InProcessChannel(args); |
| 339 |
stub_ = grpc::testing::EchoTestService::NewStub(channel); |
| 340 |
} |
| 341 |
|
| 342 |
void SendRpc(int num_rpcs) { |
| 343 |
for (int i = 0; i < num_rpcs; i++) { |
| 344 |
EchoRequest send_request; |
| 345 |
EchoRequest recv_request; |
| 346 |
EchoResponse send_response; |
| 347 |
EchoResponse recv_response; |
| 348 |
Status recv_status; |
| 349 |
|
| 350 |
ClientContext cli_ctx; |
| 351 |
ServerContext srv_ctx; |
| 352 |
grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 353 |
|
| 354 |
send_request.set_message(GetParam().message_content); |
| 355 |
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
| 356 |
stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 357 |
|
| 358 |
service_->RequestEcho(&srv_ctx, &recv_request, &response_writer, |
| 359 |
cq_.get(), cq_.get(), tag(2)); |
| 360 |
|
| 361 |
response_reader->Finish(&recv_response, &recv_status, tag(4)); |
| 362 |
|
| 363 |
Verifier().Expect(2, true).Verify(cq_.get()); |
| 364 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 365 |
|
| 366 |
send_response.set_message(recv_request.message()); |
| 367 |
response_writer.Finish(send_response, Status::OK, tag(3)); |
| 368 |
Verifier().Expect(3, true).Expect(4, true).Verify(cq_.get()); |
| 369 |
|
| 370 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 371 |
EXPECT_TRUE(recv_status.ok()); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
std::unique_ptr<ServerCompletionQueue> cq_; |
| 376 |
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
| 377 |
std::unique_ptr<Server> server_; |
| 378 |
std::unique_ptr<grpc::testing::EchoTestService::AsyncService> service_; |
| 379 |
HealthCheck health_check_; |
| 380 |
std::ostringstream server_address_; |
| 381 |
int port_; |
| 382 |
}; |
| 383 |
|
| 384 |
TEST_P(AsyncEnd2endTest, SimpleRpc) { |
| 385 |
ResetStub(); |
| 386 |
SendRpc(1); |
| 387 |
} |
| 388 |
|
| 389 |
TEST_P(AsyncEnd2endTest, SimpleRpcWithExpectedError) { |
| 390 |
ResetStub(); |
| 391 |
|
| 392 |
EchoRequest send_request; |
| 393 |
EchoRequest recv_request; |
| 394 |
EchoResponse send_response; |
| 395 |
EchoResponse recv_response; |
| 396 |
Status recv_status; |
| 397 |
|
| 398 |
ClientContext cli_ctx; |
| 399 |
ServerContext srv_ctx; |
| 400 |
grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 401 |
ErrorStatus error_status; |
| 402 |
|
| 403 |
send_request.set_message(GetParam().message_content); |
| 404 |
error_status.set_code(1); // CANCELLED |
| 405 |
error_status.set_error_message("cancel error message"); |
| 406 |
*send_request.mutable_param()->mutable_expected_error() = error_status; |
| 407 |
|
| 408 |
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
| 409 |
stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 410 |
|
| 411 |
srv_ctx.AsyncNotifyWhenDone(tag(5)); |
| 412 |
service_->RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 413 |
cq_.get(), tag(2)); |
| 414 |
|
| 415 |
response_reader->Finish(&recv_response, &recv_status, tag(4)); |
| 416 |
|
| 417 |
Verifier().Expect(2, true).Verify(cq_.get()); |
| 418 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 419 |
|
| 420 |
send_response.set_message(recv_request.message()); |
| 421 |
response_writer.Finish( |
| 422 |
send_response, |
| 423 |
Status( |
| 424 |
static_cast<StatusCode>(recv_request.param().expected_error().code()), |
| 425 |
recv_request.param().expected_error().error_message()), |
| 426 |
tag(3)); |
| 427 |
Verifier().Expect(3, true).Expect(4, true).Expect(5, true).Verify(cq_.get()); |
| 428 |
|
| 429 |
EXPECT_EQ(recv_response.message(), ""); |
| 430 |
EXPECT_EQ(recv_status.error_code(), error_status.code()); |
| 431 |
EXPECT_EQ(recv_status.error_message(), error_status.error_message()); |
| 432 |
EXPECT_FALSE(srv_ctx.IsCancelled()); |
| 433 |
} |
| 434 |
|
| 435 |
TEST_P(AsyncEnd2endTest, SequentialRpcs) { |
| 436 |
ResetStub(); |
| 437 |
SendRpc(10); |
| 438 |
} |
| 439 |
|
| 440 |
TEST_P(AsyncEnd2endTest, ReconnectChannel) { |
| 441 |
// GRPC_CLIENT_CHANNEL_BACKUP_POLL_INTERVAL_MS is set to 100ms in main() |
| 442 |
if (GetParam().inproc) { |
| 443 |
return; |
| 444 |
} |
| 445 |
int poller_slowdown_factor = 1; |
| 446 |
#ifdef GRPC_POSIX_SOCKET_EV |
| 447 |
// It needs 2 pollset_works to reconnect the channel with polling engine |
| 448 |
// "poll" |
| 449 |
grpc_core::UniquePtr<char> poller = GPR_GLOBAL_CONFIG_GET(grpc_poll_strategy); |
| 450 |
if (0 == strcmp(poller.get(), "poll")) { |
| 451 |
poller_slowdown_factor = 2; |
| 452 |
} |
| 453 |
#endif // GRPC_POSIX_SOCKET_EV |
| 454 |
ResetStub(); |
| 455 |
SendRpc(1); |
| 456 |
ServerShutdown(); |
| 457 |
BuildAndStartServer(); |
| 458 |
// It needs more than GRPC_CLIENT_CHANNEL_BACKUP_POLL_INTERVAL_MS time to |
| 459 |
// reconnect the channel. |
| 460 |
gpr_sleep_until(gpr_time_add( |
| 461 |
gpr_now(GPR_CLOCK_REALTIME), |
| 462 |
gpr_time_from_millis( |
| 463 |
300 * poller_slowdown_factor * grpc_test_slowdown_factor(), |
| 464 |
GPR_TIMESPAN))); |
| 465 |
SendRpc(1); |
| 466 |
} |
| 467 |
|
| 468 |
// We do not need to protect notify because the use is synchronized. |
| 469 |
void ServerWait(Server* server, int* notify) { |
| 470 |
server->Wait(); |
| 471 |
*notify = 1; |
| 472 |
} |
| 473 |
TEST_P(AsyncEnd2endTest, WaitAndShutdownTest) { |
| 474 |
int notify = 0; |
| 475 |
std::thread wait_thread(&ServerWait, server_.get(), ¬ify); |
| 476 |
ResetStub(); |
| 477 |
SendRpc(1); |
| 478 |
EXPECT_EQ(0, notify); |
| 479 |
ServerShutdown(); |
| 480 |
wait_thread.join(); |
| 481 |
EXPECT_EQ(1, notify); |
| 482 |
} |
| 483 |
|
| 484 |
TEST_P(AsyncEnd2endTest, ShutdownThenWait) { |
| 485 |
ResetStub(); |
| 486 |
SendRpc(1); |
| 487 |
std::thread t([this]() { ServerShutdown(); }); |
| 488 |
server_->Wait(); |
| 489 |
t.join(); |
| 490 |
} |
| 491 |
|
| 492 |
// Test a simple RPC using the async version of Next |
| 493 |
TEST_P(AsyncEnd2endTest, AsyncNextRpc) { |
| 494 |
ResetStub(); |
| 495 |
|
| 496 |
EchoRequest send_request; |
| 497 |
EchoRequest recv_request; |
| 498 |
EchoResponse send_response; |
| 499 |
EchoResponse recv_response; |
| 500 |
Status recv_status; |
| 501 |
|
| 502 |
ClientContext cli_ctx; |
| 503 |
ServerContext srv_ctx; |
| 504 |
grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 505 |
|
| 506 |
send_request.set_message(GetParam().message_content); |
| 507 |
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
| 508 |
stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 509 |
|
| 510 |
std::chrono::system_clock::time_point time_now( |
| 511 |
std::chrono::system_clock::now()); |
| 512 |
std::chrono::system_clock::time_point time_limit( |
| 513 |
std::chrono::system_clock::now() + std::chrono::seconds(10)); |
| 514 |
Verifier().Verify(cq_.get(), time_now); |
| 515 |
Verifier().Verify(cq_.get(), time_now); |
| 516 |
|
| 517 |
service_->RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 518 |
cq_.get(), tag(2)); |
| 519 |
response_reader->Finish(&recv_response, &recv_status, tag(4)); |
| 520 |
|
| 521 |
Verifier().Expect(2, true).Verify(cq_.get(), time_limit); |
| 522 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 523 |
|
| 524 |
send_response.set_message(recv_request.message()); |
| 525 |
response_writer.Finish(send_response, Status::OK, tag(3)); |
| 526 |
Verifier().Expect(3, true).Expect(4, true).Verify( |
| 527 |
cq_.get(), std::chrono::system_clock::time_point::max()); |
| 528 |
|
| 529 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 530 |
EXPECT_TRUE(recv_status.ok()); |
| 531 |
} |
| 532 |
|
| 533 |
// Test a simple RPC using the async version of Next |
| 534 |
TEST_P(AsyncEnd2endTest, DoThenAsyncNextRpc) { |
| 535 |
ResetStub(); |
| 536 |
|
| 537 |
EchoRequest send_request; |
| 538 |
EchoRequest recv_request; |
| 539 |
EchoResponse send_response; |
| 540 |
EchoResponse recv_response; |
| 541 |
Status recv_status; |
| 542 |
|
| 543 |
ClientContext cli_ctx; |
| 544 |
ServerContext srv_ctx; |
| 545 |
grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 546 |
|
| 547 |
send_request.set_message(GetParam().message_content); |
| 548 |
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
| 549 |
stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 550 |
|
| 551 |
std::chrono::system_clock::time_point time_now( |
| 552 |
std::chrono::system_clock::now()); |
| 553 |
std::chrono::system_clock::time_point time_limit( |
| 554 |
std::chrono::system_clock::now() + std::chrono::seconds(10)); |
| 555 |
Verifier().Verify(cq_.get(), time_now); |
| 556 |
Verifier().Verify(cq_.get(), time_now); |
| 557 |
|
| 558 |
auto resp_writer_ptr = &response_writer; |
| 559 |
auto lambda_2 = [&, this, resp_writer_ptr]() { |
| 560 |
service_->RequestEcho(&srv_ctx, &recv_request, resp_writer_ptr, cq_.get(), |
| 561 |
cq_.get(), tag(2)); |
| 562 |
}; |
| 563 |
response_reader->Finish(&recv_response, &recv_status, tag(4)); |
| 564 |
|
| 565 |
Verifier().Expect(2, true).Verify(cq_.get(), time_limit, lambda_2); |
| 566 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 567 |
|
| 568 |
send_response.set_message(recv_request.message()); |
| 569 |
auto lambda_3 = [resp_writer_ptr, send_response]() { |
| 570 |
resp_writer_ptr->Finish(send_response, Status::OK, tag(3)); |
| 571 |
}; |
| 572 |
Verifier().Expect(3, true).Expect(4, true).Verify( |
| 573 |
cq_.get(), std::chrono::system_clock::time_point::max(), lambda_3); |
| 574 |
|
| 575 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 576 |
EXPECT_TRUE(recv_status.ok()); |
| 577 |
} |
| 578 |
|
| 579 |
// Two pings and a final pong. |
| 580 |
TEST_P(AsyncEnd2endTest, SimpleClientStreaming) { |
| 581 |
ResetStub(); |
| 582 |
|
| 583 |
EchoRequest send_request; |
| 584 |
EchoRequest recv_request; |
| 585 |
EchoResponse send_response; |
| 586 |
EchoResponse recv_response; |
| 587 |
Status recv_status; |
| 588 |
ClientContext cli_ctx; |
| 589 |
ServerContext srv_ctx; |
| 590 |
ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 591 |
|
| 592 |
send_request.set_message(GetParam().message_content); |
| 593 |
std::unique_ptr<ClientAsyncWriter<EchoRequest>> cli_stream( |
| 594 |
stub_->AsyncRequestStream(&cli_ctx, &recv_response, cq_.get(), tag(1))); |
| 595 |
|
| 596 |
service_->RequestRequestStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 597 |
tag(2)); |
| 598 |
|
| 599 |
Verifier().Expect(2, true).Expect(1, true).Verify(cq_.get()); |
| 600 |
|
| 601 |
cli_stream->Write(send_request, tag(3)); |
| 602 |
srv_stream.Read(&recv_request, tag(4)); |
| 603 |
Verifier().Expect(3, true).Expect(4, true).Verify(cq_.get()); |
| 604 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 605 |
|
| 606 |
cli_stream->Write(send_request, tag(5)); |
| 607 |
srv_stream.Read(&recv_request, tag(6)); |
| 608 |
Verifier().Expect(5, true).Expect(6, true).Verify(cq_.get()); |
| 609 |
|
| 610 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 611 |
cli_stream->WritesDone(tag(7)); |
| 612 |
srv_stream.Read(&recv_request, tag(8)); |
| 613 |
Verifier().Expect(7, true).Expect(8, false).Verify(cq_.get()); |
| 614 |
|
| 615 |
send_response.set_message(recv_request.message()); |
| 616 |
srv_stream.Finish(send_response, Status::OK, tag(9)); |
| 617 |
cli_stream->Finish(&recv_status, tag(10)); |
| 618 |
Verifier().Expect(9, true).Expect(10, true).Verify(cq_.get()); |
| 619 |
|
| 620 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 621 |
EXPECT_TRUE(recv_status.ok()); |
| 622 |
} |
| 623 |
|
| 624 |
// Two pings and a final pong. |
| 625 |
TEST_P(AsyncEnd2endTest, SimpleClientStreamingWithCoalescingApi) { |
| 626 |
ResetStub(); |
| 627 |
|
| 628 |
EchoRequest send_request; |
| 629 |
EchoRequest recv_request; |
| 630 |
EchoResponse send_response; |
| 631 |
EchoResponse recv_response; |
| 632 |
Status recv_status; |
| 633 |
ClientContext cli_ctx; |
| 634 |
ServerContext srv_ctx; |
| 635 |
ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 636 |
|
| 637 |
send_request.set_message(GetParam().message_content); |
| 638 |
cli_ctx.set_initial_metadata_corked(true); |
| 639 |
// tag:1 never comes up since no op is performed |
| 640 |
std::unique_ptr<ClientAsyncWriter<EchoRequest>> cli_stream( |
| 641 |
stub_->AsyncRequestStream(&cli_ctx, &recv_response, cq_.get(), tag(1))); |
| 642 |
|
| 643 |
service_->RequestRequestStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 644 |
tag(2)); |
| 645 |
|
| 646 |
cli_stream->Write(send_request, tag(3)); |
| 647 |
|
| 648 |
bool seen3 = false; |
| 649 |
|
| 650 |
Verifier().Expect(2, true).ExpectMaybe(3, true, &seen3).Verify(cq_.get()); |
| 651 |
|
| 652 |
srv_stream.Read(&recv_request, tag(4)); |
| 653 |
|
| 654 |
Verifier().ExpectUnless(3, true, seen3).Expect(4, true).Verify(cq_.get()); |
| 655 |
|
| 656 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 657 |
|
| 658 |
cli_stream->WriteLast(send_request, WriteOptions(), tag(5)); |
| 659 |
srv_stream.Read(&recv_request, tag(6)); |
| 660 |
Verifier().Expect(5, true).Expect(6, true).Verify(cq_.get()); |
| 661 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 662 |
|
| 663 |
srv_stream.Read(&recv_request, tag(7)); |
| 664 |
Verifier().Expect(7, false).Verify(cq_.get()); |
| 665 |
|
| 666 |
send_response.set_message(recv_request.message()); |
| 667 |
srv_stream.Finish(send_response, Status::OK, tag(8)); |
| 668 |
cli_stream->Finish(&recv_status, tag(9)); |
| 669 |
Verifier().Expect(8, true).Expect(9, true).Verify(cq_.get()); |
| 670 |
|
| 671 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 672 |
EXPECT_TRUE(recv_status.ok()); |
| 673 |
} |
| 674 |
|
| 675 |
// One ping, two pongs. |
| 676 |
TEST_P(AsyncEnd2endTest, SimpleServerStreaming) { |
| 677 |
ResetStub(); |
| 678 |
|
| 679 |
EchoRequest send_request; |
| 680 |
EchoRequest recv_request; |
| 681 |
EchoResponse send_response; |
| 682 |
EchoResponse recv_response; |
| 683 |
Status recv_status; |
| 684 |
ClientContext cli_ctx; |
| 685 |
ServerContext srv_ctx; |
| 686 |
ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx); |
| 687 |
|
| 688 |
send_request.set_message(GetParam().message_content); |
| 689 |
std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream( |
| 690 |
stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1))); |
| 691 |
|
| 692 |
service_->RequestResponseStream(&srv_ctx, &recv_request, &srv_stream, |
| 693 |
cq_.get(), cq_.get(), tag(2)); |
| 694 |
|
| 695 |
Verifier().Expect(1, true).Expect(2, true).Verify(cq_.get()); |
| 696 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 697 |
|
| 698 |
send_response.set_message(recv_request.message()); |
| 699 |
srv_stream.Write(send_response, tag(3)); |
| 700 |
cli_stream->Read(&recv_response, tag(4)); |
| 701 |
Verifier().Expect(3, true).Expect(4, true).Verify(cq_.get()); |
| 702 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 703 |
|
| 704 |
srv_stream.Write(send_response, tag(5)); |
| 705 |
cli_stream->Read(&recv_response, tag(6)); |
| 706 |
Verifier().Expect(5, true).Expect(6, true).Verify(cq_.get()); |
| 707 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 708 |
|
| 709 |
srv_stream.Finish(Status::OK, tag(7)); |
| 710 |
cli_stream->Read(&recv_response, tag(8)); |
| 711 |
Verifier().Expect(7, true).Expect(8, false).Verify(cq_.get()); |
| 712 |
|
| 713 |
cli_stream->Finish(&recv_status, tag(9)); |
| 714 |
Verifier().Expect(9, true).Verify(cq_.get()); |
| 715 |
|
| 716 |
EXPECT_TRUE(recv_status.ok()); |
| 717 |
} |
| 718 |
|
| 719 |
// One ping, two pongs. Using WriteAndFinish API |
| 720 |
TEST_P(AsyncEnd2endTest, SimpleServerStreamingWithCoalescingApiWAF) { |
| 721 |
ResetStub(); |
| 722 |
|
| 723 |
EchoRequest send_request; |
| 724 |
EchoRequest recv_request; |
| 725 |
EchoResponse send_response; |
| 726 |
EchoResponse recv_response; |
| 727 |
Status recv_status; |
| 728 |
ClientContext cli_ctx; |
| 729 |
ServerContext srv_ctx; |
| 730 |
ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx); |
| 731 |
|
| 732 |
send_request.set_message(GetParam().message_content); |
| 733 |
std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream( |
| 734 |
stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1))); |
| 735 |
|
| 736 |
service_->RequestResponseStream(&srv_ctx, &recv_request, &srv_stream, |
| 737 |
cq_.get(), cq_.get(), tag(2)); |
| 738 |
|
| 739 |
Verifier().Expect(1, true).Expect(2, true).Verify(cq_.get()); |
| 740 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 741 |
|
| 742 |
send_response.set_message(recv_request.message()); |
| 743 |
srv_stream.Write(send_response, tag(3)); |
| 744 |
cli_stream->Read(&recv_response, tag(4)); |
| 745 |
Verifier().Expect(3, true).Expect(4, true).Verify(cq_.get()); |
| 746 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 747 |
|
| 748 |
srv_stream.WriteAndFinish(send_response, WriteOptions(), Status::OK, tag(5)); |
| 749 |
cli_stream->Read(&recv_response, tag(6)); |
| 750 |
Verifier().Expect(5, true).Expect(6, true).Verify(cq_.get()); |
| 751 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 752 |
|
| 753 |
cli_stream->Read(&recv_response, tag(7)); |
| 754 |
Verifier().Expect(7, false).Verify(cq_.get()); |
| 755 |
|
| 756 |
cli_stream->Finish(&recv_status, tag(8)); |
| 757 |
Verifier().Expect(8, true).Verify(cq_.get()); |
| 758 |
|
| 759 |
EXPECT_TRUE(recv_status.ok()); |
| 760 |
} |
| 761 |
|
| 762 |
// One ping, two pongs. Using WriteLast API |
| 763 |
TEST_P(AsyncEnd2endTest, SimpleServerStreamingWithCoalescingApiWL) { |
| 764 |
ResetStub(); |
| 765 |
|
| 766 |
EchoRequest send_request; |
| 767 |
EchoRequest recv_request; |
| 768 |
EchoResponse send_response; |
| 769 |
EchoResponse recv_response; |
| 770 |
Status recv_status; |
| 771 |
ClientContext cli_ctx; |
| 772 |
ServerContext srv_ctx; |
| 773 |
ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx); |
| 774 |
|
| 775 |
send_request.set_message(GetParam().message_content); |
| 776 |
std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream( |
| 777 |
stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1))); |
| 778 |
|
| 779 |
service_->RequestResponseStream(&srv_ctx, &recv_request, &srv_stream, |
| 780 |
cq_.get(), cq_.get(), tag(2)); |
| 781 |
|
| 782 |
Verifier().Expect(1, true).Expect(2, true).Verify(cq_.get()); |
| 783 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 784 |
|
| 785 |
send_response.set_message(recv_request.message()); |
| 786 |
srv_stream.Write(send_response, tag(3)); |
| 787 |
cli_stream->Read(&recv_response, tag(4)); |
| 788 |
Verifier().Expect(3, true).Expect(4, true).Verify(cq_.get()); |
| 789 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 790 |
|
| 791 |
srv_stream.WriteLast(send_response, WriteOptions(), tag(5)); |
| 792 |
cli_stream->Read(&recv_response, tag(6)); |
| 793 |
srv_stream.Finish(Status::OK, tag(7)); |
| 794 |
Verifier().Expect(5, true).Expect(6, true).Expect(7, true).Verify(cq_.get()); |
| 795 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 796 |
|
| 797 |
cli_stream->Read(&recv_response, tag(8)); |
| 798 |
Verifier().Expect(8, false).Verify(cq_.get()); |
| 799 |
|
| 800 |
cli_stream->Finish(&recv_status, tag(9)); |
| 801 |
Verifier().Expect(9, true).Verify(cq_.get()); |
| 802 |
|
| 803 |
EXPECT_TRUE(recv_status.ok()); |
| 804 |
} |
| 805 |
|
| 806 |
// One ping, one pong. |
| 807 |
TEST_P(AsyncEnd2endTest, SimpleBidiStreaming) { |
| 808 |
ResetStub(); |
| 809 |
|
| 810 |
EchoRequest send_request; |
| 811 |
EchoRequest recv_request; |
| 812 |
EchoResponse send_response; |
| 813 |
EchoResponse recv_response; |
| 814 |
Status recv_status; |
| 815 |
ClientContext cli_ctx; |
| 816 |
ServerContext srv_ctx; |
| 817 |
ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 818 |
|
| 819 |
send_request.set_message(GetParam().message_content); |
| 820 |
std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>> |
| 821 |
cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1))); |
| 822 |
|
| 823 |
service_->RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 824 |
tag(2)); |
| 825 |
|
| 826 |
Verifier().Expect(1, true).Expect(2, true).Verify(cq_.get()); |
| 827 |
|
| 828 |
cli_stream->Write(send_request, tag(3)); |
| 829 |
srv_stream.Read(&recv_request, tag(4)); |
| 830 |
Verifier().Expect(3, true).Expect(4, true).Verify(cq_.get()); |
| 831 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 832 |
|
| 833 |
send_response.set_message(recv_request.message()); |
| 834 |
srv_stream.Write(send_response, tag(5)); |
| 835 |
cli_stream->Read(&recv_response, tag(6)); |
| 836 |
Verifier().Expect(5, true).Expect(6, true).Verify(cq_.get()); |
| 837 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 838 |
|
| 839 |
cli_stream->WritesDone(tag(7)); |
| 840 |
srv_stream.Read(&recv_request, tag(8)); |
| 841 |
Verifier().Expect(7, true).Expect(8, false).Verify(cq_.get()); |
| 842 |
|
| 843 |
srv_stream.Finish(Status::OK, tag(9)); |
| 844 |
cli_stream->Finish(&recv_status, tag(10)); |
| 845 |
Verifier().Expect(9, true).Expect(10, true).Verify(cq_.get()); |
| 846 |
|
| 847 |
EXPECT_TRUE(recv_status.ok()); |
| 848 |
} |
| 849 |
|
| 850 |
// One ping, one pong. Using server:WriteAndFinish api |
| 851 |
TEST_P(AsyncEnd2endTest, SimpleBidiStreamingWithCoalescingApiWAF) { |
| 852 |
ResetStub(); |
| 853 |
|
| 854 |
EchoRequest send_request; |
| 855 |
EchoRequest recv_request; |
| 856 |
EchoResponse send_response; |
| 857 |
EchoResponse recv_response; |
| 858 |
Status recv_status; |
| 859 |
ClientContext cli_ctx; |
| 860 |
ServerContext srv_ctx; |
| 861 |
ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 862 |
|
| 863 |
send_request.set_message(GetParam().message_content); |
| 864 |
cli_ctx.set_initial_metadata_corked(true); |
| 865 |
std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>> |
| 866 |
cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1))); |
| 867 |
|
| 868 |
service_->RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 869 |
tag(2)); |
| 870 |
|
| 871 |
cli_stream->WriteLast(send_request, WriteOptions(), tag(3)); |
| 872 |
|
| 873 |
bool seen3 = false; |
| 874 |
|
| 875 |
Verifier().Expect(2, true).ExpectMaybe(3, true, &seen3).Verify(cq_.get()); |
| 876 |
|
| 877 |
srv_stream.Read(&recv_request, tag(4)); |
| 878 |
|
| 879 |
Verifier().ExpectUnless(3, true, seen3).Expect(4, true).Verify(cq_.get()); |
| 880 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 881 |
|
| 882 |
srv_stream.Read(&recv_request, tag(5)); |
| 883 |
Verifier().Expect(5, false).Verify(cq_.get()); |
| 884 |
|
| 885 |
send_response.set_message(recv_request.message()); |
| 886 |
srv_stream.WriteAndFinish(send_response, WriteOptions(), Status::OK, tag(6)); |
| 887 |
cli_stream->Read(&recv_response, tag(7)); |
| 888 |
Verifier().Expect(6, true).Expect(7, true).Verify(cq_.get()); |
| 889 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 890 |
|
| 891 |
cli_stream->Finish(&recv_status, tag(8)); |
| 892 |
Verifier().Expect(8, true).Verify(cq_.get()); |
| 893 |
|
| 894 |
EXPECT_TRUE(recv_status.ok()); |
| 895 |
} |
| 896 |
|
| 897 |
// One ping, one pong. Using server:WriteLast api |
| 898 |
TEST_P(AsyncEnd2endTest, SimpleBidiStreamingWithCoalescingApiWL) { |
| 899 |
ResetStub(); |
| 900 |
|
| 901 |
EchoRequest send_request; |
| 902 |
EchoRequest recv_request; |
| 903 |
EchoResponse send_response; |
| 904 |
EchoResponse recv_response; |
| 905 |
Status recv_status; |
| 906 |
ClientContext cli_ctx; |
| 907 |
ServerContext srv_ctx; |
| 908 |
ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 909 |
|
| 910 |
send_request.set_message(GetParam().message_content); |
| 911 |
cli_ctx.set_initial_metadata_corked(true); |
| 912 |
std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>> |
| 913 |
cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1))); |
| 914 |
|
| 915 |
service_->RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 916 |
tag(2)); |
| 917 |
|
| 918 |
cli_stream->WriteLast(send_request, WriteOptions(), tag(3)); |
| 919 |
|
| 920 |
bool seen3 = false; |
| 921 |
|
| 922 |
Verifier().Expect(2, true).ExpectMaybe(3, true, &seen3).Verify(cq_.get()); |
| 923 |
|
| 924 |
srv_stream.Read(&recv_request, tag(4)); |
| 925 |
|
| 926 |
Verifier().ExpectUnless(3, true, seen3).Expect(4, true).Verify(cq_.get()); |
| 927 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 928 |
|
| 929 |
srv_stream.Read(&recv_request, tag(5)); |
| 930 |
Verifier().Expect(5, false).Verify(cq_.get()); |
| 931 |
|
| 932 |
send_response.set_message(recv_request.message()); |
| 933 |
srv_stream.WriteLast(send_response, WriteOptions(), tag(6)); |
| 934 |
srv_stream.Finish(Status::OK, tag(7)); |
| 935 |
cli_stream->Read(&recv_response, tag(8)); |
| 936 |
Verifier().Expect(6, true).Expect(7, true).Expect(8, true).Verify(cq_.get()); |
| 937 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 938 |
|
| 939 |
cli_stream->Finish(&recv_status, tag(9)); |
| 940 |
Verifier().Expect(9, true).Verify(cq_.get()); |
| 941 |
|
| 942 |
EXPECT_TRUE(recv_status.ok()); |
| 943 |
} |
| 944 |
|
| 945 |
// Metadata tests |
| 946 |
TEST_P(AsyncEnd2endTest, ClientInitialMetadataRpc) { |
| 947 |
ResetStub(); |
| 948 |
|
| 949 |
EchoRequest send_request; |
| 950 |
EchoRequest recv_request; |
| 951 |
EchoResponse send_response; |
| 952 |
EchoResponse recv_response; |
| 953 |
Status recv_status; |
| 954 |
|
| 955 |
ClientContext cli_ctx; |
| 956 |
ServerContext srv_ctx; |
| 957 |
grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 958 |
|
| 959 |
send_request.set_message(GetParam().message_content); |
| 960 |
std::pair<std::string, std::string> meta1("key1", "val1"); |
| 961 |
std::pair<std::string, std::string> meta2("key2", "val2"); |
| 962 |
std::pair<std::string, std::string> meta3("g.r.d-bin", "xyz"); |
| 963 |
cli_ctx.AddMetadata(meta1.first, meta1.second); |
| 964 |
cli_ctx.AddMetadata(meta2.first, meta2.second); |
| 965 |
cli_ctx.AddMetadata(meta3.first, meta3.second); |
| 966 |
|
| 967 |
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
| 968 |
stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 969 |
response_reader->Finish(&recv_response, &recv_status, tag(4)); |
| 970 |
|
| 971 |
service_->RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 972 |
cq_.get(), tag(2)); |
| 973 |
Verifier().Expect(2, true).Verify(cq_.get()); |
| 974 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 975 |
const auto& client_initial_metadata = srv_ctx.client_metadata(); |
| 976 |
EXPECT_EQ(meta1.second, |
| 977 |
ToString(client_initial_metadata.find(meta1.first)->second)); |
| 978 |
EXPECT_EQ(meta2.second, |
| 979 |
ToString(client_initial_metadata.find(meta2.first)->second)); |
| 980 |
EXPECT_EQ(meta3.second, |
| 981 |
ToString(client_initial_metadata.find(meta3.first)->second)); |
| 982 |
EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2)); |
| 983 |
|
| 984 |
send_response.set_message(recv_request.message()); |
| 985 |
response_writer.Finish(send_response, Status::OK, tag(3)); |
| 986 |
Verifier().Expect(3, true).Expect(4, true).Verify(cq_.get()); |
| 987 |
|
| 988 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 989 |
EXPECT_TRUE(recv_status.ok()); |
| 990 |
} |
| 991 |
|
| 992 |
TEST_P(AsyncEnd2endTest, ServerInitialMetadataRpc) { |
| 993 |
ResetStub(); |
| 994 |
|
| 995 |
EchoRequest send_request; |
| 996 |
EchoRequest recv_request; |
| 997 |
EchoResponse send_response; |
| 998 |
EchoResponse recv_response; |
| 999 |
Status recv_status; |
| 1000 |
|
| 1001 |
ClientContext cli_ctx; |
| 1002 |
ServerContext srv_ctx; |
| 1003 |
grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 1004 |
|
| 1005 |
send_request.set_message(GetParam().message_content); |
| 1006 |
std::pair<std::string, std::string> meta1("key1", "val1"); |
| 1007 |
std::pair<std::string, std::string> meta2("key2", "val2"); |
| 1008 |
|
| 1009 |
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
| 1010 |
stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 1011 |
response_reader->ReadInitialMetadata(tag(4)); |
| 1012 |
|
| 1013 |
service_->RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 1014 |
cq_.get(), tag(2)); |
| 1015 |
Verifier().Expect(2, true).Verify(cq_.get()); |
| 1016 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 1017 |
srv_ctx.AddInitialMetadata(meta1.first, meta1.second); |
| 1018 |
srv_ctx.AddInitialMetadata(meta2.first, meta2.second); |
| 1019 |
response_writer.SendInitialMetadata(tag(3)); |
| 1020 |
Verifier().Expect(3, true).Expect(4, true).Verify(cq_.get()); |
| 1021 |
const auto& server_initial_metadata = cli_ctx.GetServerInitialMetadata(); |
| 1022 |
EXPECT_EQ(meta1.second, |
| 1023 |
ToString(server_initial_metadata.find(meta1.first)->second)); |
| 1024 |
EXPECT_EQ(meta2.second, |
| 1025 |
ToString(server_initial_metadata.find(meta2.first)->second)); |
| 1026 |
EXPECT_EQ(static_cast<size_t>(2), server_initial_metadata.size()); |
| 1027 |
|
| 1028 |
send_response.set_message(recv_request.message()); |
| 1029 |
response_writer.Finish(send_response, Status::OK, tag(5)); |
| 1030 |
response_reader->Finish(&recv_response, &recv_status, tag(6)); |
| 1031 |
Verifier().Expect(5, true).Expect(6, true).Verify(cq_.get()); |
| 1032 |
|
| 1033 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 1034 |
EXPECT_TRUE(recv_status.ok()); |
| 1035 |
} |
| 1036 |
|
| 1037 |
// 1 ping, 2 pongs. |
| 1038 |
TEST_P(AsyncEnd2endTest, ServerInitialMetadataServerStreaming) { |
| 1039 |
ResetStub(); |
| 1040 |
EchoRequest send_request; |
| 1041 |
EchoRequest recv_request; |
| 1042 |
EchoResponse send_response; |
| 1043 |
EchoResponse recv_response; |
| 1044 |
Status recv_status; |
| 1045 |
ClientContext cli_ctx; |
| 1046 |
ServerContext srv_ctx; |
| 1047 |
ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx); |
| 1048 |
|
| 1049 |
std::pair<::std::string, ::std::string> meta1("key1", "val1"); |
| 1050 |
std::pair<::std::string, ::std::string> meta2("key2", "val2"); |
| 1051 |
|
| 1052 |
std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream( |
| 1053 |
stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1))); |
| 1054 |
cli_stream->ReadInitialMetadata(tag(11)); |
| 1055 |
service_->RequestResponseStream(&srv_ctx, &recv_request, &srv_stream, |
| 1056 |
cq_.get(), cq_.get(), tag(2)); |
| 1057 |
|
| 1058 |
Verifier().Expect(1, true).Expect(2, true).Verify(cq_.get()); |
| 1059 |
|
| 1060 |
srv_ctx.AddInitialMetadata(meta1.first, meta1.second); |
| 1061 |
srv_ctx.AddInitialMetadata(meta2.first, meta2.second); |
| 1062 |
srv_stream.SendInitialMetadata(tag(10)); |
| 1063 |
Verifier().Expect(10, true).Expect(11, true).Verify(cq_.get()); |
| 1064 |
auto server_initial_metadata = cli_ctx.GetServerInitialMetadata(); |
| 1065 |
EXPECT_EQ(meta1.second, |
| 1066 |
ToString(server_initial_metadata.find(meta1.first)->second)); |
| 1067 |
EXPECT_EQ(meta2.second, |
| 1068 |
ToString(server_initial_metadata.find(meta2.first)->second)); |
| 1069 |
EXPECT_EQ(static_cast<size_t>(2), server_initial_metadata.size()); |
| 1070 |
|
| 1071 |
srv_stream.Write(send_response, tag(3)); |
| 1072 |
|
| 1073 |
cli_stream->Read(&recv_response, tag(4)); |
| 1074 |
Verifier().Expect(3, true).Expect(4, true).Verify(cq_.get()); |
| 1075 |
|
| 1076 |
srv_stream.Write(send_response, tag(5)); |
| 1077 |
cli_stream->Read(&recv_response, tag(6)); |
| 1078 |
Verifier().Expect(5, true).Expect(6, true).Verify(cq_.get()); |
| 1079 |
|
| 1080 |
srv_stream.Finish(Status::OK, tag(7)); |
| 1081 |
cli_stream->Read(&recv_response, tag(8)); |
| 1082 |
Verifier().Expect(7, true).Expect(8, false).Verify(cq_.get()); |
| 1083 |
|
| 1084 |
cli_stream->Finish(&recv_status, tag(9)); |
| 1085 |
Verifier().Expect(9, true).Verify(cq_.get()); |
| 1086 |
|
| 1087 |
EXPECT_TRUE(recv_status.ok()); |
| 1088 |
} |
| 1089 |
|
| 1090 |
// 1 ping, 2 pongs. |
| 1091 |
// Test for server initial metadata being sent implicitly |
| 1092 |
TEST_P(AsyncEnd2endTest, ServerInitialMetadataServerStreamingImplicit) { |
| 1093 |
ResetStub(); |
| 1094 |
EchoRequest send_request; |
| 1095 |
EchoRequest recv_request; |
| 1096 |
EchoResponse send_response; |
| 1097 |
EchoResponse recv_response; |
| 1098 |
Status recv_status; |
| 1099 |
ClientContext cli_ctx; |
| 1100 |
ServerContext srv_ctx; |
| 1101 |
ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx); |
| 1102 |
|
| 1103 |
send_request.set_message(GetParam().message_content); |
| 1104 |
std::pair<::std::string, ::std::string> meta1("key1", "val1"); |
| 1105 |
std::pair<::std::string, ::std::string> meta2("key2", "val2"); |
| 1106 |
|
| 1107 |
std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream( |
| 1108 |
stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1))); |
| 1109 |
service_->RequestResponseStream(&srv_ctx, &recv_request, &srv_stream, |
| 1110 |
cq_.get(), cq_.get(), tag(2)); |
| 1111 |
|
| 1112 |
Verifier().Expect(1, true).Expect(2, true).Verify(cq_.get()); |
| 1113 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 1114 |
|
| 1115 |
srv_ctx.AddInitialMetadata(meta1.first, meta1.second); |
| 1116 |
srv_ctx.AddInitialMetadata(meta2.first, meta2.second); |
| 1117 |
send_response.set_message(recv_request.message()); |
| 1118 |
srv_stream.Write(send_response, tag(3)); |
| 1119 |
|
| 1120 |
cli_stream->Read(&recv_response, tag(4)); |
| 1121 |
Verifier().Expect(3, true).Expect(4, true).Verify(cq_.get()); |
| 1122 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 1123 |
|
| 1124 |
auto server_initial_metadata = cli_ctx.GetServerInitialMetadata(); |
| 1125 |
EXPECT_EQ(meta1.second, |
| 1126 |
ToString(server_initial_metadata.find(meta1.first)->second)); |
| 1127 |
EXPECT_EQ(meta2.second, |
| 1128 |
ToString(server_initial_metadata.find(meta2.first)->second)); |
| 1129 |
EXPECT_EQ(static_cast<size_t>(2), server_initial_metadata.size()); |
| 1130 |
|
| 1131 |
srv_stream.Write(send_response, tag(5)); |
| 1132 |
cli_stream->Read(&recv_response, tag(6)); |
| 1133 |
Verifier().Expect(5, true).Expect(6, true).Verify(cq_.get()); |
| 1134 |
|
| 1135 |
srv_stream.Finish(Status::OK, tag(7)); |
| 1136 |
cli_stream->Read(&recv_response, tag(8)); |
| 1137 |
Verifier().Expect(7, true).Expect(8, false).Verify(cq_.get()); |
| 1138 |
|
| 1139 |
cli_stream->Finish(&recv_status, tag(9)); |
| 1140 |
Verifier().Expect(9, true).Verify(cq_.get()); |
| 1141 |
|
| 1142 |
EXPECT_TRUE(recv_status.ok()); |
| 1143 |
} |
| 1144 |
|
| 1145 |
TEST_P(AsyncEnd2endTest, ServerTrailingMetadataRpc) { |
| 1146 |
ResetStub(); |
| 1147 |
|
| 1148 |
EchoRequest send_request; |
| 1149 |
EchoRequest recv_request; |
| 1150 |
EchoResponse send_response; |
| 1151 |
EchoResponse recv_response; |
| 1152 |
Status recv_status; |
| 1153 |
|
| 1154 |
ClientContext cli_ctx; |
| 1155 |
ServerContext srv_ctx; |
| 1156 |
grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 1157 |
|
| 1158 |
send_request.set_message(GetParam().message_content); |
| 1159 |
std::pair<std::string, std::string> meta1("key1", "val1"); |
| 1160 |
std::pair<std::string, std::string> meta2("key2", "val2"); |
| 1161 |
|
| 1162 |
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
| 1163 |
stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 1164 |
response_reader->Finish(&recv_response, &recv_status, tag(5)); |
| 1165 |
|
| 1166 |
service_->RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 1167 |
cq_.get(), tag(2)); |
| 1168 |
Verifier().Expect(2, true).Verify(cq_.get()); |
| 1169 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 1170 |
response_writer.SendInitialMetadata(tag(3)); |
| 1171 |
Verifier().Expect(3, true).Verify(cq_.get()); |
| 1172 |
|
| 1173 |
send_response.set_message(recv_request.message()); |
| 1174 |
srv_ctx.AddTrailingMetadata(meta1.first, meta1.second); |
| 1175 |
srv_ctx.AddTrailingMetadata(meta2.first, meta2.second); |
| 1176 |
response_writer.Finish(send_response, Status::OK, tag(4)); |
| 1177 |
|
| 1178 |
Verifier().Expect(4, true).Expect(5, true).Verify(cq_.get()); |
| 1179 |
|
| 1180 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 1181 |
EXPECT_TRUE(recv_status.ok()); |
| 1182 |
const auto& server_trailing_metadata = cli_ctx.GetServerTrailingMetadata(); |
| 1183 |
EXPECT_EQ(meta1.second, |
| 1184 |
ToString(server_trailing_metadata.find(meta1.first)->second)); |
| 1185 |
EXPECT_EQ(meta2.second, |
| 1186 |
ToString(server_trailing_metadata.find(meta2.first)->second)); |
| 1187 |
EXPECT_EQ(static_cast<size_t>(2), server_trailing_metadata.size()); |
| 1188 |
} |
| 1189 |
|
| 1190 |
TEST_P(AsyncEnd2endTest, MetadataRpc) { |
| 1191 |
ResetStub(); |
| 1192 |
|
| 1193 |
EchoRequest send_request; |
| 1194 |
EchoRequest recv_request; |
| 1195 |
EchoResponse send_response; |
| 1196 |
EchoResponse recv_response; |
| 1197 |
Status recv_status; |
| 1198 |
|
| 1199 |
ClientContext cli_ctx; |
| 1200 |
ServerContext srv_ctx; |
| 1201 |
grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 1202 |
|
| 1203 |
send_request.set_message(GetParam().message_content); |
| 1204 |
std::pair<std::string, std::string> meta1("key1", "val1"); |
| 1205 |
std::pair<std::string, std::string> meta2( |
| 1206 |
"key2-bin", |
| 1207 |
std::string("\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc", 13)); |
| 1208 |
std::pair<std::string, std::string> meta3("key3", "val3"); |
| 1209 |
std::pair<std::string, std::string> meta6( |
| 1210 |
"key4-bin", |
| 1211 |
std::string("\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d", |
| 1212 |
14)); |
| 1213 |
std::pair<std::string, std::string> meta5("key5", "val5"); |
| 1214 |
std::pair<std::string, std::string> meta4( |
| 1215 |
"key6-bin", |
| 1216 |
std::string( |
| 1217 |
"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee", 15)); |
| 1218 |
|
| 1219 |
cli_ctx.AddMetadata(meta1.first, meta1.second); |
| 1220 |
cli_ctx.AddMetadata(meta2.first, meta2.second); |
| 1221 |
|
| 1222 |
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
| 1223 |
stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 1224 |
response_reader->ReadInitialMetadata(tag(4)); |
| 1225 |
|
| 1226 |
service_->RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 1227 |
cq_.get(), tag(2)); |
| 1228 |
Verifier().Expect(2, true).Verify(cq_.get()); |
| 1229 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 1230 |
const auto& client_initial_metadata = srv_ctx.client_metadata(); |
| 1231 |
EXPECT_EQ(meta1.second, |
| 1232 |
ToString(client_initial_metadata.find(meta1.first)->second)); |
| 1233 |
EXPECT_EQ(meta2.second, |
| 1234 |
ToString(client_initial_metadata.find(meta2.first)->second)); |
| 1235 |
EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2)); |
| 1236 |
|
| 1237 |
srv_ctx.AddInitialMetadata(meta3.first, meta3.second); |
| 1238 |
srv_ctx.AddInitialMetadata(meta4.first, meta4.second); |
| 1239 |
response_writer.SendInitialMetadata(tag(3)); |
| 1240 |
Verifier().Expect(3, true).Expect(4, true).Verify(cq_.get()); |
| 1241 |
const auto& server_initial_metadata = cli_ctx.GetServerInitialMetadata(); |
| 1242 |
EXPECT_EQ(meta3.second, |
| 1243 |
ToString(server_initial_metadata.find(meta3.first)->second)); |
| 1244 |
EXPECT_EQ(meta4.second, |
| 1245 |
ToString(server_initial_metadata.find(meta4.first)->second)); |
| 1246 |
EXPECT_GE(server_initial_metadata.size(), static_cast<size_t>(2)); |
| 1247 |
|
| 1248 |
send_response.set_message(recv_request.message()); |
| 1249 |
srv_ctx.AddTrailingMetadata(meta5.first, meta5.second); |
| 1250 |
srv_ctx.AddTrailingMetadata(meta6.first, meta6.second); |
| 1251 |
response_writer.Finish(send_response, Status::OK, tag(5)); |
| 1252 |
response_reader->Finish(&recv_response, &recv_status, tag(6)); |
| 1253 |
|
| 1254 |
Verifier().Expect(5, true).Expect(6, true).Verify(cq_.get()); |
| 1255 |
|
| 1256 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 1257 |
EXPECT_TRUE(recv_status.ok()); |
| 1258 |
const auto& server_trailing_metadata = cli_ctx.GetServerTrailingMetadata(); |
| 1259 |
EXPECT_EQ(meta5.second, |
| 1260 |
ToString(server_trailing_metadata.find(meta5.first)->second)); |
| 1261 |
EXPECT_EQ(meta6.second, |
| 1262 |
ToString(server_trailing_metadata.find(meta6.first)->second)); |
| 1263 |
EXPECT_GE(server_trailing_metadata.size(), static_cast<size_t>(2)); |
| 1264 |
} |
| 1265 |
|
| 1266 |
// Server uses AsyncNotifyWhenDone API to check for cancellation |
| 1267 |
TEST_P(AsyncEnd2endTest, ServerCheckCancellation) { |
| 1268 |
ResetStub(); |
| 1269 |
|
| 1270 |
EchoRequest send_request; |
| 1271 |
EchoRequest recv_request; |
| 1272 |
EchoResponse send_response; |
| 1273 |
EchoResponse recv_response; |
| 1274 |
Status recv_status; |
| 1275 |
|
| 1276 |
ClientContext cli_ctx; |
| 1277 |
ServerContext srv_ctx; |
| 1278 |
grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 1279 |
|
| 1280 |
send_request.set_message(GetParam().message_content); |
| 1281 |
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
| 1282 |
stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 1283 |
response_reader->Finish(&recv_response, &recv_status, tag(4)); |
| 1284 |
|
| 1285 |
srv_ctx.AsyncNotifyWhenDone(tag(5)); |
| 1286 |
service_->RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 1287 |
cq_.get(), tag(2)); |
| 1288 |
|
| 1289 |
Verifier().Expect(2, true).Verify(cq_.get()); |
| 1290 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 1291 |
|
| 1292 |
cli_ctx.TryCancel(); |
| 1293 |
Verifier().Expect(5, true).Expect(4, true).Verify(cq_.get()); |
| 1294 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1295 |
|
| 1296 |
EXPECT_EQ(StatusCode::CANCELLED, recv_status.error_code()); |
| 1297 |
} |
| 1298 |
|
| 1299 |
// Server uses AsyncNotifyWhenDone API to check for normal finish |
| 1300 |
TEST_P(AsyncEnd2endTest, ServerCheckDone) { |
| 1301 |
ResetStub(); |
| 1302 |
|
| 1303 |
EchoRequest send_request; |
| 1304 |
EchoRequest recv_request; |
| 1305 |
EchoResponse send_response; |
| 1306 |
EchoResponse recv_response; |
| 1307 |
Status recv_status; |
| 1308 |
|
| 1309 |
ClientContext cli_ctx; |
| 1310 |
ServerContext srv_ctx; |
| 1311 |
grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 1312 |
|
| 1313 |
send_request.set_message(GetParam().message_content); |
| 1314 |
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
| 1315 |
stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 1316 |
response_reader->Finish(&recv_response, &recv_status, tag(4)); |
| 1317 |
|
| 1318 |
srv_ctx.AsyncNotifyWhenDone(tag(5)); |
| 1319 |
service_->RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 1320 |
cq_.get(), tag(2)); |
| 1321 |
|
| 1322 |
Verifier().Expect(2, true).Verify(cq_.get()); |
| 1323 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 1324 |
|
| 1325 |
send_response.set_message(recv_request.message()); |
| 1326 |
response_writer.Finish(send_response, Status::OK, tag(3)); |
| 1327 |
Verifier().Expect(3, true).Expect(4, true).Expect(5, true).Verify(cq_.get()); |
| 1328 |
EXPECT_FALSE(srv_ctx.IsCancelled()); |
| 1329 |
|
| 1330 |
EXPECT_EQ(send_response.message(), recv_response.message()); |
| 1331 |
EXPECT_TRUE(recv_status.ok()); |
| 1332 |
} |
| 1333 |
|
| 1334 |
TEST_P(AsyncEnd2endTest, UnimplementedRpc) { |
| 1335 |
ChannelArguments args; |
| 1336 |
const auto& channel_creds = GetCredentialsProvider()->GetChannelCredentials( |
| 1337 |
GetParam().credentials_type, &args); |
| 1338 |
std::shared_ptr<Channel> channel = |
| 1339 |
!(GetParam().inproc) ? grpc::CreateCustomChannel(server_address_.str(), |
| 1340 |
channel_creds, args) |
| 1341 |
: server_->InProcessChannel(args); |
| 1342 |
std::unique_ptr<grpc::testing::UnimplementedEchoService::Stub> stub; |
| 1343 |
stub = grpc::testing::UnimplementedEchoService::NewStub(channel); |
| 1344 |
EchoRequest send_request; |
| 1345 |
EchoResponse recv_response; |
| 1346 |
Status recv_status; |
| 1347 |
|
| 1348 |
ClientContext cli_ctx; |
| 1349 |
send_request.set_message(GetParam().message_content); |
| 1350 |
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
| 1351 |
stub->AsyncUnimplemented(&cli_ctx, send_request, cq_.get())); |
| 1352 |
|
| 1353 |
response_reader->Finish(&recv_response, &recv_status, tag(4)); |
| 1354 |
Verifier().Expect(4, true).Verify(cq_.get()); |
| 1355 |
|
| 1356 |
EXPECT_EQ(StatusCode::UNIMPLEMENTED, recv_status.error_code()); |
| 1357 |
EXPECT_EQ("", recv_status.error_message()); |
| 1358 |
} |
| 1359 |
|
| 1360 |
// This class is for testing scenarios where RPCs are cancelled on the server |
| 1361 |
// by calling ServerContext::TryCancel(). Server uses AsyncNotifyWhenDone |
| 1362 |
// API to check for cancellation |
| 1363 |
class AsyncEnd2endServerTryCancelTest : public AsyncEnd2endTest { |
| 1364 |
protected: |
| 1365 |
typedef enum { |
| 1366 |
DO_NOT_CANCEL = 0, |
| 1367 |
CANCEL_BEFORE_PROCESSING, |
| 1368 |
CANCEL_DURING_PROCESSING, |
| 1369 |
CANCEL_AFTER_PROCESSING |
| 1370 |
} ServerTryCancelRequestPhase; |
| 1371 |
|
| 1372 |
// Helper for testing client-streaming RPCs which are cancelled on the server. |
| 1373 |
// Depending on the value of server_try_cancel parameter, this will test one |
| 1374 |
// of the following three scenarios: |
| 1375 |
// CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading |
| 1376 |
// any messages from the client |
| 1377 |
// |
| 1378 |
// CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading |
| 1379 |
// messages from the client |
| 1380 |
// |
| 1381 |
// CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all |
| 1382 |
// messages from the client (but before sending any status back to the |
| 1383 |
// client) |
| 1384 |
void TestClientStreamingServerCancel( |
| 1385 |
ServerTryCancelRequestPhase server_try_cancel) { |
| 1386 |
ResetStub(); |
| 1387 |
|
| 1388 |
EchoRequest recv_request; |
| 1389 |
EchoResponse send_response; |
| 1390 |
EchoResponse recv_response; |
| 1391 |
Status recv_status; |
| 1392 |
|
| 1393 |
ClientContext cli_ctx; |
| 1394 |
ServerContext srv_ctx; |
| 1395 |
ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 1396 |
|
| 1397 |
// Initiate the 'RequestStream' call on client |
| 1398 |
CompletionQueue cli_cq; |
| 1399 |
|
| 1400 |
std::unique_ptr<ClientAsyncWriter<EchoRequest>> cli_stream( |
| 1401 |
stub_->AsyncRequestStream(&cli_ctx, &recv_response, &cli_cq, tag(1))); |
| 1402 |
|
| 1403 |
// On the server, request to be notified of 'RequestStream' calls |
| 1404 |
// and receive the 'RequestStream' call just made by the client |
| 1405 |
srv_ctx.AsyncNotifyWhenDone(tag(11)); |
| 1406 |
service_->RequestRequestStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 1407 |
tag(2)); |
| 1408 |
std::thread t1([&cli_cq] { Verifier().Expect(1, true).Verify(&cli_cq); }); |
| 1409 |
Verifier().Expect(2, true).Verify(cq_.get()); |
| 1410 |
t1.join(); |
| 1411 |
|
| 1412 |
bool expected_server_cq_result = true; |
| 1413 |
bool expected_client_cq_result = true; |
| 1414 |
|
| 1415 |
if (server_try_cancel == CANCEL_BEFORE_PROCESSING) { |
| 1416 |
srv_ctx.TryCancel(); |
| 1417 |
Verifier().Expect(11, true).Verify(cq_.get()); |
| 1418 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1419 |
|
| 1420 |
// Since cancellation is done before server reads any results, we know |
| 1421 |
// for sure that all server cq results will return false from this |
| 1422 |
// point forward |
| 1423 |
expected_server_cq_result = false; |
| 1424 |
expected_client_cq_result = false; |
| 1425 |
} |
| 1426 |
|
| 1427 |
bool ignore_client_cq_result = |
| 1428 |
(server_try_cancel == CANCEL_DURING_PROCESSING) || |
| 1429 |
(server_try_cancel == CANCEL_BEFORE_PROCESSING); |
| 1430 |
|
| 1431 |
std::thread cli_thread([&cli_cq, &cli_stream, &expected_client_cq_result, |
| 1432 |
&ignore_client_cq_result] { |
| 1433 |
EchoRequest send_request; |
| 1434 |
// Client sends 3 messages (tags 3, 4 and 5) |
| 1435 |
for (int tag_idx = 3; tag_idx <= 5; tag_idx++) { |
| 1436 |
send_request.set_message("Ping " + std::to_string(tag_idx)); |
| 1437 |
cli_stream->Write(send_request, tag(tag_idx)); |
| 1438 |
Verifier() |
| 1439 |
.Expect(tag_idx, expected_client_cq_result) |
| 1440 |
.Verify(&cli_cq, ignore_client_cq_result); |
| 1441 |
} |
| 1442 |
cli_stream->WritesDone(tag(6)); |
| 1443 |
// Ignore ok on WritesDone since cancel can affect it |
| 1444 |
Verifier() |
| 1445 |
.Expect(6, expected_client_cq_result) |
| 1446 |
.Verify(&cli_cq, ignore_client_cq_result); |
| 1447 |
}); |
| 1448 |
|
| 1449 |
bool ignore_cq_result = false; |
| 1450 |
bool want_done_tag = false; |
| 1451 |
std::thread* server_try_cancel_thd = nullptr; |
| 1452 |
|
| 1453 |
auto verif = Verifier(); |
| 1454 |
|
| 1455 |
if (server_try_cancel == CANCEL_DURING_PROCESSING) { |
| 1456 |
server_try_cancel_thd = |
| 1457 |
new std::thread([&srv_ctx] { srv_ctx.TryCancel(); }); |
| 1458 |
// Server will cancel the RPC in a parallel thread while reading the |
| 1459 |
// requests from the client. Since the cancellation can happen at anytime, |
| 1460 |
// some of the cq results (i.e those until cancellation) might be true but |
| 1461 |
// its non deterministic. So better to ignore the cq results |
| 1462 |
ignore_cq_result = true; |
| 1463 |
// Expect that we might possibly see the done tag that |
| 1464 |
// indicates cancellation completion in this case |
| 1465 |
want_done_tag = true; |
| 1466 |
verif.Expect(11, true); |
| 1467 |
} |
| 1468 |
|
| 1469 |
// Server reads 3 messages (tags 6, 7 and 8) |
| 1470 |
// But if want_done_tag is true, we might also see tag 11 |
| 1471 |
for (int tag_idx = 6; tag_idx <= 8; tag_idx++) { |
| 1472 |
srv_stream.Read(&recv_request, tag(tag_idx)); |
| 1473 |
// Note that we'll add something to the verifier and verify that |
| 1474 |
// something was seen, but it might be tag 11 and not what we |
| 1475 |
// just added |
| 1476 |
int got_tag = verif.Expect(tag_idx, expected_server_cq_result) |
| 1477 |
.Next(cq_.get(), ignore_cq_result); |
| 1478 |
GPR_ASSERT((got_tag == tag_idx) || (got_tag == 11 && want_done_tag)); |
| 1479 |
if (got_tag == 11) { |
| 1480 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1481 |
want_done_tag = false; |
| 1482 |
// Now get the other entry that we were waiting on |
| 1483 |
EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), tag_idx); |
| 1484 |
} |
| 1485 |
} |
| 1486 |
|
| 1487 |
cli_thread.join(); |
| 1488 |
|
| 1489 |
if (server_try_cancel_thd != nullptr) { |
| 1490 |
server_try_cancel_thd->join(); |
| 1491 |
delete server_try_cancel_thd; |
| 1492 |
} |
| 1493 |
|
| 1494 |
if (server_try_cancel == CANCEL_AFTER_PROCESSING) { |
| 1495 |
srv_ctx.TryCancel(); |
| 1496 |
want_done_tag = true; |
| 1497 |
verif.Expect(11, true); |
| 1498 |
} |
| 1499 |
|
| 1500 |
if (want_done_tag) { |
| 1501 |
verif.Verify(cq_.get()); |
| 1502 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1503 |
want_done_tag = false; |
| 1504 |
} |
| 1505 |
|
| 1506 |
// The RPC has been cancelled at this point for sure (i.e irrespective of |
| 1507 |
// the value of `server_try_cancel` is). So, from this point forward, we |
| 1508 |
// know that cq results are supposed to return false on server. |
| 1509 |
|
| 1510 |
// Server sends the final message and cancelled status (but the RPC is |
| 1511 |
// already cancelled at this point. So we expect the operation to fail) |
| 1512 |
srv_stream.Finish(send_response, Status::CANCELLED, tag(9)); |
| 1513 |
Verifier().Expect(9, false).Verify(cq_.get()); |
| 1514 |
|
| 1515 |
// Client will see the cancellation |
| 1516 |
cli_stream->Finish(&recv_status, tag(10)); |
| 1517 |
Verifier().Expect(10, true).Verify(&cli_cq); |
| 1518 |
EXPECT_FALSE(recv_status.ok()); |
| 1519 |
EXPECT_EQ(grpc::StatusCode::CANCELLED, recv_status.error_code()); |
| 1520 |
|
| 1521 |
cli_cq.Shutdown(); |
| 1522 |
void* phony_tag; |
| 1523 |
bool phony_ok; |
| 1524 |
while (cli_cq.Next(&phony_tag, &phony_ok)) { |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
// Helper for testing server-streaming RPCs which are cancelled on the server. |
| 1529 |
// Depending on the value of server_try_cancel parameter, this will test one |
| 1530 |
// of the following three scenarios: |
| 1531 |
// CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before sending |
| 1532 |
// any messages to the client |
| 1533 |
// |
| 1534 |
// CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while sending |
| 1535 |
// messages to the client |
| 1536 |
// |
| 1537 |
// CANCEL_AFTER PROCESSING: Rpc is cancelled by server after sending all |
| 1538 |
// messages to the client (but before sending any status back to the |
| 1539 |
// client) |
| 1540 |
void TestServerStreamingServerCancel( |
| 1541 |
ServerTryCancelRequestPhase server_try_cancel) { |
| 1542 |
ResetStub(); |
| 1543 |
|
| 1544 |
EchoRequest send_request; |
| 1545 |
EchoRequest recv_request; |
| 1546 |
EchoResponse send_response; |
| 1547 |
Status recv_status; |
| 1548 |
ClientContext cli_ctx; |
| 1549 |
ServerContext srv_ctx; |
| 1550 |
ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx); |
| 1551 |
|
| 1552 |
send_request.set_message("Ping"); |
| 1553 |
// Initiate the 'ResponseStream' call on the client |
| 1554 |
CompletionQueue cli_cq; |
| 1555 |
std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream( |
| 1556 |
stub_->AsyncResponseStream(&cli_ctx, send_request, &cli_cq, tag(1))); |
| 1557 |
// On the server, request to be notified of 'ResponseStream' calls and |
| 1558 |
// receive the call just made by the client |
| 1559 |
srv_ctx.AsyncNotifyWhenDone(tag(11)); |
| 1560 |
service_->RequestResponseStream(&srv_ctx, &recv_request, &srv_stream, |
| 1561 |
cq_.get(), cq_.get(), tag(2)); |
| 1562 |
|
| 1563 |
std::thread t1([&cli_cq] { Verifier().Expect(1, true).Verify(&cli_cq); }); |
| 1564 |
Verifier().Expect(2, true).Verify(cq_.get()); |
| 1565 |
t1.join(); |
| 1566 |
|
| 1567 |
EXPECT_EQ(send_request.message(), recv_request.message()); |
| 1568 |
|
| 1569 |
bool expected_cq_result = true; |
| 1570 |
bool ignore_cq_result = false; |
| 1571 |
bool want_done_tag = false; |
| 1572 |
bool expected_client_cq_result = true; |
| 1573 |
bool ignore_client_cq_result = |
| 1574 |
(server_try_cancel != CANCEL_BEFORE_PROCESSING); |
| 1575 |
|
| 1576 |
if (server_try_cancel == CANCEL_BEFORE_PROCESSING) { |
| 1577 |
srv_ctx.TryCancel(); |
| 1578 |
Verifier().Expect(11, true).Verify(cq_.get()); |
| 1579 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1580 |
|
| 1581 |
// We know for sure that all cq results will be false from this point |
| 1582 |
// since the server cancelled the RPC |
| 1583 |
expected_cq_result = false; |
| 1584 |
expected_client_cq_result = false; |
| 1585 |
} |
| 1586 |
|
| 1587 |
std::thread cli_thread([&cli_cq, &cli_stream, &expected_client_cq_result, |
| 1588 |
&ignore_client_cq_result] { |
| 1589 |
// Client attempts to read the three messages from the server |
| 1590 |
for (int tag_idx = 6; tag_idx <= 8; tag_idx++) { |
| 1591 |
EchoResponse recv_response; |
| 1592 |
cli_stream->Read(&recv_response, tag(tag_idx)); |
| 1593 |
Verifier() |
| 1594 |
.Expect(tag_idx, expected_client_cq_result) |
| 1595 |
.Verify(&cli_cq, ignore_client_cq_result); |
| 1596 |
} |
| 1597 |
}); |
| 1598 |
|
| 1599 |
std::thread* server_try_cancel_thd = nullptr; |
| 1600 |
|
| 1601 |
auto verif = Verifier(); |
| 1602 |
|
| 1603 |
if (server_try_cancel == CANCEL_DURING_PROCESSING) { |
| 1604 |
server_try_cancel_thd = |
| 1605 |
new std::thread([&srv_ctx] { srv_ctx.TryCancel(); }); |
| 1606 |
|
| 1607 |
// Server will cancel the RPC in a parallel thread while writing responses |
| 1608 |
// to the client. Since the cancellation can happen at anytime, some of |
| 1609 |
// the cq results (i.e those until cancellation) might be true but it is |
| 1610 |
// non deterministic. So better to ignore the cq results |
| 1611 |
ignore_cq_result = true; |
| 1612 |
// Expect that we might possibly see the done tag that |
| 1613 |
// indicates cancellation completion in this case |
| 1614 |
want_done_tag = true; |
| 1615 |
verif.Expect(11, true); |
| 1616 |
} |
| 1617 |
|
| 1618 |
// Server sends three messages (tags 3, 4 and 5) |
| 1619 |
// But if want_done tag is true, we might also see tag 11 |
| 1620 |
for (int tag_idx = 3; tag_idx <= 5; tag_idx++) { |
| 1621 |
send_response.set_message("Pong " + std::to_string(tag_idx)); |
| 1622 |
srv_stream.Write(send_response, tag(tag_idx)); |
| 1623 |
// Note that we'll add something to the verifier and verify that |
| 1624 |
// something was seen, but it might be tag 11 and not what we |
| 1625 |
// just added |
| 1626 |
int got_tag = verif.Expect(tag_idx, expected_cq_result) |
| 1627 |
.Next(cq_.get(), ignore_cq_result); |
| 1628 |
GPR_ASSERT((got_tag == tag_idx) || (got_tag == 11 && want_done_tag)); |
| 1629 |
if (got_tag == 11) { |
| 1630 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1631 |
want_done_tag = false; |
| 1632 |
// Now get the other entry that we were waiting on |
| 1633 |
EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), tag_idx); |
| 1634 |
} |
| 1635 |
} |
| 1636 |
|
| 1637 |
if (server_try_cancel_thd != nullptr) { |
| 1638 |
server_try_cancel_thd->join(); |
| 1639 |
delete server_try_cancel_thd; |
| 1640 |
} |
| 1641 |
|
| 1642 |
if (server_try_cancel == CANCEL_AFTER_PROCESSING) { |
| 1643 |
srv_ctx.TryCancel(); |
| 1644 |
want_done_tag = true; |
| 1645 |
verif.Expect(11, true); |
| 1646 |
} |
| 1647 |
|
| 1648 |
if (want_done_tag) { |
| 1649 |
verif.Verify(cq_.get()); |
| 1650 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1651 |
want_done_tag = false; |
| 1652 |
} |
| 1653 |
|
| 1654 |
cli_thread.join(); |
| 1655 |
|
| 1656 |
// The RPC has been cancelled at this point for sure (i.e irrespective of |
| 1657 |
// the value of `server_try_cancel` is). So, from this point forward, we |
| 1658 |
// know that cq results are supposed to return false on server. |
| 1659 |
|
| 1660 |
// Server finishes the stream (but the RPC is already cancelled) |
| 1661 |
srv_stream.Finish(Status::CANCELLED, tag(9)); |
| 1662 |
Verifier().Expect(9, false).Verify(cq_.get()); |
| 1663 |
|
| 1664 |
// Client will see the cancellation |
| 1665 |
cli_stream->Finish(&recv_status, tag(10)); |
| 1666 |
Verifier().Expect(10, true).Verify(&cli_cq); |
| 1667 |
EXPECT_FALSE(recv_status.ok()); |
| 1668 |
EXPECT_EQ(grpc::StatusCode::CANCELLED, recv_status.error_code()); |
| 1669 |
|
| 1670 |
cli_cq.Shutdown(); |
| 1671 |
void* phony_tag; |
| 1672 |
bool phony_ok; |
| 1673 |
while (cli_cq.Next(&phony_tag, &phony_ok)) { |
| 1674 |
} |
| 1675 |
} |
| 1676 |
|
| 1677 |
// Helper for testing bidirectinal-streaming RPCs which are cancelled on the |
| 1678 |
// server. |
| 1679 |
// |
| 1680 |
// Depending on the value of server_try_cancel parameter, this will |
| 1681 |
// test one of the following three scenarios: |
| 1682 |
// CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/ |
| 1683 |
// writing any messages from/to the client |
| 1684 |
// |
| 1685 |
// CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading |
| 1686 |
// messages from the client |
| 1687 |
// |
| 1688 |
// CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all |
| 1689 |
// messages from the client (but before sending any status back to the |
| 1690 |
// client) |
| 1691 |
void TestBidiStreamingServerCancel( |
| 1692 |
ServerTryCancelRequestPhase server_try_cancel) { |
| 1693 |
ResetStub(); |
| 1694 |
|
| 1695 |
EchoRequest send_request; |
| 1696 |
EchoRequest recv_request; |
| 1697 |
EchoResponse send_response; |
| 1698 |
EchoResponse recv_response; |
| 1699 |
Status recv_status; |
| 1700 |
ClientContext cli_ctx; |
| 1701 |
ServerContext srv_ctx; |
| 1702 |
ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 1703 |
|
| 1704 |
// Initiate the call from the client side |
| 1705 |
std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>> |
| 1706 |
cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1))); |
| 1707 |
|
| 1708 |
// On the server, request to be notified of the 'BidiStream' call and |
| 1709 |
// receive the call just made by the client |
| 1710 |
srv_ctx.AsyncNotifyWhenDone(tag(11)); |
| 1711 |
service_->RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 1712 |
tag(2)); |
| 1713 |
Verifier().Expect(1, true).Expect(2, true).Verify(cq_.get()); |
| 1714 |
|
| 1715 |
auto verif = Verifier(); |
| 1716 |
|
| 1717 |
// Client sends the first and the only message |
| 1718 |
send_request.set_message("Ping"); |
| 1719 |
cli_stream->Write(send_request, tag(3)); |
| 1720 |
verif.Expect(3, true); |
| 1721 |
|
| 1722 |
bool expected_cq_result = true; |
| 1723 |
bool ignore_cq_result = false; |
| 1724 |
bool want_done_tag = false; |
| 1725 |
|
| 1726 |
int got_tag, got_tag2; |
| 1727 |
bool tag_3_done = false; |
| 1728 |
|
| 1729 |
if (server_try_cancel == CANCEL_BEFORE_PROCESSING) { |
| 1730 |
srv_ctx.TryCancel(); |
| 1731 |
verif.Expect(11, true); |
| 1732 |
// We know for sure that all server cq results will be false from |
| 1733 |
// this point since the server cancelled the RPC. However, we can't |
| 1734 |
// say for sure about the client |
| 1735 |
expected_cq_result = false; |
| 1736 |
ignore_cq_result = true; |
| 1737 |
|
| 1738 |
do { |
| 1739 |
got_tag = verif.Next(cq_.get(), ignore_cq_result); |
| 1740 |
GPR_ASSERT(((got_tag == 3) && !tag_3_done) || (got_tag == 11)); |
| 1741 |
if (got_tag == 3) { |
| 1742 |
tag_3_done = true; |
| 1743 |
} |
| 1744 |
} while (got_tag != 11); |
| 1745 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1746 |
} |
| 1747 |
|
| 1748 |
std::thread* server_try_cancel_thd = nullptr; |
| 1749 |
|
| 1750 |
if (server_try_cancel == CANCEL_DURING_PROCESSING) { |
| 1751 |
server_try_cancel_thd = |
| 1752 |
new std::thread([&srv_ctx] { srv_ctx.TryCancel(); }); |
| 1753 |
|
| 1754 |
// Since server is going to cancel the RPC in a parallel thread, some of |
| 1755 |
// the cq results (i.e those until the cancellation) might be true. Since |
| 1756 |
// that number is non-deterministic, it is better to ignore the cq results |
| 1757 |
ignore_cq_result = true; |
| 1758 |
// Expect that we might possibly see the done tag that |
| 1759 |
// indicates cancellation completion in this case |
| 1760 |
want_done_tag = true; |
| 1761 |
verif.Expect(11, true); |
| 1762 |
} |
| 1763 |
|
| 1764 |
srv_stream.Read(&recv_request, tag(4)); |
| 1765 |
verif.Expect(4, expected_cq_result); |
| 1766 |
got_tag = tag_3_done ? 3 : verif.Next(cq_.get(), ignore_cq_result); |
| 1767 |
got_tag2 = verif.Next(cq_.get(), ignore_cq_result); |
| 1768 |
GPR_ASSERT((got_tag == 3) || (got_tag == 4) || |
| 1769 |
(got_tag == 11 && want_done_tag)); |
| 1770 |
GPR_ASSERT((got_tag2 == 3) || (got_tag2 == 4) || |
| 1771 |
(got_tag2 == 11 && want_done_tag)); |
| 1772 |
// If we get 3 and 4, we don't need to wait for 11, but if |
| 1773 |
// we get 11, we should also clear 3 and 4 |
| 1774 |
if (got_tag + got_tag2 != 7) { |
| 1775 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1776 |
want_done_tag = false; |
| 1777 |
got_tag = verif.Next(cq_.get(), ignore_cq_result); |
| 1778 |
GPR_ASSERT((got_tag == 3) || (got_tag == 4)); |
| 1779 |
} |
| 1780 |
|
| 1781 |
send_response.set_message("Pong"); |
| 1782 |
srv_stream.Write(send_response, tag(5)); |
| 1783 |
verif.Expect(5, expected_cq_result); |
| 1784 |
|
| 1785 |
cli_stream->Read(&recv_response, tag(6)); |
| 1786 |
verif.Expect(6, expected_cq_result); |
| 1787 |
got_tag = verif.Next(cq_.get(), ignore_cq_result); |
| 1788 |
got_tag2 = verif.Next(cq_.get(), ignore_cq_result); |
| 1789 |
GPR_ASSERT((got_tag == 5) || (got_tag == 6) || |
| 1790 |
(got_tag == 11 && want_done_tag)); |
| 1791 |
GPR_ASSERT((got_tag2 == 5) || (got_tag2 == 6) || |
| 1792 |
(got_tag2 == 11 && want_done_tag)); |
| 1793 |
// If we get 5 and 6, we don't need to wait for 11, but if |
| 1794 |
// we get 11, we should also clear 5 and 6 |
| 1795 |
if (got_tag + got_tag2 != 11) { |
| 1796 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1797 |
want_done_tag = false; |
| 1798 |
got_tag = verif.Next(cq_.get(), ignore_cq_result); |
| 1799 |
GPR_ASSERT((got_tag == 5) || (got_tag == 6)); |
| 1800 |
} |
| 1801 |
|
| 1802 |
// This is expected to succeed in all cases |
| 1803 |
cli_stream->WritesDone(tag(7)); |
| 1804 |
verif.Expect(7, true); |
| 1805 |
// TODO(vjpai): Consider whether the following is too flexible |
| 1806 |
// or whether it should just be reset to ignore_cq_result |
| 1807 |
bool ignore_cq_wd_result = |
| 1808 |
ignore_cq_result || (server_try_cancel == CANCEL_BEFORE_PROCESSING); |
| 1809 |
got_tag = verif.Next(cq_.get(), ignore_cq_wd_result); |
| 1810 |
GPR_ASSERT((got_tag == 7) || (got_tag == 11 && want_done_tag)); |
| 1811 |
if (got_tag == 11) { |
| 1812 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1813 |
want_done_tag = false; |
| 1814 |
// Now get the other entry that we were waiting on |
| 1815 |
EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_wd_result), 7); |
| 1816 |
} |
| 1817 |
|
| 1818 |
// This is expected to fail in all cases i.e for all values of |
| 1819 |
// server_try_cancel. This is because at this point, either there are no |
| 1820 |
// more msgs from the client (because client called WritesDone) or the RPC |
| 1821 |
// is cancelled on the server |
| 1822 |
srv_stream.Read(&recv_request, tag(8)); |
| 1823 |
verif.Expect(8, false); |
| 1824 |
got_tag = verif.Next(cq_.get(), ignore_cq_result); |
| 1825 |
GPR_ASSERT((got_tag == 8) || (got_tag == 11 && want_done_tag)); |
| 1826 |
if (got_tag == 11) { |
| 1827 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1828 |
want_done_tag = false; |
| 1829 |
// Now get the other entry that we were waiting on |
| 1830 |
EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 8); |
| 1831 |
} |
| 1832 |
|
| 1833 |
if (server_try_cancel_thd != nullptr) { |
| 1834 |
server_try_cancel_thd->join(); |
| 1835 |
delete server_try_cancel_thd; |
| 1836 |
} |
| 1837 |
|
| 1838 |
if (server_try_cancel == CANCEL_AFTER_PROCESSING) { |
| 1839 |
srv_ctx.TryCancel(); |
| 1840 |
want_done_tag = true; |
| 1841 |
verif.Expect(11, true); |
| 1842 |
} |
| 1843 |
|
| 1844 |
if (want_done_tag) { |
| 1845 |
verif.Verify(cq_.get()); |
| 1846 |
EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1847 |
want_done_tag = false; |
| 1848 |
} |
| 1849 |
|
| 1850 |
// The RPC has been cancelled at this point for sure (i.e irrespective of |
| 1851 |
// the value of `server_try_cancel` is). So, from this point forward, we |
| 1852 |
// know that cq results are supposed to return false on server. |
| 1853 |
|
| 1854 |
srv_stream.Finish(Status::CANCELLED, tag(9)); |
| 1855 |
Verifier().Expect(9, false).Verify(cq_.get()); |
| 1856 |
|
| 1857 |
cli_stream->Finish(&recv_status, tag(10)); |
| 1858 |
Verifier().Expect(10, true).Verify(cq_.get()); |
| 1859 |
EXPECT_FALSE(recv_status.ok()); |
| 1860 |
EXPECT_EQ(grpc::StatusCode::CANCELLED, recv_status.error_code()); |
| 1861 |
} |
| 1862 |
}; |
| 1863 |
|
| 1864 |
TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelBefore) { |
| 1865 |
TestClientStreamingServerCancel(CANCEL_BEFORE_PROCESSING); |
| 1866 |
} |
| 1867 |
|
| 1868 |
TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelDuring) { |
| 1869 |
TestClientStreamingServerCancel(CANCEL_DURING_PROCESSING); |
| 1870 |
} |
| 1871 |
|
| 1872 |
TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelAfter) { |
| 1873 |
TestClientStreamingServerCancel(CANCEL_AFTER_PROCESSING); |
| 1874 |
} |
| 1875 |
|
| 1876 |
TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelBefore) { |
| 1877 |
TestServerStreamingServerCancel(CANCEL_BEFORE_PROCESSING); |
| 1878 |
} |
| 1879 |
|
| 1880 |
TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelDuring) { |
| 1881 |
TestServerStreamingServerCancel(CANCEL_DURING_PROCESSING); |
| 1882 |
} |
| 1883 |
|
| 1884 |
TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelAfter) { |
| 1885 |
TestServerStreamingServerCancel(CANCEL_AFTER_PROCESSING); |
| 1886 |
} |
| 1887 |
|
| 1888 |
TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelBefore) { |
| 1889 |
TestBidiStreamingServerCancel(CANCEL_BEFORE_PROCESSING); |
| 1890 |
} |
| 1891 |
|
| 1892 |
TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelDuring) { |
| 1893 |
TestBidiStreamingServerCancel(CANCEL_DURING_PROCESSING); |
| 1894 |
} |
| 1895 |
|
| 1896 |
TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelAfter) { |
| 1897 |
TestBidiStreamingServerCancel(CANCEL_AFTER_PROCESSING); |
| 1898 |
} |
| 1899 |
|
| 1900 |
std::vector<TestScenario> CreateTestScenarios(bool /*test_secure*/, |
| 1901 |
bool test_message_size_limit) { |
| 1902 |
std::vector<TestScenario> scenarios; |
| 1903 |
std::vector<std::string> credentials_types; |
| 1904 |
std::vector<std::string> messages; |
| 1905 |
|
| 1906 |
auto insec_ok = [] { |
| 1907 |
// Only allow insecure credentials type when it is registered with the |
| 1908 |
// provider. User may create providers that do not have insecure. |
| 1909 |
return GetCredentialsProvider()->GetChannelCredentials( |
| 1910 |
kInsecureCredentialsType, nullptr) != nullptr; |
| 1911 |
}; |
| 1912 |
|
| 1913 |
if (insec_ok()) { |
| 1914 |
credentials_types.push_back(kInsecureCredentialsType); |
| 1915 |
} |
| 1916 |
auto sec_list = GetCredentialsProvider()->GetSecureCredentialsTypeList(); |
| 1917 |
for (auto sec = sec_list.begin(); sec != sec_list.end(); sec++) { |
| 1918 |
credentials_types.push_back(*sec); |
| 1919 |
} |
| 1920 |
GPR_ASSERT(!credentials_types.empty()); |
| 1921 |
|
| 1922 |
messages.push_back("Hello"); |
| 1923 |
if (test_message_size_limit) { |
| 1924 |
for (size_t k = 1; k < GRPC_DEFAULT_MAX_RECV_MESSAGE_LENGTH / 1024; |
| 1925 |
k *= 32) { |
| 1926 |
std::string big_msg; |
| 1927 |
for (size_t i = 0; i < k * 1024; ++i) { |
| 1928 |
char c = 'a' + (i % 26); |
| 1929 |
big_msg += c; |
| 1930 |
} |
| 1931 |
messages.push_back(big_msg); |
| 1932 |
} |
| 1933 |
if (!BuiltUnderMsan()) { |
| 1934 |
// 4MB message processing with SSL is very slow under msan |
| 1935 |
// (causes timeouts) and doesn't really increase the signal from tests. |
| 1936 |
// Reserve 100 bytes for other fields of the message proto. |
| 1937 |
messages.push_back( |
| 1938 |
std::string(GRPC_DEFAULT_MAX_RECV_MESSAGE_LENGTH - 100, 'a')); |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
// TODO (sreek) Renable tests with health check service after the issue |
| 1943 |
// https://github.com/grpc/grpc/issues/11223 is resolved |
| 1944 |
for (auto health_check_service : {false}) { |
| 1945 |
for (auto msg = messages.begin(); msg != messages.end(); msg++) { |
| 1946 |
for (auto cred = credentials_types.begin(); |
| 1947 |
cred != credentials_types.end(); ++cred) { |
| 1948 |
scenarios.emplace_back(false, *cred, health_check_service, *msg); |
| 1949 |
} |
| 1950 |
if (insec_ok()) { |
| 1951 |
scenarios.emplace_back(true, kInsecureCredentialsType, |
| 1952 |
health_check_service, *msg); |
| 1953 |
} |
| 1954 |
} |
| 1955 |
} |
| 1956 |
return scenarios; |
| 1957 |
} |
| 1958 |
|
| 1959 |
INSTANTIATE_TEST_SUITE_P(AsyncEnd2end, AsyncEnd2endTest, |
| 1960 |
::testing::ValuesIn(CreateTestScenarios(true, true))); |
| 1961 |
INSTANTIATE_TEST_SUITE_P(AsyncEnd2endServerTryCancel, |
| 1962 |
AsyncEnd2endServerTryCancelTest, |
| 1963 |
::testing::ValuesIn(CreateTestScenarios(false, |
| 1964 |
false))); |
| 1965 |
|
| 1966 |
} // namespace |
| 1967 |
} // namespace testing |
| 1968 |
} // namespace grpc |
| 1969 |
|
| 1970 |
int main(int argc, char** argv) { |
| 1971 |
// Change the backup poll interval from 5s to 100ms to speed up the |
| 1972 |
// ReconnectChannel test |
| 1973 |
GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, 100); |
| 1974 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 1975 |
::testing::InitGoogleTest(&argc, argv); |
| 1976 |
int ret = RUN_ALL_TESTS(); |
| 1977 |
return ret; |
| 1978 |
} |