blob: 662ccbd1c2cceeaabd55b559e50738315e3222b2 [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
murgatroid9954070892016-08-08 17:01:18 -070050#include "src/core/lib/iomgr/port.h"
Craig Tiller1b4e3302015-12-17 16:35:00 -080051#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
52#include "src/proto/grpc/testing/echo.grpc.pb.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080053#include "test/core/util/port.h"
54#include "test/core/util/test_config.h"
yang-ge21908f2015-08-25 13:47:51 -070055#include "test/cpp/util/string_ref_helper.h"
Vijay Paidf8b62c2016-05-02 14:34:24 -070056#include "test/cpp/util/test_credentials_provider.h"
Craig Tiller0220cf12015-02-12 17:39:26 -080057
Craig Tiller69f90e62015-08-06 08:32:35 -070058#ifdef GPR_POSIX_SOCKET
Craig Tillerf45496f2016-03-30 07:41:19 -070059#include "src/core/lib/iomgr/ev_posix.h"
Craig Tiller69f90e62015-08-06 08:32:35 -070060#endif
61
Craig Tiller1b4e3302015-12-17 16:35:00 -080062using grpc::testing::EchoRequest;
63using grpc::testing::EchoResponse;
Vijay Paidf8b62c2016-05-02 14:34:24 -070064using grpc::testing::kTlsCredentialsType;
Craig Tiller0220cf12015-02-12 17:39:26 -080065using std::chrono::system_clock;
66
Vijay Paib65eda42016-02-16 13:48:05 -080067GPR_TLS_DECL(g_is_async_end2end_test);
68
Craig Tiller0220cf12015-02-12 17:39:26 -080069namespace grpc {
70namespace testing {
71
72namespace {
73
Craig Tiller7536af02015-12-22 13:49:30 -080074void* tag(int i) { return (void*)(intptr_t)i; }
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -080075int detag(void* p) { return static_cast<int>(reinterpret_cast<intptr_t>(p)); }
Yang Gaoc05b6cb2015-02-13 00:34:10 -080076
Craig Tiller69f90e62015-08-06 08:32:35 -070077#ifdef GPR_POSIX_SOCKET
Vijay Paib65eda42016-02-16 13:48:05 -080078static int maybe_assert_non_blocking_poll(struct pollfd* pfds, nfds_t nfds,
79 int timeout) {
80 if (gpr_tls_get(&g_is_async_end2end_test)) {
81 GPR_ASSERT(timeout == 0);
82 }
83 return poll(pfds, nfds, timeout);
Craig Tiller69f90e62015-08-06 08:32:35 -070084}
85
86class PollOverride {
Craig Tiller06cf3cc2015-05-13 13:11:01 -070087 public:
Craig Tiller69f90e62015-08-06 08:32:35 -070088 PollOverride(grpc_poll_function_type f) {
89 prev_ = grpc_poll_function;
90 grpc_poll_function = f;
91 }
92
Craig Tiller4c06b822015-08-06 08:41:31 -070093 ~PollOverride() { grpc_poll_function = prev_; }
Craig Tiller69f90e62015-08-06 08:32:35 -070094
95 private:
96 grpc_poll_function_type prev_;
97};
98
vjpaicf4daeb2016-02-15 02:33:54 -080099class PollingOverrider : public PollOverride {
Craig Tiller69f90e62015-08-06 08:32:35 -0700100 public:
vjpaicf4daeb2016-02-15 02:33:54 -0800101 explicit PollingOverrider(bool allow_blocking)
Vijay Paib65eda42016-02-16 13:48:05 -0800102 : PollOverride(allow_blocking ? poll : maybe_assert_non_blocking_poll) {}
Craig Tiller69f90e62015-08-06 08:32:35 -0700103};
104#else
vjpaicf4daeb2016-02-15 02:33:54 -0800105class PollingOverrider {
Craig Tiller69f90e62015-08-06 08:32:35 -0700106 public:
vjpaicf4daeb2016-02-15 02:33:54 -0800107 explicit PollingOverrider(bool allow_blocking) {}
Craig Tiller69f90e62015-08-06 08:32:35 -0700108};
109#endif
110
vjpaicf4daeb2016-02-15 02:33:54 -0800111class Verifier {
Craig Tiller69f90e62015-08-06 08:32:35 -0700112 public:
vjpaicf4daeb2016-02-15 02:33:54 -0800113 explicit Verifier(bool spin) : spin_(spin) {}
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800114 // Expect sets the expected ok value for a specific tag
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700115 Verifier& Expect(int i, bool expect_ok) {
116 expectations_[tag(i)] = expect_ok;
117 return *this;
vjpai7aadf462015-03-16 23:58:44 -0700118 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800119
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800120 // Next waits for 1 async tag to complete, checks its
121 // expectations, and returns the tag
122 int Next(CompletionQueue* cq, bool ignore_ok) {
123 bool ok;
124 void* got_tag;
125 if (spin_) {
126 for (;;) {
127 auto r = cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME));
128 if (r == CompletionQueue::TIMEOUT) continue;
129 if (r == CompletionQueue::GOT_EVENT) break;
130 gpr_log(GPR_ERROR, "unexpected result from AsyncNext");
131 abort();
132 }
133 } else {
134 EXPECT_TRUE(cq->Next(&got_tag, &ok));
135 }
136 auto it = expectations_.find(got_tag);
137 EXPECT_TRUE(it != expectations_.end());
138 if (!ignore_ok) {
139 EXPECT_EQ(it->second, ok);
140 }
141 expectations_.erase(it);
142 return detag(got_tag);
143 }
144
145 // Verify keeps calling Next until all currently set
146 // expected tags are complete
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800147 void Verify(CompletionQueue* cq) { Verify(cq, false); }
148
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800149 // This version of Verify allows optionally ignoring the
150 // outcome of the expectation
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800151 void Verify(CompletionQueue* cq, bool ignore_ok) {
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700152 GPR_ASSERT(!expectations_.empty());
153 while (!expectations_.empty()) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800154 Next(cq, ignore_ok);
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700155 }
156 }
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800157 // This version of Verify stops after a certain deadline
Craig Tillerd6c98df2015-08-18 09:33:44 -0700158 void Verify(CompletionQueue* cq,
159 std::chrono::system_clock::time_point deadline) {
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700160 if (expectations_.empty()) {
161 bool ok;
Craig Tillerd6c98df2015-08-18 09:33:44 -0700162 void* got_tag;
Craig Tiller69f90e62015-08-06 08:32:35 -0700163 if (spin_) {
164 while (std::chrono::system_clock::now() < deadline) {
Craig Tiller4c06b822015-08-06 08:41:31 -0700165 EXPECT_EQ(
166 cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME)),
167 CompletionQueue::TIMEOUT);
Craig Tiller69f90e62015-08-06 08:32:35 -0700168 }
169 } else {
Craig Tiller4c06b822015-08-06 08:41:31 -0700170 EXPECT_EQ(cq->AsyncNext(&got_tag, &ok, deadline),
171 CompletionQueue::TIMEOUT);
Craig Tiller69f90e62015-08-06 08:32:35 -0700172 }
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700173 } else {
174 while (!expectations_.empty()) {
175 bool ok;
Craig Tillerd6c98df2015-08-18 09:33:44 -0700176 void* got_tag;
Craig Tiller69f90e62015-08-06 08:32:35 -0700177 if (spin_) {
178 for (;;) {
179 GPR_ASSERT(std::chrono::system_clock::now() < deadline);
Craig Tiller4c06b822015-08-06 08:41:31 -0700180 auto r =
181 cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME));
Craig Tiller69f90e62015-08-06 08:32:35 -0700182 if (r == CompletionQueue::TIMEOUT) continue;
183 if (r == CompletionQueue::GOT_EVENT) break;
184 gpr_log(GPR_ERROR, "unexpected result from AsyncNext");
185 abort();
Craig Tiller4c06b822015-08-06 08:41:31 -0700186 }
Craig Tiller69f90e62015-08-06 08:32:35 -0700187 } else {
Craig Tiller4c06b822015-08-06 08:41:31 -0700188 EXPECT_EQ(cq->AsyncNext(&got_tag, &ok, deadline),
189 CompletionQueue::GOT_EVENT);
Craig Tiller69f90e62015-08-06 08:32:35 -0700190 }
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700191 auto it = expectations_.find(got_tag);
192 EXPECT_TRUE(it != expectations_.end());
193 EXPECT_EQ(it->second, ok);
194 expectations_.erase(it);
195 }
196 }
197 }
198
199 private:
200 std::map<void*, bool> expectations_;
Craig Tiller69f90e62015-08-06 08:32:35 -0700201 bool spin_;
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700202};
vjpai7aadf462015-03-16 23:58:44 -0700203
Yuchen Zeng13184792016-07-18 13:07:42 -0700204bool plugin_has_sync_methods(std::unique_ptr<ServerBuilderPlugin>& plugin) {
205 return plugin->has_sync_methods();
206}
207
Yuchen Zenga42ec212016-04-29 13:03:06 -0700208// This class disables the server builder plugins that may add sync services to
209// the server. If there are sync services, UnimplementedRpc test will triger
210// the sync unkown rpc routine on the server side, rather than the async one
211// that needs to be tested here.
212class ServerBuilderSyncPluginDisabler : public ::grpc::ServerBuilderOption {
213 public:
214 void UpdateArguments(ChannelArguments* arg) GRPC_OVERRIDE {}
215
vjpai97da6472016-06-13 09:56:26 -0700216 void UpdatePlugins(std::vector<std::unique_ptr<ServerBuilderPlugin>>* plugins)
Yuchen Zenga42ec212016-04-29 13:03:06 -0700217 GRPC_OVERRIDE {
Yuchen Zeng13184792016-07-18 13:07:42 -0700218 plugins->erase(std::remove_if(plugins->begin(), plugins->end(),
219 plugin_has_sync_methods),
220 plugins->end());
Yuchen Zenga42ec212016-04-29 13:03:06 -0700221 }
222};
223
Vijay Paidf8b62c2016-05-02 14:34:24 -0700224class TestScenario {
225 public:
Vijay Paid7b1e702016-05-02 15:10:21 -0700226 TestScenario(bool non_block, const grpc::string& creds_type,
227 const grpc::string& content)
228 : disable_blocking(non_block),
229 credentials_type(creds_type),
230 message_content(content) {}
231 void Log() const {
Yuchen Zengbeaa3572016-06-10 15:46:14 -0700232 gpr_log(
233 GPR_INFO,
234 "Scenario: disable_blocking %d, credentials %s, message size %" PRIuPTR,
235 disable_blocking, credentials_type.c_str(), message_content.size());
Vijay Paid7b1e702016-05-02 15:10:21 -0700236 }
Vijay Paidf8b62c2016-05-02 14:34:24 -0700237 bool disable_blocking;
Vijay Pai679c75f2016-06-15 13:08:00 -0700238 // Although the below grpc::string's are logically const, we can't declare
239 // them const because of a limitation in the way old compilers (e.g., gcc-4.4)
240 // manage vector insertion using a copy constructor
241 grpc::string credentials_type;
242 grpc::string message_content;
Vijay Paidf8b62c2016-05-02 14:34:24 -0700243};
244
245class AsyncEnd2endTest : public ::testing::TestWithParam<TestScenario> {
Craig Tiller0220cf12015-02-12 17:39:26 -0800246 protected:
Vijay Paid7b1e702016-05-02 15:10:21 -0700247 AsyncEnd2endTest() { GetParam().Log(); }
Craig Tiller0220cf12015-02-12 17:39:26 -0800248
Craig Tillercf133f42015-02-26 14:05:56 -0800249 void SetUp() GRPC_OVERRIDE {
Vijay Paidf8b62c2016-05-02 14:34:24 -0700250 poll_overrider_.reset(new PollingOverrider(!GetParam().disable_blocking));
Vijay Pai018879a2016-02-16 09:20:50 -0800251
David Klempner6fb122d2016-05-13 15:24:17 -0700252 port_ = grpc_pick_unused_port_or_die();
253 server_address_ << "localhost:" << port_;
vjpai017ed622015-12-09 10:42:54 -0800254
Craig Tiller0220cf12015-02-12 17:39:26 -0800255 // Setup server
256 ServerBuilder builder;
Vijay Paidf8b62c2016-05-02 14:34:24 -0700257 auto server_creds = GetServerCredentials(GetParam().credentials_type);
258 builder.AddListeningPort(server_address_.str(), server_creds);
Craig Tiller15f383c2016-01-07 12:45:32 -0800259 builder.RegisterService(&service_);
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700260 cq_ = builder.AddCompletionQueue();
Yuchen Zenga42ec212016-04-29 13:03:06 -0700261
262 // TODO(zyc): make a test option to choose wheather sync plugins should be
263 // deleted
264 std::unique_ptr<ServerBuilderOption> sync_plugin_disabler(
265 new ServerBuilderSyncPluginDisabler());
266 builder.SetOption(move(sync_plugin_disabler));
Craig Tiller0220cf12015-02-12 17:39:26 -0800267 server_ = builder.BuildAndStart();
Vijay Paib65eda42016-02-16 13:48:05 -0800268
269 gpr_tls_set(&g_is_async_end2end_test, 1);
Craig Tiller0220cf12015-02-12 17:39:26 -0800270 }
271
Craig Tillercf133f42015-02-26 14:05:56 -0800272 void TearDown() GRPC_OVERRIDE {
Craig Tiller492968f2015-02-18 13:14:03 -0800273 server_->Shutdown();
274 void* ignored_tag;
275 bool ignored_ok;
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700276 cq_->Shutdown();
277 while (cq_->Next(&ignored_tag, &ignored_ok))
Craig Tiller492968f2015-02-18 13:14:03 -0800278 ;
Vijay Pai018879a2016-02-16 09:20:50 -0800279 poll_overrider_.reset();
Vijay Paib65eda42016-02-16 13:48:05 -0800280 gpr_tls_set(&g_is_async_end2end_test, 0);
David Klempner6fb122d2016-05-13 15:24:17 -0700281 grpc_recycle_unused_port(port_);
Craig Tiller492968f2015-02-18 13:14:03 -0800282 }
Craig Tiller0220cf12015-02-12 17:39:26 -0800283
284 void ResetStub() {
Vijay Paidf8b62c2016-05-02 14:34:24 -0700285 ChannelArguments args;
286 auto channel_creds =
287 GetChannelCredentials(GetParam().credentials_type, &args);
yang-g730055d2015-08-27 12:29:45 -0700288 std::shared_ptr<Channel> channel =
Vijay Paidf8b62c2016-05-02 14:34:24 -0700289 CreateCustomChannel(server_address_.str(), channel_creds, args);
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800290 stub_ = grpc::testing::EchoTestService::NewStub(channel);
Craig Tiller0220cf12015-02-12 17:39:26 -0800291 }
292
Yang Gao406b32f2015-02-13 16:25:33 -0800293 void SendRpc(int num_rpcs) {
294 for (int i = 0; i < num_rpcs; i++) {
295 EchoRequest send_request;
296 EchoRequest recv_request;
297 EchoResponse send_response;
298 EchoResponse recv_response;
299 Status recv_status;
300
301 ClientContext cli_ctx;
302 ServerContext srv_ctx;
303 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
304
Vijay Paid7b1e702016-05-02 15:10:21 -0700305 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800306 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700307 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
Yang Gao406b32f2015-02-13 16:25:33 -0800308
Craig Tillerd6c98df2015-08-18 09:33:44 -0700309 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
310 cq_.get(), tag(2));
Yang Gao406b32f2015-02-13 16:25:33 -0800311
Vijay Paidf8b62c2016-05-02 14:34:24 -0700312 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Yang Gao406b32f2015-02-13 16:25:33 -0800313 EXPECT_EQ(send_request.message(), recv_request.message());
314
315 send_response.set_message(recv_request.message());
316 response_writer.Finish(send_response, Status::OK, tag(3));
Yang Gao3a5e5492015-02-18 14:32:38 -0800317 response_reader->Finish(&recv_response, &recv_status, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700318 Verifier(GetParam().disable_blocking)
319 .Expect(3, true)
320 .Expect(4, true)
321 .Verify(cq_.get());
Yang Gao406b32f2015-02-13 16:25:33 -0800322
323 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700324 EXPECT_TRUE(recv_status.ok());
Yang Gao406b32f2015-02-13 16:25:33 -0800325 }
326 }
327
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700328 std::unique_ptr<ServerCompletionQueue> cq_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800329 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Craig Tiller0220cf12015-02-12 17:39:26 -0800330 std::unique_ptr<Server> server_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800331 grpc::testing::EchoTestService::AsyncService service_;
Craig Tiller0220cf12015-02-12 17:39:26 -0800332 std::ostringstream server_address_;
David Klempner6fb122d2016-05-13 15:24:17 -0700333 int port_;
vjpaicf4daeb2016-02-15 02:33:54 -0800334
Vijay Pai018879a2016-02-16 09:20:50 -0800335 std::unique_ptr<PollingOverrider> poll_overrider_;
Craig Tiller0220cf12015-02-12 17:39:26 -0800336};
337
Craig Tiller69f90e62015-08-06 08:32:35 -0700338TEST_P(AsyncEnd2endTest, SimpleRpc) {
Craig Tiller0220cf12015-02-12 17:39:26 -0800339 ResetStub();
Yang Gao406b32f2015-02-13 16:25:33 -0800340 SendRpc(1);
341}
Yang Gaobb84a302015-02-12 23:30:12 -0800342
Craig Tiller69f90e62015-08-06 08:32:35 -0700343TEST_P(AsyncEnd2endTest, SequentialRpcs) {
Yang Gao406b32f2015-02-13 16:25:33 -0800344 ResetStub();
345 SendRpc(10);
Craig Tiller0220cf12015-02-12 17:39:26 -0800346}
347
Yuchen Zeng77b74252016-07-19 11:53:37 -0700348// We do not need to protect notify because the use is synchronized.
349void ServerWait(Server* server, int* notify) {
350 server->Wait();
351 *notify = 1;
352}
353TEST_P(AsyncEnd2endTest, WaitAndShutdownTest) {
354 int notify = 0;
355 std::thread* wait_thread =
356 new std::thread(&ServerWait, server_.get(), &notify);
357 ResetStub();
358 SendRpc(1);
359 EXPECT_EQ(0, notify);
360 server_->Shutdown();
361 wait_thread->join();
362 EXPECT_EQ(1, notify);
363 delete wait_thread;
364}
365
366TEST_P(AsyncEnd2endTest, ShutdownThenWait) {
367 ResetStub();
368 SendRpc(1);
369 server_->Shutdown();
370 server_->Wait();
371}
372
vjpai7aadf462015-03-16 23:58:44 -0700373// Test a simple RPC using the async version of Next
Craig Tiller69f90e62015-08-06 08:32:35 -0700374TEST_P(AsyncEnd2endTest, AsyncNextRpc) {
vjpai7aadf462015-03-16 23:58:44 -0700375 ResetStub();
376
377 EchoRequest send_request;
378 EchoRequest recv_request;
379 EchoResponse send_response;
380 EchoResponse recv_response;
381 Status recv_status;
382
383 ClientContext cli_ctx;
384 ServerContext srv_ctx;
385 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
386
Vijay Paid7b1e702016-05-02 15:10:21 -0700387 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800388 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700389 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
vjpai7aadf462015-03-16 23:58:44 -0700390
Yang Gao757afae2015-03-17 15:49:26 -0700391 std::chrono::system_clock::time_point time_now(
Craig Tillerf51199f2015-05-08 09:32:53 -0700392 std::chrono::system_clock::now());
393 std::chrono::system_clock::time_point time_limit(
394 std::chrono::system_clock::now() + std::chrono::seconds(10));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700395 Verifier(GetParam().disable_blocking).Verify(cq_.get(), time_now);
396 Verifier(GetParam().disable_blocking).Verify(cq_.get(), time_now);
vjpai7aadf462015-03-16 23:58:44 -0700397
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700398 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
399 cq_.get(), tag(2));
vjpai7aadf462015-03-16 23:58:44 -0700400
Vijay Paidf8b62c2016-05-02 14:34:24 -0700401 Verifier(GetParam().disable_blocking)
402 .Expect(2, true)
403 .Verify(cq_.get(), time_limit);
vjpai7aadf462015-03-16 23:58:44 -0700404 EXPECT_EQ(send_request.message(), recv_request.message());
vjpai7aadf462015-03-16 23:58:44 -0700405
406 send_response.set_message(recv_request.message());
407 response_writer.Finish(send_response, Status::OK, tag(3));
vjpai7aadf462015-03-16 23:58:44 -0700408 response_reader->Finish(&recv_response, &recv_status, tag(4));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700409 Verifier(GetParam().disable_blocking)
Craig Tillere4d27482016-05-03 12:19:33 -0700410 .Expect(3, true)
Craig Tiller4c06b822015-08-06 08:41:31 -0700411 .Expect(4, true)
412 .Verify(cq_.get(), std::chrono::system_clock::time_point::max());
vjpai7aadf462015-03-16 23:58:44 -0700413
414 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700415 EXPECT_TRUE(recv_status.ok());
vjpai7aadf462015-03-16 23:58:44 -0700416}
Yang Gao757afae2015-03-17 15:49:26 -0700417
Yang Gao0e0d8e12015-02-13 14:40:41 -0800418// Two pings and a final pong.
Craig Tiller69f90e62015-08-06 08:32:35 -0700419TEST_P(AsyncEnd2endTest, SimpleClientStreaming) {
Yang Gao005f18a2015-02-13 10:22:33 -0800420 ResetStub();
421
422 EchoRequest send_request;
423 EchoRequest recv_request;
424 EchoResponse send_response;
425 EchoResponse recv_response;
426 Status recv_status;
427 ClientContext cli_ctx;
428 ServerContext srv_ctx;
429 ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx);
430
Vijay Paid7b1e702016-05-02 15:10:21 -0700431 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800432 std::unique_ptr<ClientAsyncWriter<EchoRequest>> cli_stream(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700433 stub_->AsyncRequestStream(&cli_ctx, &recv_response, cq_.get(), tag(1)));
Yang Gao005f18a2015-02-13 10:22:33 -0800434
Craig Tillerd6c98df2015-08-18 09:33:44 -0700435 service_.RequestRequestStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(),
436 tag(2));
Yang Gao005f18a2015-02-13 10:22:33 -0800437
Vijay Paidf8b62c2016-05-02 14:34:24 -0700438 Verifier(GetParam().disable_blocking)
439 .Expect(2, true)
440 .Expect(1, true)
441 .Verify(cq_.get());
Yang Gao005f18a2015-02-13 10:22:33 -0800442
443 cli_stream->Write(send_request, tag(3));
Yang Gao005f18a2015-02-13 10:22:33 -0800444 srv_stream.Read(&recv_request, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700445 Verifier(GetParam().disable_blocking)
446 .Expect(3, true)
447 .Expect(4, true)
448 .Verify(cq_.get());
Yang Gao005f18a2015-02-13 10:22:33 -0800449 EXPECT_EQ(send_request.message(), recv_request.message());
450
451 cli_stream->Write(send_request, tag(5));
Yang Gao005f18a2015-02-13 10:22:33 -0800452 srv_stream.Read(&recv_request, tag(6));
Craig Tillere4d27482016-05-03 12:19:33 -0700453 Verifier(GetParam().disable_blocking)
454 .Expect(5, true)
455 .Expect(6, true)
456 .Verify(cq_.get());
Yang Gao005f18a2015-02-13 10:22:33 -0800457
458 EXPECT_EQ(send_request.message(), recv_request.message());
459 cli_stream->WritesDone(tag(7));
Yang Gao005f18a2015-02-13 10:22:33 -0800460 srv_stream.Read(&recv_request, tag(8));
Craig Tillere4d27482016-05-03 12:19:33 -0700461 Verifier(GetParam().disable_blocking)
462 .Expect(7, true)
463 .Expect(8, false)
464 .Verify(cq_.get());
Yang Gao005f18a2015-02-13 10:22:33 -0800465
466 send_response.set_message(recv_request.message());
467 srv_stream.Finish(send_response, Status::OK, tag(9));
Yang Gao005f18a2015-02-13 10:22:33 -0800468 cli_stream->Finish(&recv_status, tag(10));
Craig Tillere4d27482016-05-03 12:19:33 -0700469 Verifier(GetParam().disable_blocking)
470 .Expect(9, true)
471 .Expect(10, true)
472 .Verify(cq_.get());
Yang Gao005f18a2015-02-13 10:22:33 -0800473
474 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700475 EXPECT_TRUE(recv_status.ok());
Yang Gao005f18a2015-02-13 10:22:33 -0800476}
477
Yang Gao0e0d8e12015-02-13 14:40:41 -0800478// One ping, two pongs.
Craig Tiller69f90e62015-08-06 08:32:35 -0700479TEST_P(AsyncEnd2endTest, SimpleServerStreaming) {
Yang Gao0e0d8e12015-02-13 14:40:41 -0800480 ResetStub();
481
482 EchoRequest send_request;
483 EchoRequest recv_request;
484 EchoResponse send_response;
485 EchoResponse recv_response;
486 Status recv_status;
487 ClientContext cli_ctx;
488 ServerContext srv_ctx;
489 ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx);
490
Vijay Paid7b1e702016-05-02 15:10:21 -0700491 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800492 std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700493 stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1)));
Yang Gao0e0d8e12015-02-13 14:40:41 -0800494
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700495 service_.RequestResponseStream(&srv_ctx, &recv_request, &srv_stream,
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700496 cq_.get(), cq_.get(), tag(2));
Yang Gao0e0d8e12015-02-13 14:40:41 -0800497
Vijay Paidf8b62c2016-05-02 14:34:24 -0700498 Verifier(GetParam().disable_blocking)
499 .Expect(1, true)
500 .Expect(2, true)
501 .Verify(cq_.get());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800502 EXPECT_EQ(send_request.message(), recv_request.message());
503
504 send_response.set_message(recv_request.message());
505 srv_stream.Write(send_response, tag(3));
Yang Gao0e0d8e12015-02-13 14:40:41 -0800506 cli_stream->Read(&recv_response, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700507 Verifier(GetParam().disable_blocking)
508 .Expect(3, true)
509 .Expect(4, true)
510 .Verify(cq_.get());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800511 EXPECT_EQ(send_response.message(), recv_response.message());
512
513 srv_stream.Write(send_response, tag(5));
Yang Gao0e0d8e12015-02-13 14:40:41 -0800514 cli_stream->Read(&recv_response, tag(6));
Craig Tillere4d27482016-05-03 12:19:33 -0700515 Verifier(GetParam().disable_blocking)
516 .Expect(5, true)
517 .Expect(6, true)
518 .Verify(cq_.get());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800519 EXPECT_EQ(send_response.message(), recv_response.message());
520
521 srv_stream.Finish(Status::OK, tag(7));
Yang Gao0e0d8e12015-02-13 14:40:41 -0800522 cli_stream->Read(&recv_response, tag(8));
Craig Tillere4d27482016-05-03 12:19:33 -0700523 Verifier(GetParam().disable_blocking)
524 .Expect(7, true)
525 .Expect(8, false)
526 .Verify(cq_.get());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800527
528 cli_stream->Finish(&recv_status, tag(9));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700529 Verifier(GetParam().disable_blocking).Expect(9, true).Verify(cq_.get());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800530
Yang Gaoc1a2c312015-06-16 10:59:46 -0700531 EXPECT_TRUE(recv_status.ok());
Yang Gao0e0d8e12015-02-13 14:40:41 -0800532}
533
534// One ping, one pong.
Craig Tiller69f90e62015-08-06 08:32:35 -0700535TEST_P(AsyncEnd2endTest, SimpleBidiStreaming) {
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800536 ResetStub();
537
538 EchoRequest send_request;
539 EchoRequest recv_request;
540 EchoResponse send_response;
541 EchoResponse recv_response;
542 Status recv_status;
543 ClientContext cli_ctx;
544 ServerContext srv_ctx;
545 ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx);
546
Vijay Paid7b1e702016-05-02 15:10:21 -0700547 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800548 std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>>
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700549 cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1)));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800550
Craig Tillerd6c98df2015-08-18 09:33:44 -0700551 service_.RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(),
552 tag(2));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800553
Vijay Paidf8b62c2016-05-02 14:34:24 -0700554 Verifier(GetParam().disable_blocking)
555 .Expect(1, true)
556 .Expect(2, true)
557 .Verify(cq_.get());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800558
559 cli_stream->Write(send_request, tag(3));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800560 srv_stream.Read(&recv_request, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700561 Verifier(GetParam().disable_blocking)
562 .Expect(3, true)
563 .Expect(4, true)
564 .Verify(cq_.get());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800565 EXPECT_EQ(send_request.message(), recv_request.message());
566
567 send_response.set_message(recv_request.message());
568 srv_stream.Write(send_response, tag(5));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800569 cli_stream->Read(&recv_response, tag(6));
Craig Tillere4d27482016-05-03 12:19:33 -0700570 Verifier(GetParam().disable_blocking)
571 .Expect(5, true)
572 .Expect(6, true)
573 .Verify(cq_.get());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800574 EXPECT_EQ(send_response.message(), recv_response.message());
575
576 cli_stream->WritesDone(tag(7));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800577 srv_stream.Read(&recv_request, tag(8));
Craig Tillere4d27482016-05-03 12:19:33 -0700578 Verifier(GetParam().disable_blocking)
579 .Expect(7, true)
580 .Expect(8, false)
581 .Verify(cq_.get());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800582
583 srv_stream.Finish(Status::OK, tag(9));
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800584 cli_stream->Finish(&recv_status, tag(10));
Craig Tillere4d27482016-05-03 12:19:33 -0700585 Verifier(GetParam().disable_blocking)
586 .Expect(9, true)
587 .Expect(10, true)
588 .Verify(cq_.get());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800589
Yang Gaoc1a2c312015-06-16 10:59:46 -0700590 EXPECT_TRUE(recv_status.ok());
Yang Gaoc05b6cb2015-02-13 00:34:10 -0800591}
592
Yang Gao406b32f2015-02-13 16:25:33 -0800593// Metadata tests
Craig Tiller69f90e62015-08-06 08:32:35 -0700594TEST_P(AsyncEnd2endTest, ClientInitialMetadataRpc) {
Yang Gao406b32f2015-02-13 16:25:33 -0800595 ResetStub();
596
597 EchoRequest send_request;
598 EchoRequest recv_request;
599 EchoResponse send_response;
600 EchoResponse recv_response;
601 Status recv_status;
602
603 ClientContext cli_ctx;
604 ServerContext srv_ctx;
605 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
606
Vijay Paid7b1e702016-05-02 15:10:21 -0700607 send_request.set_message(GetParam().message_content);
Yang Gao406b32f2015-02-13 16:25:33 -0800608 std::pair<grpc::string, grpc::string> meta1("key1", "val1");
609 std::pair<grpc::string, grpc::string> meta2("key2", "val2");
Craig Tiller6f871642016-02-03 16:15:31 -0800610 std::pair<grpc::string, grpc::string> meta3("g.r.d-bin", "xyz");
Yang Gao406b32f2015-02-13 16:25:33 -0800611 cli_ctx.AddMetadata(meta1.first, meta1.second);
612 cli_ctx.AddMetadata(meta2.first, meta2.second);
Craig Tiller6f871642016-02-03 16:15:31 -0800613 cli_ctx.AddMetadata(meta3.first, meta3.second);
Yang Gao406b32f2015-02-13 16:25:33 -0800614
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800615 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700616 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
Yang Gao406b32f2015-02-13 16:25:33 -0800617
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700618 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
619 cq_.get(), tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700620 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Yang Gao406b32f2015-02-13 16:25:33 -0800621 EXPECT_EQ(send_request.message(), recv_request.message());
622 auto client_initial_metadata = srv_ctx.client_metadata();
yang-ge21908f2015-08-25 13:47:51 -0700623 EXPECT_EQ(meta1.second,
624 ToString(client_initial_metadata.find(meta1.first)->second));
625 EXPECT_EQ(meta2.second,
626 ToString(client_initial_metadata.find(meta2.first)->second));
Craig Tiller6f871642016-02-03 16:15:31 -0800627 EXPECT_EQ(meta3.second,
628 ToString(client_initial_metadata.find(meta3.first)->second));
Craig Tiller8bf2dca2015-07-10 13:08:41 -0700629 EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2));
Yang Gao406b32f2015-02-13 16:25:33 -0800630
631 send_response.set_message(recv_request.message());
632 response_writer.Finish(send_response, Status::OK, tag(3));
Yang Gao3a5e5492015-02-18 14:32:38 -0800633 response_reader->Finish(&recv_response, &recv_status, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700634 Verifier(GetParam().disable_blocking)
635 .Expect(3, true)
636 .Expect(4, true)
637 .Verify(cq_.get());
Yang Gao406b32f2015-02-13 16:25:33 -0800638
639 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700640 EXPECT_TRUE(recv_status.ok());
Yang Gao406b32f2015-02-13 16:25:33 -0800641}
642
Craig Tiller69f90e62015-08-06 08:32:35 -0700643TEST_P(AsyncEnd2endTest, ServerInitialMetadataRpc) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800644 ResetStub();
645
646 EchoRequest send_request;
647 EchoRequest recv_request;
648 EchoResponse send_response;
649 EchoResponse recv_response;
650 Status recv_status;
651
652 ClientContext cli_ctx;
653 ServerContext srv_ctx;
654 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
655
Vijay Paid7b1e702016-05-02 15:10:21 -0700656 send_request.set_message(GetParam().message_content);
Yang Gao2b7f5372015-02-18 00:45:53 -0800657 std::pair<grpc::string, grpc::string> meta1("key1", "val1");
658 std::pair<grpc::string, grpc::string> meta2("key2", "val2");
659
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800660 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700661 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
Yang Gao2b7f5372015-02-18 00:45:53 -0800662
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700663 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
664 cq_.get(), tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700665 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800666 EXPECT_EQ(send_request.message(), recv_request.message());
667 srv_ctx.AddInitialMetadata(meta1.first, meta1.second);
668 srv_ctx.AddInitialMetadata(meta2.first, meta2.second);
669 response_writer.SendInitialMetadata(tag(3));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700670 Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800671
Yang Gao3a5e5492015-02-18 14:32:38 -0800672 response_reader->ReadInitialMetadata(tag(4));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700673 Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800674 auto server_initial_metadata = cli_ctx.GetServerInitialMetadata();
yang-ge21908f2015-08-25 13:47:51 -0700675 EXPECT_EQ(meta1.second,
676 ToString(server_initial_metadata.find(meta1.first)->second));
677 EXPECT_EQ(meta2.second,
678 ToString(server_initial_metadata.find(meta2.first)->second));
vjpaid5577aa2015-02-18 22:26:48 -0800679 EXPECT_EQ(static_cast<size_t>(2), server_initial_metadata.size());
Yang Gao3a5e5492015-02-18 14:32:38 -0800680
681 send_response.set_message(recv_request.message());
682 response_writer.Finish(send_response, Status::OK, tag(5));
Yang Gao3a5e5492015-02-18 14:32:38 -0800683 response_reader->Finish(&recv_response, &recv_status, tag(6));
Craig Tillere4d27482016-05-03 12:19:33 -0700684 Verifier(GetParam().disable_blocking)
685 .Expect(5, true)
686 .Expect(6, true)
687 .Verify(cq_.get());
Yang Gao3a5e5492015-02-18 14:32:38 -0800688
689 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700690 EXPECT_TRUE(recv_status.ok());
Yang Gao2b7f5372015-02-18 00:45:53 -0800691}
692
Craig Tiller69f90e62015-08-06 08:32:35 -0700693TEST_P(AsyncEnd2endTest, ServerTrailingMetadataRpc) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800694 ResetStub();
695
696 EchoRequest send_request;
697 EchoRequest recv_request;
698 EchoResponse send_response;
699 EchoResponse recv_response;
700 Status recv_status;
701
702 ClientContext cli_ctx;
703 ServerContext srv_ctx;
704 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
705
Vijay Paid7b1e702016-05-02 15:10:21 -0700706 send_request.set_message(GetParam().message_content);
Yang Gao2b7f5372015-02-18 00:45:53 -0800707 std::pair<grpc::string, grpc::string> meta1("key1", "val1");
708 std::pair<grpc::string, grpc::string> meta2("key2", "val2");
709
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800710 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700711 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
Yang Gao2b7f5372015-02-18 00:45:53 -0800712
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700713 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
714 cq_.get(), tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700715 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800716 EXPECT_EQ(send_request.message(), recv_request.message());
717 response_writer.SendInitialMetadata(tag(3));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700718 Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800719
720 send_response.set_message(recv_request.message());
721 srv_ctx.AddTrailingMetadata(meta1.first, meta1.second);
722 srv_ctx.AddTrailingMetadata(meta2.first, meta2.second);
723 response_writer.Finish(send_response, Status::OK, tag(4));
Yang Gao3a5e5492015-02-18 14:32:38 -0800724 response_reader->Finish(&recv_response, &recv_status, tag(5));
Craig Tillere4d27482016-05-03 12:19:33 -0700725
726 Verifier(GetParam().disable_blocking)
727 .Expect(4, true)
728 .Expect(5, true)
729 .Verify(cq_.get());
730
Yang Gao2b7f5372015-02-18 00:45:53 -0800731 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700732 EXPECT_TRUE(recv_status.ok());
Yang Gao2b7f5372015-02-18 00:45:53 -0800733 auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata();
yang-ge21908f2015-08-25 13:47:51 -0700734 EXPECT_EQ(meta1.second,
735 ToString(server_trailing_metadata.find(meta1.first)->second));
736 EXPECT_EQ(meta2.second,
737 ToString(server_trailing_metadata.find(meta2.first)->second));
vjpaid5577aa2015-02-18 22:26:48 -0800738 EXPECT_EQ(static_cast<size_t>(2), server_trailing_metadata.size());
Yang Gao2b7f5372015-02-18 00:45:53 -0800739}
740
Craig Tiller69f90e62015-08-06 08:32:35 -0700741TEST_P(AsyncEnd2endTest, MetadataRpc) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800742 ResetStub();
743
744 EchoRequest send_request;
745 EchoRequest recv_request;
746 EchoResponse send_response;
747 EchoResponse recv_response;
748 Status recv_status;
749
750 ClientContext cli_ctx;
751 ServerContext srv_ctx;
752 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
753
Vijay Paid7b1e702016-05-02 15:10:21 -0700754 send_request.set_message(GetParam().message_content);
Yang Gao2b7f5372015-02-18 00:45:53 -0800755 std::pair<grpc::string, grpc::string> meta1("key1", "val1");
Yang Gao3a5e5492015-02-18 14:32:38 -0800756 std::pair<grpc::string, grpc::string> meta2(
Vijay Pai92a928f2015-03-26 16:30:22 -0400757 "key2-bin",
Craig Tillerd6c98df2015-08-18 09:33:44 -0700758 grpc::string("\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc", 13));
Yang Gao2b7f5372015-02-18 00:45:53 -0800759 std::pair<grpc::string, grpc::string> meta3("key3", "val3");
Craig Tiller47c83fd2015-02-21 22:45:35 -0800760 std::pair<grpc::string, grpc::string> meta6(
761 "key4-bin",
Vijay Pai92a928f2015-03-26 16:30:22 -0400762 grpc::string("\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d",
Craig Tillerd6c98df2015-08-18 09:33:44 -0700763 14));
Yang Gao2b7f5372015-02-18 00:45:53 -0800764 std::pair<grpc::string, grpc::string> meta5("key5", "val5");
Craig Tiller47c83fd2015-02-21 22:45:35 -0800765 std::pair<grpc::string, grpc::string> meta4(
766 "key6-bin",
Craig Tillerd6c98df2015-08-18 09:33:44 -0700767 grpc::string(
768 "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee", 15));
Yang Gao2b7f5372015-02-18 00:45:53 -0800769
770 cli_ctx.AddMetadata(meta1.first, meta1.second);
771 cli_ctx.AddMetadata(meta2.first, meta2.second);
772
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800773 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700774 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
Yang Gao2b7f5372015-02-18 00:45:53 -0800775
Craig Tiller06cf3cc2015-05-13 13:11:01 -0700776 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
777 cq_.get(), tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700778 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800779 EXPECT_EQ(send_request.message(), recv_request.message());
780 auto client_initial_metadata = srv_ctx.client_metadata();
yang-ge21908f2015-08-25 13:47:51 -0700781 EXPECT_EQ(meta1.second,
782 ToString(client_initial_metadata.find(meta1.first)->second));
783 EXPECT_EQ(meta2.second,
784 ToString(client_initial_metadata.find(meta2.first)->second));
Craig Tiller8bf2dca2015-07-10 13:08:41 -0700785 EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2));
Yang Gao2b7f5372015-02-18 00:45:53 -0800786
787 srv_ctx.AddInitialMetadata(meta3.first, meta3.second);
788 srv_ctx.AddInitialMetadata(meta4.first, meta4.second);
789 response_writer.SendInitialMetadata(tag(3));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700790 Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get());
Yang Gao3a5e5492015-02-18 14:32:38 -0800791 response_reader->ReadInitialMetadata(tag(4));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700792 Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get());
Yang Gao2b7f5372015-02-18 00:45:53 -0800793 auto server_initial_metadata = cli_ctx.GetServerInitialMetadata();
yang-ge21908f2015-08-25 13:47:51 -0700794 EXPECT_EQ(meta3.second,
795 ToString(server_initial_metadata.find(meta3.first)->second));
796 EXPECT_EQ(meta4.second,
797 ToString(server_initial_metadata.find(meta4.first)->second));
Craig Tiller8bf2dca2015-07-10 13:08:41 -0700798 EXPECT_GE(server_initial_metadata.size(), static_cast<size_t>(2));
Yang Gao3a5e5492015-02-18 14:32:38 -0800799
800 send_response.set_message(recv_request.message());
801 srv_ctx.AddTrailingMetadata(meta5.first, meta5.second);
802 srv_ctx.AddTrailingMetadata(meta6.first, meta6.second);
803 response_writer.Finish(send_response, Status::OK, tag(5));
Yang Gao3a5e5492015-02-18 14:32:38 -0800804 response_reader->Finish(&recv_response, &recv_status, tag(6));
Craig Tillere4d27482016-05-03 12:19:33 -0700805
806 Verifier(GetParam().disable_blocking)
807 .Expect(5, true)
808 .Expect(6, true)
809 .Verify(cq_.get());
810
Yang Gao3a5e5492015-02-18 14:32:38 -0800811 EXPECT_EQ(send_response.message(), recv_response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700812 EXPECT_TRUE(recv_status.ok());
Yang Gao2b7f5372015-02-18 00:45:53 -0800813 auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata();
yang-ge21908f2015-08-25 13:47:51 -0700814 EXPECT_EQ(meta5.second,
815 ToString(server_trailing_metadata.find(meta5.first)->second));
816 EXPECT_EQ(meta6.second,
817 ToString(server_trailing_metadata.find(meta6.first)->second));
Craig Tiller8bf2dca2015-07-10 13:08:41 -0700818 EXPECT_GE(server_trailing_metadata.size(), static_cast<size_t>(2));
Yang Gao2b7f5372015-02-18 00:45:53 -0800819}
yang-gb3352562015-08-04 14:42:06 -0700820
821// Server uses AsyncNotifyWhenDone API to check for cancellation
Craig Tiller69f90e62015-08-06 08:32:35 -0700822TEST_P(AsyncEnd2endTest, ServerCheckCancellation) {
yang-gb3352562015-08-04 14:42:06 -0700823 ResetStub();
824
825 EchoRequest send_request;
826 EchoRequest recv_request;
827 EchoResponse send_response;
828 EchoResponse recv_response;
829 Status recv_status;
830
831 ClientContext cli_ctx;
832 ServerContext srv_ctx;
833 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
834
Vijay Paid7b1e702016-05-02 15:10:21 -0700835 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800836 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
yang-gb3352562015-08-04 14:42:06 -0700837 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
838
839 srv_ctx.AsyncNotifyWhenDone(tag(5));
840 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
841 cq_.get(), tag(2));
842
Vijay Paidf8b62c2016-05-02 14:34:24 -0700843 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
yang-gb3352562015-08-04 14:42:06 -0700844 EXPECT_EQ(send_request.message(), recv_request.message());
845
846 cli_ctx.TryCancel();
Vijay Paidf8b62c2016-05-02 14:34:24 -0700847 Verifier(GetParam().disable_blocking).Expect(5, true).Verify(cq_.get());
yang-gb3352562015-08-04 14:42:06 -0700848 EXPECT_TRUE(srv_ctx.IsCancelled());
849
850 response_reader->Finish(&recv_response, &recv_status, tag(4));
yang-g15759f62016-06-01 11:21:27 -0700851 Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get());
yang-gb3352562015-08-04 14:42:06 -0700852
853 EXPECT_EQ(StatusCode::CANCELLED, recv_status.error_code());
854}
855
856// Server uses AsyncNotifyWhenDone API to check for normal finish
Craig Tiller69f90e62015-08-06 08:32:35 -0700857TEST_P(AsyncEnd2endTest, ServerCheckDone) {
yang-gb3352562015-08-04 14:42:06 -0700858 ResetStub();
859
860 EchoRequest send_request;
861 EchoRequest recv_request;
862 EchoResponse send_response;
863 EchoResponse recv_response;
864 Status recv_status;
865
866 ClientContext cli_ctx;
867 ServerContext srv_ctx;
868 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
869
Vijay Paid7b1e702016-05-02 15:10:21 -0700870 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800871 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
yang-gb3352562015-08-04 14:42:06 -0700872 stub_->AsyncEcho(&cli_ctx, send_request, cq_.get()));
873
874 srv_ctx.AsyncNotifyWhenDone(tag(5));
875 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(),
876 cq_.get(), tag(2));
877
Vijay Paidf8b62c2016-05-02 14:34:24 -0700878 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
yang-gb3352562015-08-04 14:42:06 -0700879 EXPECT_EQ(send_request.message(), recv_request.message());
880
881 send_response.set_message(recv_request.message());
882 response_writer.Finish(send_response, Status::OK, tag(3));
yang-gb3352562015-08-04 14:42:06 -0700883 response_reader->Finish(&recv_response, &recv_status, tag(4));
Craig Tillere4d27482016-05-03 12:19:33 -0700884 Verifier(GetParam().disable_blocking)
885 .Expect(3, true)
886 .Expect(4, true)
887 .Expect(5, true)
888 .Verify(cq_.get());
889 EXPECT_FALSE(srv_ctx.IsCancelled());
yang-gb3352562015-08-04 14:42:06 -0700890
891 EXPECT_EQ(send_response.message(), recv_response.message());
892 EXPECT_TRUE(recv_status.ok());
893}
894
Craig Tiller8f7bff72015-08-17 13:23:14 -0700895TEST_P(AsyncEnd2endTest, UnimplementedRpc) {
Vijay Paidf8b62c2016-05-02 14:34:24 -0700896 ChannelArguments args;
897 auto channel_creds =
898 GetChannelCredentials(GetParam().credentials_type, &args);
yang-g730055d2015-08-27 12:29:45 -0700899 std::shared_ptr<Channel> channel =
Vijay Paidf8b62c2016-05-02 14:34:24 -0700900 CreateCustomChannel(server_address_.str(), channel_creds, args);
Craig Tiller1b4e3302015-12-17 16:35:00 -0800901 std::unique_ptr<grpc::testing::UnimplementedService::Stub> stub;
902 stub = grpc::testing::UnimplementedService::NewStub(channel);
yang-g9b7757d2015-08-13 11:15:53 -0700903 EchoRequest send_request;
904 EchoResponse recv_response;
905 Status recv_status;
906
907 ClientContext cli_ctx;
Vijay Paid7b1e702016-05-02 15:10:21 -0700908 send_request.set_message(GetParam().message_content);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800909 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
yang-g9b7757d2015-08-13 11:15:53 -0700910 stub->AsyncUnimplemented(&cli_ctx, send_request, cq_.get()));
911
912 response_reader->Finish(&recv_response, &recv_status, tag(4));
yang-g15759f62016-06-01 11:21:27 -0700913 Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get());
yang-g9b7757d2015-08-13 11:15:53 -0700914
915 EXPECT_EQ(StatusCode::UNIMPLEMENTED, recv_status.error_code());
916 EXPECT_EQ("", recv_status.error_message());
917}
918
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800919// This class is for testing scenarios where RPCs are cancelled on the server
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800920// by calling ServerContext::TryCancel(). Server uses AsyncNotifyWhenDone
921// API to check for cancellation
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800922class AsyncEnd2endServerTryCancelTest : public AsyncEnd2endTest {
923 protected:
924 typedef enum {
925 DO_NOT_CANCEL = 0,
926 CANCEL_BEFORE_PROCESSING,
927 CANCEL_DURING_PROCESSING,
928 CANCEL_AFTER_PROCESSING
929 } ServerTryCancelRequestPhase;
930
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800931 // Helper for testing client-streaming RPCs which are cancelled on the server.
932 // Depending on the value of server_try_cancel parameter, this will test one
933 // of the following three scenarios:
934 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading
935 // any messages from the client
936 //
937 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading
938 // messages from the client
939 //
940 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all
941 // messages from the client (but before sending any status back to the
942 // client)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800943 void TestClientStreamingServerCancel(
944 ServerTryCancelRequestPhase server_try_cancel) {
945 ResetStub();
946
947 EchoRequest send_request;
948 EchoRequest recv_request;
949 EchoResponse send_response;
950 EchoResponse recv_response;
951 Status recv_status;
952
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800953 ClientContext cli_ctx;
954 ServerContext srv_ctx;
955 ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx);
956
957 // Initiate the 'RequestStream' call on client
958 std::unique_ptr<ClientAsyncWriter<EchoRequest>> cli_stream(
Sree Kuchibhotla4fb59082016-01-29 11:16:24 -0800959 stub_->AsyncRequestStream(&cli_ctx, &recv_response, cq_.get(), tag(1)));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700960 Verifier(GetParam().disable_blocking).Expect(1, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800961
962 // On the server, request to be notified of 'RequestStream' calls
963 // and receive the 'RequestStream' call just made by the client
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800964 srv_ctx.AsyncNotifyWhenDone(tag(11));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800965 service_.RequestRequestStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(),
966 tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700967 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800968
969 // Client sends 3 messages (tags 3, 4 and 5)
970 for (int tag_idx = 3; tag_idx <= 5; tag_idx++) {
Vijay Paia63271c2016-06-15 12:56:38 -0700971 send_request.set_message("Ping " + grpc::to_string(tag_idx));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800972 cli_stream->Write(send_request, tag(tag_idx));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700973 Verifier(GetParam().disable_blocking)
974 .Expect(tag_idx, true)
975 .Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800976 }
977 cli_stream->WritesDone(tag(6));
Vijay Paidf8b62c2016-05-02 14:34:24 -0700978 Verifier(GetParam().disable_blocking).Expect(6, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800979
980 bool expected_server_cq_result = true;
981 bool ignore_cq_result = false;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800982 bool want_done_tag = false;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800983
984 if (server_try_cancel == CANCEL_BEFORE_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800985 srv_ctx.TryCancel();
Vijay Paidf8b62c2016-05-02 14:34:24 -0700986 Verifier(GetParam().disable_blocking).Expect(11, true).Verify(cq_.get());
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800987 EXPECT_TRUE(srv_ctx.IsCancelled());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800988
989 // Since cancellation is done before server reads any results, we know
990 // for sure that all cq results will return false from this point forward
991 expected_server_cq_result = false;
992 }
993
994 std::thread* server_try_cancel_thd = NULL;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800995
Vijay Paidf8b62c2016-05-02 14:34:24 -0700996 auto verif = Verifier(GetParam().disable_blocking);
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800997
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800998 if (server_try_cancel == CANCEL_DURING_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800999 server_try_cancel_thd =
1000 new std::thread(&ServerContext::TryCancel, &srv_ctx);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001001 // Server will cancel the RPC in a parallel thread while reading the
1002 // requests from the client. Since the cancellation can happen at anytime,
1003 // some of the cq results (i.e those until cancellation) might be true but
1004 // its non deterministic. So better to ignore the cq results
1005 ignore_cq_result = true;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001006 // Expect that we might possibly see the done tag that
1007 // indicates cancellation completion in this case
1008 want_done_tag = true;
1009 verif.Expect(11, true);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001010 }
1011
1012 // Server reads 3 messages (tags 6, 7 and 8)
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001013 // But if want_done_tag is true, we might also see tag 11
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001014 for (int tag_idx = 6; tag_idx <= 8; tag_idx++) {
1015 srv_stream.Read(&recv_request, tag(tag_idx));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001016 // Note that we'll add something to the verifier and verify that
1017 // something was seen, but it might be tag 11 and not what we
1018 // just added
1019 int got_tag = verif.Expect(tag_idx, expected_server_cq_result)
1020 .Next(cq_.get(), ignore_cq_result);
1021 GPR_ASSERT((got_tag == tag_idx) || (got_tag == 11 && want_done_tag));
1022 if (got_tag == 11) {
1023 EXPECT_TRUE(srv_ctx.IsCancelled());
1024 want_done_tag = false;
1025 // Now get the other entry that we were waiting on
1026 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), tag_idx);
1027 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001028 }
1029
1030 if (server_try_cancel_thd != NULL) {
1031 server_try_cancel_thd->join();
1032 delete server_try_cancel_thd;
1033 }
1034
1035 if (server_try_cancel == CANCEL_AFTER_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001036 srv_ctx.TryCancel();
1037 want_done_tag = true;
1038 verif.Expect(11, true);
1039 }
1040
1041 if (want_done_tag) {
1042 verif.Verify(cq_.get());
1043 EXPECT_TRUE(srv_ctx.IsCancelled());
1044 want_done_tag = false;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001045 }
1046
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001047 // The RPC has been cancelled at this point for sure (i.e irrespective of
1048 // the value of `server_try_cancel` is). So, from this point forward, we
1049 // know that cq results are supposed to return false on server.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001050
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001051 // Server sends the final message and cancelled status (but the RPC is
1052 // already cancelled at this point. So we expect the operation to fail)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001053 srv_stream.Finish(send_response, Status::CANCELLED, tag(9));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001054 Verifier(GetParam().disable_blocking).Expect(9, false).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001055
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001056 // Client will see the cancellation
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001057 cli_stream->Finish(&recv_status, tag(10));
yang-g15759f62016-06-01 11:21:27 -07001058 Verifier(GetParam().disable_blocking).Expect(10, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001059 EXPECT_FALSE(recv_status.ok());
1060 EXPECT_EQ(::grpc::StatusCode::CANCELLED, recv_status.error_code());
1061 }
1062
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001063 // Helper for testing server-streaming RPCs which are cancelled on the server.
1064 // Depending on the value of server_try_cancel parameter, this will test one
1065 // of the following three scenarios:
1066 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before sending
1067 // any messages to the client
1068 //
1069 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while sending
1070 // messages to the client
1071 //
1072 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after sending all
1073 // messages to the client (but before sending any status back to the
1074 // client)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001075 void TestServerStreamingServerCancel(
1076 ServerTryCancelRequestPhase server_try_cancel) {
1077 ResetStub();
1078
1079 EchoRequest send_request;
1080 EchoRequest recv_request;
1081 EchoResponse send_response;
1082 EchoResponse recv_response;
1083 Status recv_status;
1084 ClientContext cli_ctx;
1085 ServerContext srv_ctx;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001086 ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx);
1087
1088 send_request.set_message("Ping");
1089 // Initiate the 'ResponseStream' call on the client
1090 std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream(
Sree Kuchibhotla4fb59082016-01-29 11:16:24 -08001091 stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1)));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001092 Verifier(GetParam().disable_blocking).Expect(1, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001093 // On the server, request to be notified of 'ResponseStream' calls and
1094 // receive the call just made by the client
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001095 srv_ctx.AsyncNotifyWhenDone(tag(11));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001096 service_.RequestResponseStream(&srv_ctx, &recv_request, &srv_stream,
1097 cq_.get(), cq_.get(), tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001098 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001099 EXPECT_EQ(send_request.message(), recv_request.message());
1100
1101 bool expected_cq_result = true;
1102 bool ignore_cq_result = false;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001103 bool want_done_tag = false;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001104
1105 if (server_try_cancel == CANCEL_BEFORE_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001106 srv_ctx.TryCancel();
Vijay Paidf8b62c2016-05-02 14:34:24 -07001107 Verifier(GetParam().disable_blocking).Expect(11, true).Verify(cq_.get());
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001108 EXPECT_TRUE(srv_ctx.IsCancelled());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001109
1110 // We know for sure that all cq results will be false from this point
1111 // since the server cancelled the RPC
1112 expected_cq_result = false;
1113 }
1114
1115 std::thread* server_try_cancel_thd = NULL;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001116
Vijay Paidf8b62c2016-05-02 14:34:24 -07001117 auto verif = Verifier(GetParam().disable_blocking);
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001118
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001119 if (server_try_cancel == CANCEL_DURING_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001120 server_try_cancel_thd =
1121 new std::thread(&ServerContext::TryCancel, &srv_ctx);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001122
1123 // Server will cancel the RPC in a parallel thread while writing responses
1124 // to the client. Since the cancellation can happen at anytime, some of
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001125 // the cq results (i.e those until cancellation) might be true but it is
1126 // non deterministic. So better to ignore the cq results
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001127 ignore_cq_result = true;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001128 // Expect that we might possibly see the done tag that
1129 // indicates cancellation completion in this case
1130 want_done_tag = true;
1131 verif.Expect(11, true);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001132 }
1133
1134 // Server sends three messages (tags 3, 4 and 5)
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001135 // But if want_done tag is true, we might also see tag 11
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001136 for (int tag_idx = 3; tag_idx <= 5; tag_idx++) {
Vijay Paia63271c2016-06-15 12:56:38 -07001137 send_response.set_message("Pong " + grpc::to_string(tag_idx));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001138 srv_stream.Write(send_response, tag(tag_idx));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001139 // Note that we'll add something to the verifier and verify that
1140 // something was seen, but it might be tag 11 and not what we
1141 // just added
1142 int got_tag = verif.Expect(tag_idx, expected_cq_result)
1143 .Next(cq_.get(), ignore_cq_result);
1144 GPR_ASSERT((got_tag == tag_idx) || (got_tag == 11 && want_done_tag));
1145 if (got_tag == 11) {
1146 EXPECT_TRUE(srv_ctx.IsCancelled());
1147 want_done_tag = false;
1148 // Now get the other entry that we were waiting on
1149 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), tag_idx);
1150 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001151 }
1152
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001153 if (server_try_cancel_thd != NULL) {
1154 server_try_cancel_thd->join();
1155 delete server_try_cancel_thd;
1156 }
1157
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001158 if (server_try_cancel == CANCEL_AFTER_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001159 srv_ctx.TryCancel();
1160 want_done_tag = true;
1161 verif.Expect(11, true);
yang-gad0df7b2016-02-22 10:00:20 -08001162
1163 // Client reads may fail bacause it is notified that the stream is
1164 // cancelled.
1165 ignore_cq_result = true;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001166 }
1167
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001168 if (want_done_tag) {
1169 verif.Verify(cq_.get());
1170 EXPECT_TRUE(srv_ctx.IsCancelled());
1171 want_done_tag = false;
1172 }
1173
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001174 // Client attemts to read the three messages from the server
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001175 for (int tag_idx = 6; tag_idx <= 8; tag_idx++) {
1176 cli_stream->Read(&recv_response, tag(tag_idx));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001177 Verifier(GetParam().disable_blocking)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001178 .Expect(tag_idx, expected_cq_result)
Sree Kuchibhotla4fb59082016-01-29 11:16:24 -08001179 .Verify(cq_.get(), ignore_cq_result);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001180 }
1181
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001182 // The RPC has been cancelled at this point for sure (i.e irrespective of
1183 // the value of `server_try_cancel` is). So, from this point forward, we
1184 // know that cq results are supposed to return false on server.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001185
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001186 // Server finishes the stream (but the RPC is already cancelled)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001187 srv_stream.Finish(Status::CANCELLED, tag(9));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001188 Verifier(GetParam().disable_blocking).Expect(9, false).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001189
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001190 // Client will see the cancellation
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001191 cli_stream->Finish(&recv_status, tag(10));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001192 Verifier(GetParam().disable_blocking).Expect(10, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001193 EXPECT_FALSE(recv_status.ok());
1194 EXPECT_EQ(::grpc::StatusCode::CANCELLED, recv_status.error_code());
1195 }
1196
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001197 // Helper for testing bidirectinal-streaming RPCs which are cancelled on the
1198 // server.
1199 //
1200 // Depending on the value of server_try_cancel parameter, this will
1201 // test one of the following three scenarios:
1202 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/
1203 // writing any messages from/to the client
1204 //
1205 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading
1206 // messages from the client
1207 //
1208 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all
1209 // messages from the client (but before sending any status back to the
1210 // client)
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001211 void TestBidiStreamingServerCancel(
1212 ServerTryCancelRequestPhase server_try_cancel) {
1213 ResetStub();
1214
1215 EchoRequest send_request;
1216 EchoRequest recv_request;
1217 EchoResponse send_response;
1218 EchoResponse recv_response;
1219 Status recv_status;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001220 ClientContext cli_ctx;
1221 ServerContext srv_ctx;
1222 ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx);
1223
1224 // Initiate the call from the client side
1225 std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>>
Sree Kuchibhotla4fb59082016-01-29 11:16:24 -08001226 cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1)));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001227 Verifier(GetParam().disable_blocking).Expect(1, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001228
1229 // On the server, request to be notified of the 'BidiStream' call and
1230 // receive the call just made by the client
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001231 srv_ctx.AsyncNotifyWhenDone(tag(11));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001232 service_.RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(),
1233 tag(2));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001234 Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001235
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001236 // Client sends the first and the only message
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001237 send_request.set_message("Ping");
1238 cli_stream->Write(send_request, tag(3));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001239 Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001240
1241 bool expected_cq_result = true;
1242 bool ignore_cq_result = false;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001243 bool want_done_tag = false;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001244
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001245 if (server_try_cancel == CANCEL_BEFORE_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001246 srv_ctx.TryCancel();
Vijay Paidf8b62c2016-05-02 14:34:24 -07001247 Verifier(GetParam().disable_blocking).Expect(11, true).Verify(cq_.get());
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001248 EXPECT_TRUE(srv_ctx.IsCancelled());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001249
1250 // We know for sure that all cq results will be false from this point
1251 // since the server cancelled the RPC
1252 expected_cq_result = false;
1253 }
1254
1255 std::thread* server_try_cancel_thd = NULL;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001256
Vijay Paidf8b62c2016-05-02 14:34:24 -07001257 auto verif = Verifier(GetParam().disable_blocking);
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001258
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001259 if (server_try_cancel == CANCEL_DURING_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001260 server_try_cancel_thd =
1261 new std::thread(&ServerContext::TryCancel, &srv_ctx);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001262
1263 // Since server is going to cancel the RPC in a parallel thread, some of
1264 // the cq results (i.e those until the cancellation) might be true. Since
1265 // that number is non-deterministic, it is better to ignore the cq results
1266 ignore_cq_result = true;
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001267 // Expect that we might possibly see the done tag that
1268 // indicates cancellation completion in this case
1269 want_done_tag = true;
1270 verif.Expect(11, true);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001271 }
1272
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001273 int got_tag;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001274 srv_stream.Read(&recv_request, tag(4));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001275 verif.Expect(4, expected_cq_result);
1276 got_tag = verif.Next(cq_.get(), ignore_cq_result);
1277 GPR_ASSERT((got_tag == 4) || (got_tag == 11 && want_done_tag));
1278 if (got_tag == 11) {
1279 EXPECT_TRUE(srv_ctx.IsCancelled());
1280 want_done_tag = false;
1281 // Now get the other entry that we were waiting on
1282 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 4);
1283 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001284
1285 send_response.set_message("Pong");
1286 srv_stream.Write(send_response, tag(5));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001287 verif.Expect(5, expected_cq_result);
1288 got_tag = verif.Next(cq_.get(), ignore_cq_result);
1289 GPR_ASSERT((got_tag == 5) || (got_tag == 11 && want_done_tag));
1290 if (got_tag == 11) {
1291 EXPECT_TRUE(srv_ctx.IsCancelled());
1292 want_done_tag = false;
1293 // Now get the other entry that we were waiting on
1294 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 5);
1295 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001296
1297 cli_stream->Read(&recv_response, tag(6));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001298 verif.Expect(6, expected_cq_result);
1299 got_tag = verif.Next(cq_.get(), ignore_cq_result);
1300 GPR_ASSERT((got_tag == 6) || (got_tag == 11 && want_done_tag));
1301 if (got_tag == 11) {
1302 EXPECT_TRUE(srv_ctx.IsCancelled());
1303 want_done_tag = false;
1304 // Now get the other entry that we were waiting on
1305 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 6);
1306 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001307
1308 // This is expected to succeed in all cases
1309 cli_stream->WritesDone(tag(7));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001310 verif.Expect(7, true);
1311 got_tag = verif.Next(cq_.get(), ignore_cq_result);
1312 GPR_ASSERT((got_tag == 7) || (got_tag == 11 && want_done_tag));
1313 if (got_tag == 11) {
1314 EXPECT_TRUE(srv_ctx.IsCancelled());
1315 want_done_tag = false;
1316 // Now get the other entry that we were waiting on
1317 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 7);
1318 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001319
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001320 // This is expected to fail in all cases i.e for all values of
Vijay Pai018879a2016-02-16 09:20:50 -08001321 // server_try_cancel. This is because at this point, either there are no
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001322 // more msgs from the client (because client called WritesDone) or the RPC
1323 // is cancelled on the server
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001324 srv_stream.Read(&recv_request, tag(8));
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001325 verif.Expect(8, false);
1326 got_tag = verif.Next(cq_.get(), ignore_cq_result);
1327 GPR_ASSERT((got_tag == 8) || (got_tag == 11 && want_done_tag));
1328 if (got_tag == 11) {
1329 EXPECT_TRUE(srv_ctx.IsCancelled());
1330 want_done_tag = false;
1331 // Now get the other entry that we were waiting on
1332 EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 8);
1333 }
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001334
1335 if (server_try_cancel_thd != NULL) {
1336 server_try_cancel_thd->join();
1337 delete server_try_cancel_thd;
1338 }
1339
1340 if (server_try_cancel == CANCEL_AFTER_PROCESSING) {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -08001341 srv_ctx.TryCancel();
1342 want_done_tag = true;
1343 verif.Expect(11, true);
1344 }
1345
1346 if (want_done_tag) {
1347 verif.Verify(cq_.get());
1348 EXPECT_TRUE(srv_ctx.IsCancelled());
1349 want_done_tag = false;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001350 }
1351
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -08001352 // The RPC has been cancelled at this point for sure (i.e irrespective of
1353 // the value of `server_try_cancel` is). So, from this point forward, we
1354 // know that cq results are supposed to return false on server.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001355
1356 srv_stream.Finish(Status::CANCELLED, tag(9));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001357 Verifier(GetParam().disable_blocking).Expect(9, false).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001358
1359 cli_stream->Finish(&recv_status, tag(10));
Vijay Paidf8b62c2016-05-02 14:34:24 -07001360 Verifier(GetParam().disable_blocking).Expect(10, true).Verify(cq_.get());
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001361 EXPECT_FALSE(recv_status.ok());
1362 EXPECT_EQ(grpc::StatusCode::CANCELLED, recv_status.error_code());
1363 }
1364};
1365
1366TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelBefore) {
1367 TestClientStreamingServerCancel(CANCEL_BEFORE_PROCESSING);
1368}
1369
1370TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelDuring) {
1371 TestClientStreamingServerCancel(CANCEL_DURING_PROCESSING);
1372}
1373
1374TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelAfter) {
1375 TestClientStreamingServerCancel(CANCEL_AFTER_PROCESSING);
1376}
1377
1378TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelBefore) {
1379 TestServerStreamingServerCancel(CANCEL_BEFORE_PROCESSING);
1380}
1381
1382TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelDuring) {
1383 TestServerStreamingServerCancel(CANCEL_DURING_PROCESSING);
1384}
1385
1386TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelAfter) {
1387 TestServerStreamingServerCancel(CANCEL_AFTER_PROCESSING);
1388}
1389
1390TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelBefore) {
1391 TestBidiStreamingServerCancel(CANCEL_BEFORE_PROCESSING);
1392}
1393
1394TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelDuring) {
1395 TestBidiStreamingServerCancel(CANCEL_DURING_PROCESSING);
1396}
1397
1398TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelAfter) {
1399 TestBidiStreamingServerCancel(CANCEL_AFTER_PROCESSING);
1400}
1401
Vijay Paidf8b62c2016-05-02 14:34:24 -07001402std::vector<TestScenario> CreateTestScenarios(bool test_disable_blocking,
Vijay Paid7b1e702016-05-02 15:10:21 -07001403 bool test_secure,
1404 int test_big_limit) {
Vijay Paidf8b62c2016-05-02 14:34:24 -07001405 std::vector<TestScenario> scenarios;
1406 std::vector<grpc::string> credentials_types;
Vijay Paid7b1e702016-05-02 15:10:21 -07001407 std::vector<grpc::string> messages;
1408
Vijay Paidf8b62c2016-05-02 14:34:24 -07001409 credentials_types.push_back(kInsecureCredentialsType);
Vijay Paid7b1e702016-05-02 15:10:21 -07001410 auto sec_list = GetSecureCredentialsTypeList();
1411 for (auto sec = sec_list.begin(); sec != sec_list.end(); sec++) {
1412 credentials_types.push_back(*sec);
1413 }
1414
1415 messages.push_back("Hello");
1416 for (int sz = 1; sz < test_big_limit; sz *= 2) {
1417 grpc::string big_msg;
1418 for (int i = 0; i < sz * 1024; i++) {
1419 char c = 'a' + (i % 26);
1420 big_msg += c;
1421 }
1422 messages.push_back(big_msg);
1423 }
1424
1425 for (auto cred = credentials_types.begin(); cred != credentials_types.end();
1426 ++cred) {
1427 for (auto msg = messages.begin(); msg != messages.end(); msg++) {
Vijay Pai679c75f2016-06-15 13:08:00 -07001428 scenarios.emplace_back(false, *cred, *msg);
Vijay Paid7b1e702016-05-02 15:10:21 -07001429 if (test_disable_blocking) {
Vijay Pai679c75f2016-06-15 13:08:00 -07001430 scenarios.emplace_back(true, *cred, *msg);
Vijay Paid7b1e702016-05-02 15:10:21 -07001431 }
Vijay Paidf8b62c2016-05-02 14:34:24 -07001432 }
1433 }
1434 return scenarios;
1435}
1436
Craig Tiller4c06b822015-08-06 08:41:31 -07001437INSTANTIATE_TEST_CASE_P(AsyncEnd2end, AsyncEnd2endTest,
Vijay Paid7b1e702016-05-02 15:10:21 -07001438 ::testing::ValuesIn(CreateTestScenarios(true, true,
1439 1024)));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001440INSTANTIATE_TEST_CASE_P(AsyncEnd2endServerTryCancel,
1441 AsyncEnd2endServerTryCancelTest,
Vijay Paid7b1e702016-05-02 15:10:21 -07001442 ::testing::ValuesIn(CreateTestScenarios(false, false,
1443 0)));
Craig Tiller69f90e62015-08-06 08:32:35 -07001444
Craig Tiller0220cf12015-02-12 17:39:26 -08001445} // namespace
1446} // namespace testing
1447} // namespace grpc
1448
1449int main(int argc, char** argv) {
1450 grpc_test_init(argc, argv);
Vijay Paib65eda42016-02-16 13:48:05 -08001451 gpr_tls_init(&g_is_async_end2end_test);
Craig Tiller0220cf12015-02-12 17:39:26 -08001452 ::testing::InitGoogleTest(&argc, argv);
Vijay Paib65eda42016-02-16 13:48:05 -08001453 int ret = RUN_ALL_TESTS();
1454 gpr_tls_destroy(&g_is_async_end2end_test);
1455 return ret;
Craig Tiller0220cf12015-02-12 17:39:26 -08001456}