blob: ac79fe827400d029352336bab71ca0b742557f2f [file] [log] [blame]
Craig Tiller0220cf12015-02-12 17:39:26 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller0220cf12015-02-12 17:39:26 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Vijay Paib31a9a02016-06-16 07:50:20 +000034#include <cinttypes>
Yang Gaoda699b82015-02-18 01:10:22 -080035#include <memory>
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -080036#include <thread>
Craig Tiller0220cf12015-02-12 17:39:26 -080037
yang-g8c2be9f2015-08-19 16:28:09 -070038#include <grpc++/channel.h>
Craig Tiller0220cf12015-02-12 17:39:26 -080039#include <grpc++/client_context.h>
40#include <grpc++/create_channel.h>
Craig Tiller0220cf12015-02-12 17:39:26 -080041#include <grpc++/server.h>
42#include <grpc++/server_builder.h>
43#include <grpc++/server_context.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080044#include <grpc/grpc.h>
45#include <grpc/support/thd.h>
46#include <grpc/support/time.h>
Vijay Paib65eda42016-02-16 13:48:05 -080047#include <grpc/support/tls.h>
Craig Tiller0220cf12015-02-12 17:39:26 -080048#include <gtest/gtest.h>
49
Craig Tiller1b4e3302015-12-17 16:35:00 -080050#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
51#include "src/proto/grpc/testing/echo.grpc.pb.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080052#include "test/core/util/port.h"
53#include "test/core/util/test_config.h"
yang-ge21908f2015-08-25 13:47:51 -070054#include "test/cpp/util/string_ref_helper.h"
Vijay Paidf8b62c2016-05-02 14:34:24 -070055#include "test/cpp/util/test_credentials_provider.h"
Craig Tiller0220cf12015-02-12 17:39:26 -080056
Craig Tiller69f90e62015-08-06 08:32:35 -070057#ifdef GPR_POSIX_SOCKET
Craig Tillerf45496f2016-03-30 07:41:19 -070058#include "src/core/lib/iomgr/ev_posix.h"
Craig Tiller69f90e62015-08-06 08:32:35 -070059#endif
60
Craig Tiller1b4e3302015-12-17 16:35:00 -080061using grpc::testing::EchoRequest;
62using grpc::testing::EchoResponse;
Vijay Paidf8b62c2016-05-02 14:34:24 -070063using grpc::testing::kTlsCredentialsType;
Craig Tiller0220cf12015-02-12 17:39:26 -080064using std::chrono::system_clock;
65
Vijay Paib65eda42016-02-16 13:48:05 -080066GPR_TLS_DECL(g_is_async_end2end_test);
67
Craig Tiller0220cf12015-02-12 17:39:26 -080068namespace grpc {
69namespace testing {
70
71namespace {
72
Craig Tiller7536af02015-12-22 13:49:30 -080073void* tag(int i) { return (void*)(intptr_t)i; }
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -080074int detag(void* p) { return static_cast<int>(reinterpret_cast<intptr_t>(p)); }
Yang Gaoc05b6cb2015-02-13 00:34:10 -080075
Craig Tiller69f90e62015-08-06 08:32:35 -070076#ifdef GPR_POSIX_SOCKET
Vijay Paib65eda42016-02-16 13:48:05 -080077static int maybe_assert_non_blocking_poll(struct pollfd* pfds, nfds_t nfds,
78 int timeout) {
79 if (gpr_tls_get(&g_is_async_end2end_test)) {
80 GPR_ASSERT(timeout == 0);
81 }
82 return poll(pfds, nfds, timeout);
Craig Tiller69f90e62015-08-06 08:32:35 -070083}
84
85class PollOverride {
Craig Tiller06cf3cc2015-05-13 13:11:01 -070086 public:
Craig Tiller69f90e62015-08-06 08:32:35 -070087 PollOverride(grpc_poll_function_type f) {
88 prev_ = grpc_poll_function;
89 grpc_poll_function = f;
90 }
91
Craig Tiller4c06b822015-08-06 08:41:31 -070092 ~PollOverride() { grpc_poll_function = prev_; }
Craig Tiller69f90e62015-08-06 08:32:35 -070093
94 private:
95 grpc_poll_function_type prev_;
96};
97
vjpaicf4daeb2016-02-15 02:33:54 -080098class PollingOverrider : public PollOverride {
Craig Tiller69f90e62015-08-06 08:32:35 -070099 public:
vjpaicf4daeb2016-02-15 02:33:54 -0800100 explicit PollingOverrider(bool allow_blocking)
Vijay Paib65eda42016-02-16 13:48:05 -0800101 : PollOverride(allow_blocking ? poll : maybe_assert_non_blocking_poll) {}
Craig Tiller69f90e62015-08-06 08:32:35 -0700102};
103#else
vjpaicf4daeb2016-02-15 02:33:54 -0800104class PollingOverrider {
Craig Tiller69f90e62015-08-06 08:32:35 -0700105 public:
vjpaicf4daeb2016-02-15 02:33:54 -0800106 explicit PollingOverrider(bool allow_blocking) {}
Craig Tiller69f90e62015-08-06 08:32:35 -0700107};
108#endif
109
vjpaicf4daeb2016-02-15 02:33:54 -0800110class Verifier {
Craig Tiller69f90e62015-08-06 08:32:35 -0700111 public:
vjpaicf4daeb2016-02-15 02:33:54 -0800112 explicit Verifier(bool spin) : spin_(spin) {}
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800113 // Expect sets the expected ok value for a specific tag
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700114 Verifier& Expect(int i, bool expect_ok) {
115 expectations_[tag(i)] = expect_ok;
116 return *this;
vjpai7aadf462015-03-16 23:58:44 -0700117 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800118
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800119 // Next waits for 1 async tag to complete, checks its
120 // expectations, and returns the tag
121 int Next(CompletionQueue* cq, bool ignore_ok) {
122 bool ok;
123 void* got_tag;
124 if (spin_) {
125 for (;;) {
126 auto r = cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME));
127 if (r == CompletionQueue::TIMEOUT) continue;
128 if (r == CompletionQueue::GOT_EVENT) break;
129 gpr_log(GPR_ERROR, "unexpected result from AsyncNext");
130 abort();
131 }
132 } else {
133 EXPECT_TRUE(cq->Next(&got_tag, &ok));
134 }
135 auto it = expectations_.find(got_tag);
136 EXPECT_TRUE(it != expectations_.end());
137 if (!ignore_ok) {
138 EXPECT_EQ(it->second, ok);
139 }
140 expectations_.erase(it);
141 return detag(got_tag);
142 }
143
144 // Verify keeps calling Next until all currently set
145 // expected tags are complete
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800146 void Verify(CompletionQueue* cq) { Verify(cq, false); }
147
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800148 // This version of Verify allows optionally ignoring the
149 // outcome of the expectation
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800150 void Verify(CompletionQueue* cq, bool ignore_ok) {
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700151 GPR_ASSERT(!expectations_.empty());
152 while (!expectations_.empty()) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800153 Next(cq, ignore_ok);
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700154 }
155 }
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800156 // This version of Verify stops after a certain deadline
Craig Tillerd6c98df2015-08-18 09:33:44 -0700157 void Verify(CompletionQueue* cq,
158 std::chrono::system_clock::time_point deadline) {
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700159 if (expectations_.empty()) {
160 bool ok;
Craig Tillerd6c98df2015-08-18 09:33:44 -0700161 void* got_tag;
Craig Tiller69f90e62015-08-06 08:32:35 -0700162 if (spin_) {
163 while (std::chrono::system_clock::now() < deadline) {
Craig Tiller4c06b822015-08-06 08:41:31 -0700164 EXPECT_EQ(
165 cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME)),
166 CompletionQueue::TIMEOUT);
Craig Tiller69f90e62015-08-06 08:32:35 -0700167 }
168 } else {
Craig Tiller4c06b822015-08-06 08:41:31 -0700169 EXPECT_EQ(cq->AsyncNext(&got_tag, &ok, deadline),
170 CompletionQueue::TIMEOUT);
Craig Tiller69f90e62015-08-06 08:32:35 -0700171 }
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700172 } else {
173 while (!expectations_.empty()) {
174 bool ok;
Craig Tillerd6c98df2015-08-18 09:33:44 -0700175 void* got_tag;
Craig Tiller69f90e62015-08-06 08:32:35 -0700176 if (spin_) {
177 for (;;) {
178 GPR_ASSERT(std::chrono::system_clock::now() < deadline);
Craig Tiller4c06b822015-08-06 08:41:31 -0700179 auto r =
180 cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME));
Craig Tiller69f90e62015-08-06 08:32:35 -0700181 if (r == CompletionQueue::TIMEOUT) continue;
182 if (r == CompletionQueue::GOT_EVENT) break;
183 gpr_log(GPR_ERROR, "unexpected result from AsyncNext");
184 abort();
Craig Tiller4c06b822015-08-06 08:41:31 -0700185 }
Craig Tiller69f90e62015-08-06 08:32:35 -0700186 } else {
Craig Tiller4c06b822015-08-06 08:41:31 -0700187 EXPECT_EQ(cq->AsyncNext(&got_tag, &ok, deadline),
188 CompletionQueue::GOT_EVENT);
Craig Tiller69f90e62015-08-06 08:32:35 -0700189 }
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700190 auto it = expectations_.find(got_tag);
191 EXPECT_TRUE(it != expectations_.end());
192 EXPECT_EQ(it->second, ok);
193 expectations_.erase(it);
194 }
195 }
196 }
197
198 private:
199 std::map<void*, bool> expectations_;
Craig Tiller69f90e62015-08-06 08:32:35 -0700200 bool spin_;
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700201};
vjpai7aadf462015-03-16 23:58:44 -0700202
Yuchen Zeng13184792016-07-18 13:07:42 -0700203bool plugin_has_sync_methods(std::unique_ptr<ServerBuilderPlugin>& plugin) {
204 return plugin->has_sync_methods();
205}
206
Yuchen Zenga42ec212016-04-29 13:03:06 -0700207// This class disables the server builder plugins that may add sync services to
208// the server. If there are sync services, UnimplementedRpc test will triger
209// the sync unkown rpc routine on the server side, rather than the async one
210// that needs to be tested here.
211class ServerBuilderSyncPluginDisabler : public ::grpc::ServerBuilderOption {
212 public:
213 void UpdateArguments(ChannelArguments* arg) GRPC_OVERRIDE {}
214
vjpai97da6472016-06-13 09:56:26 -0700215 void UpdatePlugins(std::vector<std::unique_ptr<ServerBuilderPlugin>>* plugins)
Yuchen Zenga42ec212016-04-29 13:03:06 -0700216 GRPC_OVERRIDE {
Yuchen Zeng13184792016-07-18 13:07:42 -0700217 plugins->erase(std::remove_if(plugins->begin(), plugins->end(),
218 plugin_has_sync_methods),
219 plugins->end());
Yuchen Zenga42ec212016-04-29 13:03:06 -0700220 }
221};
222
Vijay Paidf8b62c2016-05-02 14:34:24 -0700223class TestScenario {
224 public:
Vijay Paid7b1e702016-05-02 15:10:21 -0700225 TestScenario(bool non_block, const grpc::string& creds_type,
226 const grpc::string& content)
227 : disable_blocking(non_block),
228 credentials_type(creds_type),
229 message_content(content) {}
230 void Log() const {
Yuchen Zengbeaa3572016-06-10 15:46:14 -0700231 gpr_log(
232 GPR_INFO,
233 "Scenario: disable_blocking %d, credentials %s, message size %" PRIuPTR,
234 disable_blocking, credentials_type.c_str(), message_content.size());
Vijay Paid7b1e702016-05-02 15:10:21 -0700235 }
Vijay Paidf8b62c2016-05-02 14:34:24 -0700236 bool disable_blocking;
Vijay Pai679c75f2016-06-15 13:08:00 -0700237 // Although the below grpc::string's are logically const, we can't declare
238 // them const because of a limitation in the way old compilers (e.g., gcc-4.4)
239 // manage vector insertion using a copy constructor
240 grpc::string credentials_type;
241 grpc::string message_content;
Vijay Paidf8b62c2016-05-02 14:34:24 -0700242};
243
244class AsyncEnd2endTest : public ::testing::TestWithParam<TestScenario> {
Craig Tiller0220cf12015-02-12 17:39:26 -0800245 protected:
Vijay Paid7b1e702016-05-02 15:10:21 -0700246 AsyncEnd2endTest() { GetParam().Log(); }
Craig Tiller0220cf12015-02-12 17:39:26 -0800247
Craig Tillercf133f42015-02-26 14:05:56 -0800248 void SetUp() GRPC_OVERRIDE {
Vijay Paidf8b62c2016-05-02 14:34:24 -0700249 poll_overrider_.reset(new PollingOverrider(!GetParam().disable_blocking));
Vijay Pai018879a2016-02-16 09:20:50 -0800250
David Klempner6fb122d2016-05-13 15:24:17 -0700251 port_ = grpc_pick_unused_port_or_die();
252 server_address_ << "localhost:" << port_;
vjpai017ed622015-12-09 10:42:54 -0800253
Craig Tiller0220cf12015-02-12 17:39:26 -0800254 // Setup server
255 ServerBuilder builder;
Vijay Paidf8b62c2016-05-02 14:34:24 -0700256 auto server_creds = GetServerCredentials(GetParam().credentials_type);
257 builder.AddListeningPort(server_address_.str(), server_creds);
Craig Tiller15f383c2016-01-07 12:45:32 -0800258 builder.RegisterService(&service_);
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700259 cq_ = builder.AddCompletionQueue();
Yuchen Zenga42ec212016-04-29 13:03:06 -0700260
261 // TODO(zyc): make a test option to choose wheather sync plugins should be
262 // deleted
263 std::unique_ptr<ServerBuilderOption> sync_plugin_disabler(
264 new ServerBuilderSyncPluginDisabler());
265 builder.SetOption(move(sync_plugin_disabler));
Craig Tiller0220cf12015-02-12 17:39:26 -0800266 server_ = builder.BuildAndStart();
Vijay Paib65eda42016-02-16 13:48:05 -0800267
268 gpr_tls_set(&g_is_async_end2end_test, 1);
Craig Tiller0220cf12015-02-12 17:39:26 -0800269 }
270
Craig Tillercf133f42015-02-26 14:05:56 -0800271 void TearDown() GRPC_OVERRIDE {
Craig Tiller492968f2015-02-18 13:14:03 -0800272 server_->Shutdown();
273 void* ignored_tag;
274 bool ignored_ok;
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700275 cq_->Shutdown();
276 while (cq_->Next(&ignored_tag, &ignored_ok))
Craig Tiller492968f2015-02-18 13:14:03 -0800277 ;
Vijay Pai018879a2016-02-16 09:20:50 -0800278 poll_overrider_.reset();
Vijay Paib65eda42016-02-16 13:48:05 -0800279 gpr_tls_set(&g_is_async_end2end_test, 0);
David Klempner6fb122d2016-05-13 15:24:17 -0700280 grpc_recycle_unused_port(port_);
Craig Tiller492968f2015-02-18 13:14:03 -0800281 }
Craig Tiller0220cf12015-02-12 17:39:26 -0800282
283 void ResetStub() {
Vijay Paidf8b62c2016-05-02 14:34:24 -0700284 ChannelArguments args;
285 auto channel_creds =
286 GetChannelCredentials(GetParam().credentials_type, &args);
yang-g730055d2015-08-27 12:29:45 -0700287 std::shared_ptr<Channel> channel =
Vijay Paidf8b62c2016-05-02 14:34:24 -0700288 CreateCustomChannel(server_address_.str(), channel_creds, args);
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800289 stub_ = grpc::testing::EchoTestService::NewStub(channel);
Craig Tiller0220cf12015-02-12 17:39:26 -0800290 }
291
Yang Gao406b32f2015-02-13 16:25:33 -0800292 void SendRpc(int num_rpcs) {
293 for (int i = 0; i < num_rpcs; i++) {
294 EchoRequest send_request;
295 EchoRequest recv_request;
296 EchoResponse send_response;
297 EchoResponse recv_response;
298 Status recv_status;
299
300 ClientContext cli_ctx;
301 ServerContext srv_ctx;
302 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
303
Vijay Paid7b1e702016-05-02 15:10:21 -0700304 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800305 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700306 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
Yang Gao406b32f2015-02-13 16:25:33 -0800307
Craig Tillerd6c98df2015-08-18 09:33:44 -0700308 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
309 cq_.get(), tag(2));
Yang Gao406b32f2015-02-13 16:25:33 -0800310
Vijay Paidf8b62c2016-05-02 14:34:24 -0700311 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Yang Gao406b32f2015-02-13 16:25:33 -0800312 EXPECT_EQ(send_request.message(), recv_request.message());
313
314 send_response.set_message(recv_request.message());
315 response_writer.Finish(send_response, Status::OK, tag(3));
Yang Gao3a5e5492015-02-18 14:32:38 -0800316 response_reader->Finish(&recv_response, &recv_status, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700317 Verifier(GetParam().disable_blocking)
318 .Expect(3, true)
319 .Expect(4, true)
320 .Verify(cq_.get());
Yang Gao406b32f2015-02-13 16:25:33 -0800321
322 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700323 EXPECT_TRUE(recv_status.ok());
Yang Gao406b32f2015-02-13 16:25:33 -0800324 }
325 }
326
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700327 std::unique_ptr<ServerCompletionQueue> cq_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800328 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Craig Tiller0220cf12015-02-12 17:39:26 -0800329 std::unique_ptr<Server> server_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800330 grpc::testing::EchoTestService::AsyncService service_;
Craig Tiller0220cf12015-02-12 17:39:26 -0800331 std::ostringstream server_address_;
David Klempner6fb122d2016-05-13 15:24:17 -0700332 int port_;
vjpaicf4daeb2016-02-15 02:33:54 -0800333
Vijay Pai018879a2016-02-16 09:20:50 -0800334 std::unique_ptr<PollingOverrider> poll_overrider_;
Craig Tiller0220cf12015-02-12 17:39:26 -0800335};
336
Craig Tiller69f90e62015-08-06 08:32:35 -0700337TEST_P(AsyncEnd2endTest, SimpleRpc) {
Craig Tiller0220cf12015-02-12 17:39:26 -0800338 ResetStub();
Yang Gao406b32f2015-02-13 16:25:33 -0800339 SendRpc(1);
340}
Yang Gaobb84a302015-02-12 23:30:12 -0800341
Craig Tiller69f90e62015-08-06 08:32:35 -0700342TEST_P(AsyncEnd2endTest, SequentialRpcs) {
Yang Gao406b32f2015-02-13 16:25:33 -0800343 ResetStub();
344 SendRpc(10);
Craig Tiller0220cf12015-02-12 17:39:26 -0800345}
346
Yuchen Zeng77b74252016-07-19 11:53:37 -0700347// We do not need to protect notify because the use is synchronized.
348void ServerWait(Server* server, int* notify) {
349 server->Wait();
350 *notify = 1;
351}
352TEST_P(AsyncEnd2endTest, WaitAndShutdownTest) {
353 int notify = 0;
354 std::thread* wait_thread =
355 new std::thread(&ServerWait, server_.get(), &notify);
356 ResetStub();
357 SendRpc(1);
358 EXPECT_EQ(0, notify);
359 server_->Shutdown();
360 wait_thread->join();
361 EXPECT_EQ(1, notify);
362 delete wait_thread;
363}
364
365TEST_P(AsyncEnd2endTest, ShutdownThenWait) {
366 ResetStub();
367 SendRpc(1);
368 server_->Shutdown();
369 server_->Wait();
370}
371
vjpai7aadf462015-03-16 23:58:44 -0700372// Test a simple RPC using the async version of Next
Craig Tiller69f90e62015-08-06 08:32:35 -0700373TEST_P(AsyncEnd2endTest, AsyncNextRpc) {
vjpai7aadf462015-03-16 23:58:44 -0700374 ResetStub();
375
376 EchoRequest send_request;
377 EchoRequest recv_request;
378 EchoResponse send_response;
379 EchoResponse recv_response;
380 Status recv_status;
381
382 ClientContext cli_ctx;
383 ServerContext srv_ctx;
384 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
385
Vijay Paid7b1e702016-05-02 15:10:21 -0700386 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800387 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700388 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
vjpai7aadf462015-03-16 23:58:44 -0700389
Yang Gao757afae2015-03-17 15:49:26 -0700390 std::chrono::system_clock::time_point time_now(
Craig Tillerf51199f2015-05-08 09:32:53 -0700391 std::chrono::system_clock::now());
392 std::chrono::system_clock::time_point time_limit(
393 std::chrono::system_clock::now() + std::chrono::seconds(10));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700394 Verifier(GetParam().disable_blocking).Verify(cq_.get(), time_now);
395 Verifier(GetParam().disable_blocking).Verify(cq_.get(), time_now);
vjpai7aadf462015-03-16 23:58:44 -0700396
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700397 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
398 cq_.get(), tag(2));
vjpai7aadf462015-03-16 23:58:44 -0700399
Vijay Paidf8b62c2016-05-02 14:34:24 -0700400 Verifier(GetParam().disable_blocking)
401 .Expect(2, true)
402 .Verify(cq_.get(), time_limit);
vjpai7aadf462015-03-16 23:58:44 -0700403 EXPECT_EQ(send_request.message(), recv_request.message());
vjpai7aadf462015-03-16 23:58:44 -0700404
405 send_response.set_message(recv_request.message());
406 response_writer.Finish(send_response, Status::OK, tag(3));
vjpai7aadf462015-03-16 23:58:44 -0700407 response_reader->Finish(&recv_response, &recv_status, tag(4));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700408 Verifier(GetParam().disable_blocking)
Craig Tillere4d27482016-05-03 12:19:33 -0700409 .Expect(3, true)
Craig Tiller4c06b822015-08-06 08:41:31 -0700410 .Expect(4, true)
411 .Verify(cq_.get(), std::chrono::system_clock::time_point::max());
vjpai7aadf462015-03-16 23:58:44 -0700412
413 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700414 EXPECT_TRUE(recv_status.ok());
vjpai7aadf462015-03-16 23:58:44 -0700415}
Yang Gao757afae2015-03-17 15:49:26 -0700416
Yang Gao0e0d8e12015-02-13 14:40:41 -0800417// Two pings and a final pong.
Craig Tiller69f90e62015-08-06 08:32:35 -0700418TEST_P(AsyncEnd2endTest, SimpleClientStreaming) {
Yang Gao005f18a2015-02-13 10:22:33 -0800419 ResetStub();
420
421 EchoRequest send_request;
422 EchoRequest recv_request;
423 EchoResponse send_response;
424 EchoResponse recv_response;
425 Status recv_status;
426 ClientContext cli_ctx;
427 ServerContext srv_ctx;
428 ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx);
429
Vijay Paid7b1e702016-05-02 15:10:21 -0700430 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800431 std::unique_ptr<ClientAsyncWriter<EchoRequest>> cli_stream(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700432 stub_->AsyncRequestStream(&cli_ctx, &recv_response, cq_.get(), tag(1)));
Yang Gao005f18a2015-02-13 10:22:33 -0800433
Craig Tillerd6c98df2015-08-18 09:33:44 -0700434 service_.RequestRequestStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(),
435 tag(2));
Yang Gao005f18a2015-02-13 10:22:33 -0800436
Vijay Paidf8b62c2016-05-02 14:34:24 -0700437 Verifier(GetParam().disable_blocking)
438 .Expect(2, true)
439 .Expect(1, true)
440 .Verify(cq_.get());
Yang Gao005f18a2015-02-13 10:22:33 -0800441
442 cli_stream->Write(send_request, tag(3));
Yang Gao005f18a2015-02-13 10:22:33 -0800443 srv_stream.Read(&recv_request, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700444 Verifier(GetParam().disable_blocking)
445 .Expect(3, true)
446 .Expect(4, true)
447 .Verify(cq_.get());
Yang Gao005f18a2015-02-13 10:22:33 -0800448 EXPECT_EQ(send_request.message(), recv_request.message());
449
450 cli_stream->Write(send_request, tag(5));
Yang Gao005f18a2015-02-13 10:22:33 -0800451 srv_stream.Read(&recv_request, tag(6));
Craig Tillere4d27482016-05-03 12:19:33 -0700452 Verifier(GetParam().disable_blocking)
453 .Expect(5, true)
454 .Expect(6, true)
455 .Verify(cq_.get());
Yang Gao005f18a2015-02-13 10:22:33 -0800456
457 EXPECT_EQ(send_request.message(), recv_request.message());
458 cli_stream->WritesDone(tag(7));
Yang Gao005f18a2015-02-13 10:22:33 -0800459 srv_stream.Read(&recv_request, tag(8));
Craig Tillere4d27482016-05-03 12:19:33 -0700460 Verifier(GetParam().disable_blocking)
461 .Expect(7, true)
462 .Expect(8, false)
463 .Verify(cq_.get());
Yang Gao005f18a2015-02-13 10:22:33 -0800464
465 send_response.set_message(recv_request.message());
466 srv_stream.Finish(send_response, Status::OK, tag(9));
Yang Gao005f18a2015-02-13 10:22:33 -0800467 cli_stream->Finish(&recv_status, tag(10));
Craig Tillere4d27482016-05-03 12:19:33 -0700468 Verifier(GetParam().disable_blocking)
469 .Expect(9, true)
470 .Expect(10, true)
471 .Verify(cq_.get());
Yang Gao005f18a2015-02-13 10:22:33 -0800472
473 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700474 EXPECT_TRUE(recv_status.ok());
Yang Gao005f18a2015-02-13 10:22:33 -0800475}
476
Yang Gao0e0d8e12015-02-13 14:40:41 -0800477// One ping, two pongs.
Craig Tiller69f90e62015-08-06 08:32:35 -0700478TEST_P(AsyncEnd2endTest, SimpleServerStreaming) {
Yang Gao0e0d8e12015-02-13 14:40:41 -0800479 ResetStub();
480
481 EchoRequest send_request;
482 EchoRequest recv_request;
483 EchoResponse send_response;
484 EchoResponse recv_response;
485 Status recv_status;
486 ClientContext cli_ctx;
487 ServerContext srv_ctx;
488 ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx);
489
Vijay Paid7b1e702016-05-02 15:10:21 -0700490 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800491 std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700492 stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1)));
Yang Gao0e0d8e12015-02-13 14:40:41 -0800493
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700494 service_.RequestResponseStream(&srv_ctx, &recv_request, &srv_stream,
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700495 cq_.get(), cq_.get(), tag(2));
Yang Gao0e0d8e12015-02-13 14:40:41 -0800496
Vijay Paidf8b62c2016-05-02 14:34:24 -0700497 Verifier(GetParam().disable_blocking)
498 .Expect(1, true)
499 .Expect(2, true)
500 .Verify(cq_.get());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800501 EXPECT_EQ(send_request.message(), recv_request.message());
502
503 send_response.set_message(recv_request.message());
504 srv_stream.Write(send_response, tag(3));
Yang Gao0e0d8e12015-02-13 14:40:41 -0800505 cli_stream->Read(&recv_response, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700506 Verifier(GetParam().disable_blocking)
507 .Expect(3, true)
508 .Expect(4, true)
509 .Verify(cq_.get());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800510 EXPECT_EQ(send_response.message(), recv_response.message());
511
512 srv_stream.Write(send_response, tag(5));
Yang Gao0e0d8e12015-02-13 14:40:41 -0800513 cli_stream->Read(&recv_response, tag(6));
Craig Tillere4d27482016-05-03 12:19:33 -0700514 Verifier(GetParam().disable_blocking)
515 .Expect(5, true)
516 .Expect(6, true)
517 .Verify(cq_.get());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800518 EXPECT_EQ(send_response.message(), recv_response.message());
519
520 srv_stream.Finish(Status::OK, tag(7));
Yang Gao0e0d8e12015-02-13 14:40:41 -0800521 cli_stream->Read(&recv_response, tag(8));
Craig Tillere4d27482016-05-03 12:19:33 -0700522 Verifier(GetParam().disable_blocking)
523 .Expect(7, true)
524 .Expect(8, false)
525 .Verify(cq_.get());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800526
527 cli_stream->Finish(&recv_status, tag(9));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700528 Verifier(GetParam().disable_blocking).Expect(9, true).Verify(cq_.get());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800529
Yang Gaoc1a2c312015-06-16 10:59:46 -0700530 EXPECT_TRUE(recv_status.ok());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800531}
532
533// One ping, one pong.
Craig Tiller69f90e62015-08-06 08:32:35 -0700534TEST_P(AsyncEnd2endTest, SimpleBidiStreaming) {
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800535 ResetStub();
536
537 EchoRequest send_request;
538 EchoRequest recv_request;
539 EchoResponse send_response;
540 EchoResponse recv_response;
541 Status recv_status;
542 ClientContext cli_ctx;
543 ServerContext srv_ctx;
544 ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx);
545
Vijay Paid7b1e702016-05-02 15:10:21 -0700546 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800547 std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>>
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700548 cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1)));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800549
Craig Tillerd6c98df2015-08-18 09:33:44 -0700550 service_.RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(),
551 tag(2));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800552
Vijay Paidf8b62c2016-05-02 14:34:24 -0700553 Verifier(GetParam().disable_blocking)
554 .Expect(1, true)
555 .Expect(2, true)
556 .Verify(cq_.get());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800557
558 cli_stream->Write(send_request, tag(3));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800559 srv_stream.Read(&recv_request, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700560 Verifier(GetParam().disable_blocking)
561 .Expect(3, true)
562 .Expect(4, true)
563 .Verify(cq_.get());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800564 EXPECT_EQ(send_request.message(), recv_request.message());
565
566 send_response.set_message(recv_request.message());
567 srv_stream.Write(send_response, tag(5));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800568 cli_stream->Read(&recv_response, tag(6));
Craig Tillere4d27482016-05-03 12:19:33 -0700569 Verifier(GetParam().disable_blocking)
570 .Expect(5, true)
571 .Expect(6, true)
572 .Verify(cq_.get());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800573 EXPECT_EQ(send_response.message(), recv_response.message());
574
575 cli_stream->WritesDone(tag(7));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800576 srv_stream.Read(&recv_request, tag(8));
Craig Tillere4d27482016-05-03 12:19:33 -0700577 Verifier(GetParam().disable_blocking)
578 .Expect(7, true)
579 .Expect(8, false)
580 .Verify(cq_.get());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800581
582 srv_stream.Finish(Status::OK, tag(9));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800583 cli_stream->Finish(&recv_status, tag(10));
Craig Tillere4d27482016-05-03 12:19:33 -0700584 Verifier(GetParam().disable_blocking)
585 .Expect(9, true)
586 .Expect(10, true)
587 .Verify(cq_.get());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800588
Yang Gaoc1a2c312015-06-16 10:59:46 -0700589 EXPECT_TRUE(recv_status.ok());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800590}
591
Yang Gao406b32f2015-02-13 16:25:33 -0800592// Metadata tests
Craig Tiller69f90e62015-08-06 08:32:35 -0700593TEST_P(AsyncEnd2endTest, ClientInitialMetadataRpc) {
Yang Gao406b32f2015-02-13 16:25:33 -0800594 ResetStub();
595
596 EchoRequest send_request;
597 EchoRequest recv_request;
598 EchoResponse send_response;
599 EchoResponse recv_response;
600 Status recv_status;
601
602 ClientContext cli_ctx;
603 ServerContext srv_ctx;
604 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
605
Vijay Paid7b1e702016-05-02 15:10:21 -0700606 send_request.set_message(GetParam().message_content);
Yang Gao406b32f2015-02-13 16:25:33 -0800607 std::pair<grpc::string, grpc::string> meta1("key1", "val1");
608 std::pair<grpc::string, grpc::string> meta2("key2", "val2");
Craig Tiller6f871642016-02-03 16:15:31 -0800609 std::pair<grpc::string, grpc::string> meta3("g.r.d-bin", "xyz");
Yang Gao406b32f2015-02-13 16:25:33 -0800610 cli_ctx.AddMetadata(meta1.first, meta1.second);
611 cli_ctx.AddMetadata(meta2.first, meta2.second);
Craig Tiller6f871642016-02-03 16:15:31 -0800612 cli_ctx.AddMetadata(meta3.first, meta3.second);
Yang Gao406b32f2015-02-13 16:25:33 -0800613
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800614 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700615 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
Yang Gao406b32f2015-02-13 16:25:33 -0800616
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700617 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
618 cq_.get(), tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700619 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Yang Gao406b32f2015-02-13 16:25:33 -0800620 EXPECT_EQ(send_request.message(), recv_request.message());
621 auto client_initial_metadata = srv_ctx.client_metadata();
yang-ge21908f2015-08-25 13:47:51 -0700622 EXPECT_EQ(meta1.second,
623 ToString(client_initial_metadata.find(meta1.first)->second));
624 EXPECT_EQ(meta2.second,
625 ToString(client_initial_metadata.find(meta2.first)->second));
Craig Tiller6f871642016-02-03 16:15:31 -0800626 EXPECT_EQ(meta3.second,
627 ToString(client_initial_metadata.find(meta3.first)->second));
Craig Tiller8bf2dca2015-07-10 13:08:41 -0700628 EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2));
Yang Gao406b32f2015-02-13 16:25:33 -0800629
630 send_response.set_message(recv_request.message());
631 response_writer.Finish(send_response, Status::OK, tag(3));
Yang Gao3a5e5492015-02-18 14:32:38 -0800632 response_reader->Finish(&recv_response, &recv_status, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700633 Verifier(GetParam().disable_blocking)
634 .Expect(3, true)
635 .Expect(4, true)
636 .Verify(cq_.get());
Yang Gao406b32f2015-02-13 16:25:33 -0800637
638 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700639 EXPECT_TRUE(recv_status.ok());
Yang Gao406b32f2015-02-13 16:25:33 -0800640}
641
Craig Tiller69f90e62015-08-06 08:32:35 -0700642TEST_P(AsyncEnd2endTest, ServerInitialMetadataRpc) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800643 ResetStub();
644
645 EchoRequest send_request;
646 EchoRequest recv_request;
647 EchoResponse send_response;
648 EchoResponse recv_response;
649 Status recv_status;
650
651 ClientContext cli_ctx;
652 ServerContext srv_ctx;
653 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
654
Vijay Paid7b1e702016-05-02 15:10:21 -0700655 send_request.set_message(GetParam().message_content);
Yang Gao2b7f5372015-02-18 00:45:53 -0800656 std::pair<grpc::string, grpc::string> meta1("key1", "val1");
657 std::pair<grpc::string, grpc::string> meta2("key2", "val2");
658
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800659 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700660 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
Yang Gao2b7f5372015-02-18 00:45:53 -0800661
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700662 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
663 cq_.get(), tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700664 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800665 EXPECT_EQ(send_request.message(), recv_request.message());
666 srv_ctx.AddInitialMetadata(meta1.first, meta1.second);
667 srv_ctx.AddInitialMetadata(meta2.first, meta2.second);
668 response_writer.SendInitialMetadata(tag(3));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700669 Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800670
Yang Gao3a5e5492015-02-18 14:32:38 -0800671 response_reader->ReadInitialMetadata(tag(4));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700672 Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800673 auto server_initial_metadata = cli_ctx.GetServerInitialMetadata();
yang-ge21908f2015-08-25 13:47:51 -0700674 EXPECT_EQ(meta1.second,
675 ToString(server_initial_metadata.find(meta1.first)->second));
676 EXPECT_EQ(meta2.second,
677 ToString(server_initial_metadata.find(meta2.first)->second));
vjpaid5577aa2015-02-18 22:26:48 -0800678 EXPECT_EQ(static_cast<size_t>(2), server_initial_metadata.size());
Yang Gao3a5e5492015-02-18 14:32:38 -0800679
680 send_response.set_message(recv_request.message());
681 response_writer.Finish(send_response, Status::OK, tag(5));
Yang Gao3a5e5492015-02-18 14:32:38 -0800682 response_reader->Finish(&recv_response, &recv_status, tag(6));
Craig Tillere4d27482016-05-03 12:19:33 -0700683 Verifier(GetParam().disable_blocking)
684 .Expect(5, true)
685 .Expect(6, true)
686 .Verify(cq_.get());
Yang Gao3a5e5492015-02-18 14:32:38 -0800687
688 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700689 EXPECT_TRUE(recv_status.ok());
Yang Gao2b7f5372015-02-18 00:45:53 -0800690}
691
Craig Tiller69f90e62015-08-06 08:32:35 -0700692TEST_P(AsyncEnd2endTest, ServerTrailingMetadataRpc) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800693 ResetStub();
694
695 EchoRequest send_request;
696 EchoRequest recv_request;
697 EchoResponse send_response;
698 EchoResponse recv_response;
699 Status recv_status;
700
701 ClientContext cli_ctx;
702 ServerContext srv_ctx;
703 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
704
Vijay Paid7b1e702016-05-02 15:10:21 -0700705 send_request.set_message(GetParam().message_content);
Yang Gao2b7f5372015-02-18 00:45:53 -0800706 std::pair<grpc::string, grpc::string> meta1("key1", "val1");
707 std::pair<grpc::string, grpc::string> meta2("key2", "val2");
708
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800709 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700710 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
Yang Gao2b7f5372015-02-18 00:45:53 -0800711
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700712 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
713 cq_.get(), tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700714 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800715 EXPECT_EQ(send_request.message(), recv_request.message());
716 response_writer.SendInitialMetadata(tag(3));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700717 Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800718
719 send_response.set_message(recv_request.message());
720 srv_ctx.AddTrailingMetadata(meta1.first, meta1.second);
721 srv_ctx.AddTrailingMetadata(meta2.first, meta2.second);
722 response_writer.Finish(send_response, Status::OK, tag(4));
Yang Gao3a5e5492015-02-18 14:32:38 -0800723 response_reader->Finish(&recv_response, &recv_status, tag(5));
Craig Tillere4d27482016-05-03 12:19:33 -0700724
725 Verifier(GetParam().disable_blocking)
726 .Expect(4, true)
727 .Expect(5, true)
728 .Verify(cq_.get());
729
Yang Gao2b7f5372015-02-18 00:45:53 -0800730 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700731 EXPECT_TRUE(recv_status.ok());
Yang Gao2b7f5372015-02-18 00:45:53 -0800732 auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata();
yang-ge21908f2015-08-25 13:47:51 -0700733 EXPECT_EQ(meta1.second,
734 ToString(server_trailing_metadata.find(meta1.first)->second));
735 EXPECT_EQ(meta2.second,
736 ToString(server_trailing_metadata.find(meta2.first)->second));
vjpaid5577aa2015-02-18 22:26:48 -0800737 EXPECT_EQ(static_cast<size_t>(2), server_trailing_metadata.size());
Yang Gao2b7f5372015-02-18 00:45:53 -0800738}
739
Craig Tiller69f90e62015-08-06 08:32:35 -0700740TEST_P(AsyncEnd2endTest, MetadataRpc) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800741 ResetStub();
742
743 EchoRequest send_request;
744 EchoRequest recv_request;
745 EchoResponse send_response;
746 EchoResponse recv_response;
747 Status recv_status;
748
749 ClientContext cli_ctx;
750 ServerContext srv_ctx;
751 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
752
Vijay Paid7b1e702016-05-02 15:10:21 -0700753 send_request.set_message(GetParam().message_content);
Yang Gao2b7f5372015-02-18 00:45:53 -0800754 std::pair<grpc::string, grpc::string> meta1("key1", "val1");
Yang Gao3a5e5492015-02-18 14:32:38 -0800755 std::pair<grpc::string, grpc::string> meta2(
Vijay Pai92a928f2015-03-26 16:30:22 -0400756 "key2-bin",
Craig Tillerd6c98df2015-08-18 09:33:44 -0700757 grpc::string("\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc", 13));
Yang Gao2b7f5372015-02-18 00:45:53 -0800758 std::pair<grpc::string, grpc::string> meta3("key3", "val3");
Craig Tiller47c83fd2015-02-21 22:45:35 -0800759 std::pair<grpc::string, grpc::string> meta6(
760 "key4-bin",
Vijay Pai92a928f2015-03-26 16:30:22 -0400761 grpc::string("\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d",
Craig Tillerd6c98df2015-08-18 09:33:44 -0700762 14));
Yang Gao2b7f5372015-02-18 00:45:53 -0800763 std::pair<grpc::string, grpc::string> meta5("key5", "val5");
Craig Tiller47c83fd2015-02-21 22:45:35 -0800764 std::pair<grpc::string, grpc::string> meta4(
765 "key6-bin",
Craig Tillerd6c98df2015-08-18 09:33:44 -0700766 grpc::string(
767 "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee", 15));
Yang Gao2b7f5372015-02-18 00:45:53 -0800768
769 cli_ctx.AddMetadata(meta1.first, meta1.second);
770 cli_ctx.AddMetadata(meta2.first, meta2.second);
771
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800772 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700773 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
Yang Gao2b7f5372015-02-18 00:45:53 -0800774
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700775 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
776 cq_.get(), tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700777 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800778 EXPECT_EQ(send_request.message(), recv_request.message());
779 auto client_initial_metadata = srv_ctx.client_metadata();
yang-ge21908f2015-08-25 13:47:51 -0700780 EXPECT_EQ(meta1.second,
781 ToString(client_initial_metadata.find(meta1.first)->second));
782 EXPECT_EQ(meta2.second,
783 ToString(client_initial_metadata.find(meta2.first)->second));
Craig Tiller8bf2dca2015-07-10 13:08:41 -0700784 EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2));
Yang Gao2b7f5372015-02-18 00:45:53 -0800785
786 srv_ctx.AddInitialMetadata(meta3.first, meta3.second);
787 srv_ctx.AddInitialMetadata(meta4.first, meta4.second);
788 response_writer.SendInitialMetadata(tag(3));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700789 Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get());
Yang Gao3a5e5492015-02-18 14:32:38 -0800790 response_reader->ReadInitialMetadata(tag(4));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700791 Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800792 auto server_initial_metadata = cli_ctx.GetServerInitialMetadata();
yang-ge21908f2015-08-25 13:47:51 -0700793 EXPECT_EQ(meta3.second,
794 ToString(server_initial_metadata.find(meta3.first)->second));
795 EXPECT_EQ(meta4.second,
796 ToString(server_initial_metadata.find(meta4.first)->second));
Craig Tiller8bf2dca2015-07-10 13:08:41 -0700797 EXPECT_GE(server_initial_metadata.size(), static_cast<size_t>(2));
Yang Gao3a5e5492015-02-18 14:32:38 -0800798
799 send_response.set_message(recv_request.message());
800 srv_ctx.AddTrailingMetadata(meta5.first, meta5.second);
801 srv_ctx.AddTrailingMetadata(meta6.first, meta6.second);
802 response_writer.Finish(send_response, Status::OK, tag(5));
Yang Gao3a5e5492015-02-18 14:32:38 -0800803 response_reader->Finish(&recv_response, &recv_status, tag(6));
Craig Tillere4d27482016-05-03 12:19:33 -0700804
805 Verifier(GetParam().disable_blocking)
806 .Expect(5, true)
807 .Expect(6, true)
808 .Verify(cq_.get());
809
Yang Gao3a5e5492015-02-18 14:32:38 -0800810 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700811 EXPECT_TRUE(recv_status.ok());
Yang Gao2b7f5372015-02-18 00:45:53 -0800812 auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata();
yang-ge21908f2015-08-25 13:47:51 -0700813 EXPECT_EQ(meta5.second,
814 ToString(server_trailing_metadata.find(meta5.first)->second));
815 EXPECT_EQ(meta6.second,
816 ToString(server_trailing_metadata.find(meta6.first)->second));
Craig Tiller8bf2dca2015-07-10 13:08:41 -0700817 EXPECT_GE(server_trailing_metadata.size(), static_cast<size_t>(2));
Yang Gao2b7f5372015-02-18 00:45:53 -0800818}
yang-gb3352562015-08-04 14:42:06 -0700819
820// Server uses AsyncNotifyWhenDone API to check for cancellation
Craig Tiller69f90e62015-08-06 08:32:35 -0700821TEST_P(AsyncEnd2endTest, ServerCheckCancellation) {
yang-gb3352562015-08-04 14:42:06 -0700822 ResetStub();
823
824 EchoRequest send_request;
825 EchoRequest recv_request;
826 EchoResponse send_response;
827 EchoResponse recv_response;
828 Status recv_status;
829
830 ClientContext cli_ctx;
831 ServerContext srv_ctx;
832 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
833
Vijay Paid7b1e702016-05-02 15:10:21 -0700834 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800835 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
yang-gb3352562015-08-04 14:42:06 -0700836 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
837
838 srv_ctx.AsyncNotifyWhenDone(tag(5));
839 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
840 cq_.get(), tag(2));
841
Vijay Paidf8b62c2016-05-02 14:34:24 -0700842 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
yang-gb3352562015-08-04 14:42:06 -0700843 EXPECT_EQ(send_request.message(), recv_request.message());
844
845 cli_ctx.TryCancel();
Vijay Paidf8b62c2016-05-02 14:34:24 -0700846 Verifier(GetParam().disable_blocking).Expect(5, true).Verify(cq_.get());
yang-gb3352562015-08-04 14:42:06 -0700847 EXPECT_TRUE(srv_ctx.IsCancelled());
848
849 response_reader->Finish(&recv_response, &recv_status, tag(4));
yang-g15759f62016-06-01 11:21:27 -0700850 Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get());
yang-gb3352562015-08-04 14:42:06 -0700851
852 EXPECT_EQ(StatusCode::CANCELLED, recv_status.error_code());
853}
854
855// Server uses AsyncNotifyWhenDone API to check for normal finish
Craig Tiller69f90e62015-08-06 08:32:35 -0700856TEST_P(AsyncEnd2endTest, ServerCheckDone) {
yang-gb3352562015-08-04 14:42:06 -0700857 ResetStub();
858
859 EchoRequest send_request;
860 EchoRequest recv_request;
861 EchoResponse send_response;
862 EchoResponse recv_response;
863 Status recv_status;
864
865 ClientContext cli_ctx;
866 ServerContext srv_ctx;
867 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
868
Vijay Paid7b1e702016-05-02 15:10:21 -0700869 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800870 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
yang-gb3352562015-08-04 14:42:06 -0700871 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
872
873 srv_ctx.AsyncNotifyWhenDone(tag(5));
874 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
875 cq_.get(), tag(2));
876
Vijay Paidf8b62c2016-05-02 14:34:24 -0700877 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
yang-gb3352562015-08-04 14:42:06 -0700878 EXPECT_EQ(send_request.message(), recv_request.message());
879
880 send_response.set_message(recv_request.message());
881 response_writer.Finish(send_response, Status::OK, tag(3));
yang-gb3352562015-08-04 14:42:06 -0700882 response_reader->Finish(&recv_response, &recv_status, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700883 Verifier(GetParam().disable_blocking)
884 .Expect(3, true)
885 .Expect(4, true)
886 .Expect(5, true)
887 .Verify(cq_.get());
888 EXPECT_FALSE(srv_ctx.IsCancelled());
yang-gb3352562015-08-04 14:42:06 -0700889
890 EXPECT_EQ(send_response.message(), recv_response.message());
891 EXPECT_TRUE(recv_status.ok());
892}
893
Craig Tiller8f7bff72015-08-17 13:23:14 -0700894TEST_P(AsyncEnd2endTest, UnimplementedRpc) {
Vijay Paidf8b62c2016-05-02 14:34:24 -0700895 ChannelArguments args;
896 auto channel_creds =
897 GetChannelCredentials(GetParam().credentials_type, &args);
yang-g730055d2015-08-27 12:29:45 -0700898 std::shared_ptr<Channel> channel =
Vijay Paidf8b62c2016-05-02 14:34:24 -0700899 CreateCustomChannel(server_address_.str(), channel_creds, args);
Craig Tiller1b4e3302015-12-17 16:35:00 -0800900 std::unique_ptr<grpc::testing::UnimplementedService::Stub> stub;
901 stub = grpc::testing::UnimplementedService::NewStub(channel);
yang-g9b7757d2015-08-13 11:15:53 -0700902 EchoRequest send_request;
903 EchoResponse recv_response;
904 Status recv_status;
905
906 ClientContext cli_ctx;
Vijay Paid7b1e702016-05-02 15:10:21 -0700907 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800908 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
yang-g9b7757d2015-08-13 11:15:53 -0700909 stub->AsyncUnimplemented(&cli_ctx, send_request, cq_.get()));
910
911 response_reader->Finish(&recv_response, &recv_status, tag(4));
yang-g15759f62016-06-01 11:21:27 -0700912 Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get());
yang-g9b7757d2015-08-13 11:15:53 -0700913
914 EXPECT_EQ(StatusCode::UNIMPLEMENTED, recv_status.error_code());
915 EXPECT_EQ("", recv_status.error_message());
916}
917
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800918// This class is for testing scenarios where RPCs are cancelled on the server
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800919// by calling ServerContext::TryCancel(). Server uses AsyncNotifyWhenDone
920// API to check for cancellation
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800921class AsyncEnd2endServerTryCancelTest : public AsyncEnd2endTest {
922 protected:
923 typedef enum {
924 DO_NOT_CANCEL = 0,
925 CANCEL_BEFORE_PROCESSING,
926 CANCEL_DURING_PROCESSING,
927 CANCEL_AFTER_PROCESSING
928 } ServerTryCancelRequestPhase;
929
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800930 // Helper for testing client-streaming RPCs which are cancelled on the server.
931 // Depending on the value of server_try_cancel parameter, this will test one
932 // of the following three scenarios:
933 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading
934 // any messages from the client
935 //
936 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading
937 // messages from the client
938 //
939 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all
940 // messages from the client (but before sending any status back to the
941 // client)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800942 void TestClientStreamingServerCancel(
943 ServerTryCancelRequestPhase server_try_cancel) {
944 ResetStub();
945
946 EchoRequest send_request;
947 EchoRequest recv_request;
948 EchoResponse send_response;
949 EchoResponse recv_response;
950 Status recv_status;
951
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800952 ClientContext cli_ctx;
953 ServerContext srv_ctx;
954 ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx);
955
956 // Initiate the 'RequestStream' call on client
957 std::unique_ptr<ClientAsyncWriter<EchoRequest>> cli_stream(
Sree Kuchibhotla4fb59082016-01-29 11:16:24 -0800958 stub_->AsyncRequestStream(&cli_ctx, &recv_response, cq_.get(), tag(1)));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700959 Verifier(GetParam().disable_blocking).Expect(1, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800960
961 // On the server, request to be notified of 'RequestStream' calls
962 // and receive the 'RequestStream' call just made by the client
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800963 srv_ctx.AsyncNotifyWhenDone(tag(11));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800964 service_.RequestRequestStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(),
965 tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700966 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800967
968 // Client sends 3 messages (tags 3, 4 and 5)
969 for (int tag_idx = 3; tag_idx <= 5; tag_idx++) {
Vijay Paia63271c2016-06-15 12:56:38 -0700970 send_request.set_message("Ping " + grpc::to_string(tag_idx));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800971 cli_stream->Write(send_request, tag(tag_idx));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700972 Verifier(GetParam().disable_blocking)
973 .Expect(tag_idx, true)
974 .Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800975 }
976 cli_stream->WritesDone(tag(6));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700977 Verifier(GetParam().disable_blocking).Expect(6, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800978
979 bool expected_server_cq_result = true;
980 bool ignore_cq_result = false;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800981 bool want_done_tag = false;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800982
983 if (server_try_cancel == CANCEL_BEFORE_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800984 srv_ctx.TryCancel();
Vijay Paidf8b62c2016-05-02 14:34:24 -0700985 Verifier(GetParam().disable_blocking).Expect(11, true).Verify(cq_.get());
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800986 EXPECT_TRUE(srv_ctx.IsCancelled());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800987
988 // Since cancellation is done before server reads any results, we know
989 // for sure that all cq results will return false from this point forward
990 expected_server_cq_result = false;
991 }
992
993 std::thread* server_try_cancel_thd = NULL;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800994
Vijay Paidf8b62c2016-05-02 14:34:24 -0700995 auto verif = Verifier(GetParam().disable_blocking);
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800996
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800997 if (server_try_cancel == CANCEL_DURING_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800998 server_try_cancel_thd =
999 new std::thread(&ServerContext::TryCancel, &srv_ctx);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001000 // Server will cancel the RPC in a parallel thread while reading the
1001 // requests from the client. Since the cancellation can happen at anytime,
1002 // some of the cq results (i.e those until cancellation) might be true but
1003 // its non deterministic. So better to ignore the cq results
1004 ignore_cq_result = true;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001005 // Expect that we might possibly see the done tag that
1006 // indicates cancellation completion in this case
1007 want_done_tag = true;
1008 verif.Expect(11, true);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001009 }
1010
1011 // Server reads 3 messages (tags 6, 7 and 8)
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001012 // But if want_done_tag is true, we might also see tag 11
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001013 for (int tag_idx = 6; tag_idx <= 8; tag_idx++) {
1014 srv_stream.Read(&recv_request, tag(tag_idx));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001015 // Note that we'll add something to the verifier and verify that
1016 // something was seen, but it might be tag 11 and not what we
1017 // just added
1018 int got_tag = verif.Expect(tag_idx, expected_server_cq_result)
1019 .Next(cq_.get(), ignore_cq_result);
1020 GPR_ASSERT((got_tag == tag_idx) || (got_tag == 11 && want_done_tag));
1021 if (got_tag == 11) {
1022 EXPECT_TRUE(srv_ctx.IsCancelled());
1023 want_done_tag = false;
1024 // Now get the other entry that we were waiting on
1025 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), tag_idx);
1026 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001027 }
1028
1029 if (server_try_cancel_thd != NULL) {
1030 server_try_cancel_thd->join();
1031 delete server_try_cancel_thd;
1032 }
1033
1034 if (server_try_cancel == CANCEL_AFTER_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001035 srv_ctx.TryCancel();
1036 want_done_tag = true;
1037 verif.Expect(11, true);
1038 }
1039
1040 if (want_done_tag) {
1041 verif.Verify(cq_.get());
1042 EXPECT_TRUE(srv_ctx.IsCancelled());
1043 want_done_tag = false;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001044 }
1045
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001046 // The RPC has been cancelled at this point for sure (i.e irrespective of
1047 // the value of `server_try_cancel` is). So, from this point forward, we
1048 // know that cq results are supposed to return false on server.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001049
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001050 // Server sends the final message and cancelled status (but the RPC is
1051 // already cancelled at this point. So we expect the operation to fail)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001052 srv_stream.Finish(send_response, Status::CANCELLED, tag(9));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001053 Verifier(GetParam().disable_blocking).Expect(9, false).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001054
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001055 // Client will see the cancellation
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001056 cli_stream->Finish(&recv_status, tag(10));
yang-g15759f62016-06-01 11:21:27 -07001057 Verifier(GetParam().disable_blocking).Expect(10, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001058 EXPECT_FALSE(recv_status.ok());
1059 EXPECT_EQ(::grpc::StatusCode::CANCELLED, recv_status.error_code());
1060 }
1061
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001062 // Helper for testing server-streaming RPCs which are cancelled on the server.
1063 // Depending on the value of server_try_cancel parameter, this will test one
1064 // of the following three scenarios:
1065 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before sending
1066 // any messages to the client
1067 //
1068 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while sending
1069 // messages to the client
1070 //
1071 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after sending all
1072 // messages to the client (but before sending any status back to the
1073 // client)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001074 void TestServerStreamingServerCancel(
1075 ServerTryCancelRequestPhase server_try_cancel) {
1076 ResetStub();
1077
1078 EchoRequest send_request;
1079 EchoRequest recv_request;
1080 EchoResponse send_response;
1081 EchoResponse recv_response;
1082 Status recv_status;
1083 ClientContext cli_ctx;
1084 ServerContext srv_ctx;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001085 ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx);
1086
1087 send_request.set_message("Ping");
1088 // Initiate the 'ResponseStream' call on the client
1089 std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream(
Sree Kuchibhotla4fb59082016-01-29 11:16:24 -08001090 stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1)));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001091 Verifier(GetParam().disable_blocking).Expect(1, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001092 // On the server, request to be notified of 'ResponseStream' calls and
1093 // receive the call just made by the client
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001094 srv_ctx.AsyncNotifyWhenDone(tag(11));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001095 service_.RequestResponseStream(&srv_ctx, &recv_request, &srv_stream,
1096 cq_.get(), cq_.get(), tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001097 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001098 EXPECT_EQ(send_request.message(), recv_request.message());
1099
1100 bool expected_cq_result = true;
1101 bool ignore_cq_result = false;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001102 bool want_done_tag = false;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001103
1104 if (server_try_cancel == CANCEL_BEFORE_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001105 srv_ctx.TryCancel();
Vijay Paidf8b62c2016-05-02 14:34:24 -07001106 Verifier(GetParam().disable_blocking).Expect(11, true).Verify(cq_.get());
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001107 EXPECT_TRUE(srv_ctx.IsCancelled());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001108
1109 // We know for sure that all cq results will be false from this point
1110 // since the server cancelled the RPC
1111 expected_cq_result = false;
1112 }
1113
1114 std::thread* server_try_cancel_thd = NULL;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001115
Vijay Paidf8b62c2016-05-02 14:34:24 -07001116 auto verif = Verifier(GetParam().disable_blocking);
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001117
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001118 if (server_try_cancel == CANCEL_DURING_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001119 server_try_cancel_thd =
1120 new std::thread(&ServerContext::TryCancel, &srv_ctx);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001121
1122 // Server will cancel the RPC in a parallel thread while writing responses
1123 // to the client. Since the cancellation can happen at anytime, some of
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001124 // the cq results (i.e those until cancellation) might be true but it is
1125 // non deterministic. So better to ignore the cq results
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001126 ignore_cq_result = true;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001127 // Expect that we might possibly see the done tag that
1128 // indicates cancellation completion in this case
1129 want_done_tag = true;
1130 verif.Expect(11, true);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001131 }
1132
1133 // Server sends three messages (tags 3, 4 and 5)
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001134 // But if want_done tag is true, we might also see tag 11
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001135 for (int tag_idx = 3; tag_idx <= 5; tag_idx++) {
Vijay Paia63271c2016-06-15 12:56:38 -07001136 send_response.set_message("Pong " + grpc::to_string(tag_idx));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001137 srv_stream.Write(send_response, tag(tag_idx));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001138 // Note that we'll add something to the verifier and verify that
1139 // something was seen, but it might be tag 11 and not what we
1140 // just added
1141 int got_tag = verif.Expect(tag_idx, expected_cq_result)
1142 .Next(cq_.get(), ignore_cq_result);
1143 GPR_ASSERT((got_tag == tag_idx) || (got_tag == 11 && want_done_tag));
1144 if (got_tag == 11) {
1145 EXPECT_TRUE(srv_ctx.IsCancelled());
1146 want_done_tag = false;
1147 // Now get the other entry that we were waiting on
1148 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), tag_idx);
1149 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001150 }
1151
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001152 if (server_try_cancel_thd != NULL) {
1153 server_try_cancel_thd->join();
1154 delete server_try_cancel_thd;
1155 }
1156
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001157 if (server_try_cancel == CANCEL_AFTER_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001158 srv_ctx.TryCancel();
1159 want_done_tag = true;
1160 verif.Expect(11, true);
yang-gad0df7b2016-02-22 10:00:20 -08001161
1162 // Client reads may fail bacause it is notified that the stream is
1163 // cancelled.
1164 ignore_cq_result = true;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001165 }
1166
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001167 if (want_done_tag) {
1168 verif.Verify(cq_.get());
1169 EXPECT_TRUE(srv_ctx.IsCancelled());
1170 want_done_tag = false;
1171 }
1172
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001173 // Client attemts to read the three messages from the server
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001174 for (int tag_idx = 6; tag_idx <= 8; tag_idx++) {
1175 cli_stream->Read(&recv_response, tag(tag_idx));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001176 Verifier(GetParam().disable_blocking)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001177 .Expect(tag_idx, expected_cq_result)
Sree Kuchibhotla4fb59082016-01-29 11:16:24 -08001178 .Verify(cq_.get(), ignore_cq_result);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001179 }
1180
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001181 // The RPC has been cancelled at this point for sure (i.e irrespective of
1182 // the value of `server_try_cancel` is). So, from this point forward, we
1183 // know that cq results are supposed to return false on server.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001184
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001185 // Server finishes the stream (but the RPC is already cancelled)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001186 srv_stream.Finish(Status::CANCELLED, tag(9));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001187 Verifier(GetParam().disable_blocking).Expect(9, false).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001188
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001189 // Client will see the cancellation
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001190 cli_stream->Finish(&recv_status, tag(10));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001191 Verifier(GetParam().disable_blocking).Expect(10, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001192 EXPECT_FALSE(recv_status.ok());
1193 EXPECT_EQ(::grpc::StatusCode::CANCELLED, recv_status.error_code());
1194 }
1195
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001196 // Helper for testing bidirectinal-streaming RPCs which are cancelled on the
1197 // server.
1198 //
1199 // Depending on the value of server_try_cancel parameter, this will
1200 // test one of the following three scenarios:
1201 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/
1202 // writing any messages from/to the client
1203 //
1204 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading
1205 // messages from the client
1206 //
1207 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all
1208 // messages from the client (but before sending any status back to the
1209 // client)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001210 void TestBidiStreamingServerCancel(
1211 ServerTryCancelRequestPhase server_try_cancel) {
1212 ResetStub();
1213
1214 EchoRequest send_request;
1215 EchoRequest recv_request;
1216 EchoResponse send_response;
1217 EchoResponse recv_response;
1218 Status recv_status;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001219 ClientContext cli_ctx;
1220 ServerContext srv_ctx;
1221 ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx);
1222
1223 // Initiate the call from the client side
1224 std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>>
Sree Kuchibhotla4fb59082016-01-29 11:16:24 -08001225 cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1)));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001226 Verifier(GetParam().disable_blocking).Expect(1, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001227
1228 // On the server, request to be notified of the 'BidiStream' call and
1229 // receive the call just made by the client
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001230 srv_ctx.AsyncNotifyWhenDone(tag(11));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001231 service_.RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(),
1232 tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001233 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001234
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001235 // Client sends the first and the only message
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001236 send_request.set_message("Ping");
1237 cli_stream->Write(send_request, tag(3));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001238 Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001239
1240 bool expected_cq_result = true;
1241 bool ignore_cq_result = false;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001242 bool want_done_tag = false;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001243
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001244 if (server_try_cancel == CANCEL_BEFORE_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001245 srv_ctx.TryCancel();
Vijay Paidf8b62c2016-05-02 14:34:24 -07001246 Verifier(GetParam().disable_blocking).Expect(11, true).Verify(cq_.get());
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001247 EXPECT_TRUE(srv_ctx.IsCancelled());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001248
1249 // We know for sure that all cq results will be false from this point
1250 // since the server cancelled the RPC
1251 expected_cq_result = false;
1252 }
1253
1254 std::thread* server_try_cancel_thd = NULL;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001255
Vijay Paidf8b62c2016-05-02 14:34:24 -07001256 auto verif = Verifier(GetParam().disable_blocking);
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001257
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001258 if (server_try_cancel == CANCEL_DURING_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001259 server_try_cancel_thd =
1260 new std::thread(&ServerContext::TryCancel, &srv_ctx);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001261
1262 // Since server is going to cancel the RPC in a parallel thread, some of
1263 // the cq results (i.e those until the cancellation) might be true. Since
1264 // that number is non-deterministic, it is better to ignore the cq results
1265 ignore_cq_result = true;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001266 // Expect that we might possibly see the done tag that
1267 // indicates cancellation completion in this case
1268 want_done_tag = true;
1269 verif.Expect(11, true);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001270 }
1271
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001272 int got_tag;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001273 srv_stream.Read(&recv_request, tag(4));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001274 verif.Expect(4, expected_cq_result);
1275 got_tag = verif.Next(cq_.get(), ignore_cq_result);
1276 GPR_ASSERT((got_tag == 4) || (got_tag == 11 && want_done_tag));
1277 if (got_tag == 11) {
1278 EXPECT_TRUE(srv_ctx.IsCancelled());
1279 want_done_tag = false;
1280 // Now get the other entry that we were waiting on
1281 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 4);
1282 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001283
1284 send_response.set_message("Pong");
1285 srv_stream.Write(send_response, tag(5));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001286 verif.Expect(5, expected_cq_result);
1287 got_tag = verif.Next(cq_.get(), ignore_cq_result);
1288 GPR_ASSERT((got_tag == 5) || (got_tag == 11 && want_done_tag));
1289 if (got_tag == 11) {
1290 EXPECT_TRUE(srv_ctx.IsCancelled());
1291 want_done_tag = false;
1292 // Now get the other entry that we were waiting on
1293 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 5);
1294 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001295
1296 cli_stream->Read(&recv_response, tag(6));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001297 verif.Expect(6, expected_cq_result);
1298 got_tag = verif.Next(cq_.get(), ignore_cq_result);
1299 GPR_ASSERT((got_tag == 6) || (got_tag == 11 && want_done_tag));
1300 if (got_tag == 11) {
1301 EXPECT_TRUE(srv_ctx.IsCancelled());
1302 want_done_tag = false;
1303 // Now get the other entry that we were waiting on
1304 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 6);
1305 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001306
1307 // This is expected to succeed in all cases
1308 cli_stream->WritesDone(tag(7));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001309 verif.Expect(7, true);
1310 got_tag = verif.Next(cq_.get(), ignore_cq_result);
1311 GPR_ASSERT((got_tag == 7) || (got_tag == 11 && want_done_tag));
1312 if (got_tag == 11) {
1313 EXPECT_TRUE(srv_ctx.IsCancelled());
1314 want_done_tag = false;
1315 // Now get the other entry that we were waiting on
1316 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 7);
1317 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001318
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001319 // This is expected to fail in all cases i.e for all values of
Vijay Pai018879a2016-02-16 09:20:50 -08001320 // server_try_cancel. This is because at this point, either there are no
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001321 // more msgs from the client (because client called WritesDone) or the RPC
1322 // is cancelled on the server
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001323 srv_stream.Read(&recv_request, tag(8));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001324 verif.Expect(8, false);
1325 got_tag = verif.Next(cq_.get(), ignore_cq_result);
1326 GPR_ASSERT((got_tag == 8) || (got_tag == 11 && want_done_tag));
1327 if (got_tag == 11) {
1328 EXPECT_TRUE(srv_ctx.IsCancelled());
1329 want_done_tag = false;
1330 // Now get the other entry that we were waiting on
1331 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 8);
1332 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001333
1334 if (server_try_cancel_thd != NULL) {
1335 server_try_cancel_thd->join();
1336 delete server_try_cancel_thd;
1337 }
1338
1339 if (server_try_cancel == CANCEL_AFTER_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001340 srv_ctx.TryCancel();
1341 want_done_tag = true;
1342 verif.Expect(11, true);
1343 }
1344
1345 if (want_done_tag) {
1346 verif.Verify(cq_.get());
1347 EXPECT_TRUE(srv_ctx.IsCancelled());
1348 want_done_tag = false;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001349 }
1350
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001351 // The RPC has been cancelled at this point for sure (i.e irrespective of
1352 // the value of `server_try_cancel` is). So, from this point forward, we
1353 // know that cq results are supposed to return false on server.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001354
1355 srv_stream.Finish(Status::CANCELLED, tag(9));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001356 Verifier(GetParam().disable_blocking).Expect(9, false).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001357
1358 cli_stream->Finish(&recv_status, tag(10));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001359 Verifier(GetParam().disable_blocking).Expect(10, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001360 EXPECT_FALSE(recv_status.ok());
1361 EXPECT_EQ(grpc::StatusCode::CANCELLED, recv_status.error_code());
1362 }
1363};
1364
1365TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelBefore) {
1366 TestClientStreamingServerCancel(CANCEL_BEFORE_PROCESSING);
1367}
1368
1369TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelDuring) {
1370 TestClientStreamingServerCancel(CANCEL_DURING_PROCESSING);
1371}
1372
1373TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelAfter) {
1374 TestClientStreamingServerCancel(CANCEL_AFTER_PROCESSING);
1375}
1376
1377TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelBefore) {
1378 TestServerStreamingServerCancel(CANCEL_BEFORE_PROCESSING);
1379}
1380
1381TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelDuring) {
1382 TestServerStreamingServerCancel(CANCEL_DURING_PROCESSING);
1383}
1384
1385TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelAfter) {
1386 TestServerStreamingServerCancel(CANCEL_AFTER_PROCESSING);
1387}
1388
1389TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelBefore) {
1390 TestBidiStreamingServerCancel(CANCEL_BEFORE_PROCESSING);
1391}
1392
1393TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelDuring) {
1394 TestBidiStreamingServerCancel(CANCEL_DURING_PROCESSING);
1395}
1396
1397TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelAfter) {
1398 TestBidiStreamingServerCancel(CANCEL_AFTER_PROCESSING);
1399}
1400
Vijay Paidf8b62c2016-05-02 14:34:24 -07001401std::vector<TestScenario> CreateTestScenarios(bool test_disable_blocking,
Vijay Paid7b1e702016-05-02 15:10:21 -07001402 bool test_secure,
1403 int test_big_limit) {
Vijay Paidf8b62c2016-05-02 14:34:24 -07001404 std::vector<TestScenario> scenarios;
1405 std::vector<grpc::string> credentials_types;
Vijay Paid7b1e702016-05-02 15:10:21 -07001406 std::vector<grpc::string> messages;
1407
Vijay Paidf8b62c2016-05-02 14:34:24 -07001408 credentials_types.push_back(kInsecureCredentialsType);
Vijay Paid7b1e702016-05-02 15:10:21 -07001409 auto sec_list = GetSecureCredentialsTypeList();
1410 for (auto sec = sec_list.begin(); sec != sec_list.end(); sec++) {
1411 credentials_types.push_back(*sec);
1412 }
1413
1414 messages.push_back("Hello");
1415 for (int sz = 1; sz < test_big_limit; sz *= 2) {
1416 grpc::string big_msg;
1417 for (int i = 0; i < sz * 1024; i++) {
1418 char c = 'a' + (i % 26);
1419 big_msg += c;
1420 }
1421 messages.push_back(big_msg);
1422 }
1423
1424 for (auto cred = credentials_types.begin(); cred != credentials_types.end();
1425 ++cred) {
1426 for (auto msg = messages.begin(); msg != messages.end(); msg++) {
Vijay Pai679c75f2016-06-15 13:08:00 -07001427 scenarios.emplace_back(false, *cred, *msg);
Vijay Paid7b1e702016-05-02 15:10:21 -07001428 if (test_disable_blocking) {
Vijay Pai679c75f2016-06-15 13:08:00 -07001429 scenarios.emplace_back(true, *cred, *msg);
Vijay Paid7b1e702016-05-02 15:10:21 -07001430 }
Vijay Paidf8b62c2016-05-02 14:34:24 -07001431 }
1432 }
1433 return scenarios;
1434}
1435
Craig Tiller4c06b822015-08-06 08:41:31 -07001436INSTANTIATE_TEST_CASE_P(AsyncEnd2end, AsyncEnd2endTest,
Vijay Paid7b1e702016-05-02 15:10:21 -07001437 ::testing::ValuesIn(CreateTestScenarios(true, true,
1438 1024)));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001439INSTANTIATE_TEST_CASE_P(AsyncEnd2endServerTryCancel,
1440 AsyncEnd2endServerTryCancelTest,
Vijay Paid7b1e702016-05-02 15:10:21 -07001441 ::testing::ValuesIn(CreateTestScenarios(false, false,
1442 0)));
Craig Tiller69f90e62015-08-06 08:32:35 -07001443
Craig Tiller0220cf12015-02-12 17:39:26 -08001444} // namespace
1445} // namespace testing
1446} // namespace grpc
1447
1448int main(int argc, char** argv) {
1449 grpc_test_init(argc, argv);
Vijay Paib65eda42016-02-16 13:48:05 -08001450 gpr_tls_init(&g_is_async_end2end_test);
Craig Tiller0220cf12015-02-12 17:39:26 -08001451 ::testing::InitGoogleTest(&argc, argv);
Vijay Paib65eda42016-02-16 13:48:05 -08001452 int ret = RUN_ALL_TESTS();
1453 gpr_tls_destroy(&g_is_async_end2end_test);
1454 return ret;
Craig Tiller0220cf12015-02-12 17:39:26 -08001455}