Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 4 | * 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 | |
Yang Gao | da699b8 | 2015-02-18 01:10:22 -0800 | [diff] [blame] | 34 | #include <memory> |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 35 | #include <thread> |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 36 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 37 | #include <grpc++/channel.h> |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 38 | #include <grpc++/client_context.h> |
| 39 | #include <grpc++/create_channel.h> |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 40 | #include <grpc++/server.h> |
| 41 | #include <grpc++/server_builder.h> |
| 42 | #include <grpc++/server_context.h> |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 43 | #include <grpc/grpc.h> |
| 44 | #include <grpc/support/thd.h> |
| 45 | #include <grpc/support/time.h> |
Vijay Pai | b65eda4 | 2016-02-16 13:48:05 -0800 | [diff] [blame] | 46 | #include <grpc/support/tls.h> |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 47 | #include <gtest/gtest.h> |
| 48 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 49 | #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 50 | #include "src/proto/grpc/testing/echo.grpc.pb.h" |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 51 | #include "test/core/util/port.h" |
| 52 | #include "test/core/util/test_config.h" |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 53 | #include "test/cpp/util/string_ref_helper.h" |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 54 | #include "test/cpp/util/test_credentials_provider.h" |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 55 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 56 | #ifdef GPR_POSIX_SOCKET |
Craig Tiller | f45496f | 2016-03-30 07:41:19 -0700 | [diff] [blame] | 57 | #include "src/core/lib/iomgr/ev_posix.h" |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 58 | #endif |
| 59 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 60 | using grpc::testing::EchoRequest; |
| 61 | using grpc::testing::EchoResponse; |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 62 | using grpc::testing::kTlsCredentialsType; |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 63 | using std::chrono::system_clock; |
| 64 | |
Vijay Pai | b65eda4 | 2016-02-16 13:48:05 -0800 | [diff] [blame] | 65 | GPR_TLS_DECL(g_is_async_end2end_test); |
| 66 | |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 67 | namespace grpc { |
| 68 | namespace testing { |
| 69 | |
| 70 | namespace { |
| 71 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 72 | void* tag(int i) { return (void*)(intptr_t)i; } |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 73 | int detag(void* p) { return static_cast<int>(reinterpret_cast<intptr_t>(p)); } |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 74 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 75 | #ifdef GPR_POSIX_SOCKET |
Vijay Pai | b65eda4 | 2016-02-16 13:48:05 -0800 | [diff] [blame] | 76 | static int maybe_assert_non_blocking_poll(struct pollfd* pfds, nfds_t nfds, |
| 77 | int timeout) { |
| 78 | if (gpr_tls_get(&g_is_async_end2end_test)) { |
| 79 | GPR_ASSERT(timeout == 0); |
| 80 | } |
| 81 | return poll(pfds, nfds, timeout); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | class PollOverride { |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 85 | public: |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 86 | PollOverride(grpc_poll_function_type f) { |
| 87 | prev_ = grpc_poll_function; |
| 88 | grpc_poll_function = f; |
| 89 | } |
| 90 | |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 91 | ~PollOverride() { grpc_poll_function = prev_; } |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 92 | |
| 93 | private: |
| 94 | grpc_poll_function_type prev_; |
| 95 | }; |
| 96 | |
vjpai | cf4daeb | 2016-02-15 02:33:54 -0800 | [diff] [blame] | 97 | class PollingOverrider : public PollOverride { |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 98 | public: |
vjpai | cf4daeb | 2016-02-15 02:33:54 -0800 | [diff] [blame] | 99 | explicit PollingOverrider(bool allow_blocking) |
Vijay Pai | b65eda4 | 2016-02-16 13:48:05 -0800 | [diff] [blame] | 100 | : PollOverride(allow_blocking ? poll : maybe_assert_non_blocking_poll) {} |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 101 | }; |
| 102 | #else |
vjpai | cf4daeb | 2016-02-15 02:33:54 -0800 | [diff] [blame] | 103 | class PollingOverrider { |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 104 | public: |
vjpai | cf4daeb | 2016-02-15 02:33:54 -0800 | [diff] [blame] | 105 | explicit PollingOverrider(bool allow_blocking) {} |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 106 | }; |
| 107 | #endif |
| 108 | |
vjpai | cf4daeb | 2016-02-15 02:33:54 -0800 | [diff] [blame] | 109 | class Verifier { |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 110 | public: |
vjpai | cf4daeb | 2016-02-15 02:33:54 -0800 | [diff] [blame] | 111 | explicit Verifier(bool spin) : spin_(spin) {} |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 112 | // Expect sets the expected ok value for a specific tag |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 113 | Verifier& Expect(int i, bool expect_ok) { |
| 114 | expectations_[tag(i)] = expect_ok; |
| 115 | return *this; |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 116 | } |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 117 | |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 118 | // Next waits for 1 async tag to complete, checks its |
| 119 | // expectations, and returns the tag |
| 120 | int Next(CompletionQueue* cq, bool ignore_ok) { |
| 121 | bool ok; |
| 122 | void* got_tag; |
| 123 | if (spin_) { |
| 124 | for (;;) { |
| 125 | auto r = cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME)); |
| 126 | if (r == CompletionQueue::TIMEOUT) continue; |
| 127 | if (r == CompletionQueue::GOT_EVENT) break; |
| 128 | gpr_log(GPR_ERROR, "unexpected result from AsyncNext"); |
| 129 | abort(); |
| 130 | } |
| 131 | } else { |
| 132 | EXPECT_TRUE(cq->Next(&got_tag, &ok)); |
| 133 | } |
| 134 | auto it = expectations_.find(got_tag); |
| 135 | EXPECT_TRUE(it != expectations_.end()); |
| 136 | if (!ignore_ok) { |
| 137 | EXPECT_EQ(it->second, ok); |
| 138 | } |
| 139 | expectations_.erase(it); |
| 140 | return detag(got_tag); |
| 141 | } |
| 142 | |
| 143 | // Verify keeps calling Next until all currently set |
| 144 | // expected tags are complete |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 145 | void Verify(CompletionQueue* cq) { Verify(cq, false); } |
| 146 | |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 147 | // This version of Verify allows optionally ignoring the |
| 148 | // outcome of the expectation |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 149 | void Verify(CompletionQueue* cq, bool ignore_ok) { |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 150 | GPR_ASSERT(!expectations_.empty()); |
| 151 | while (!expectations_.empty()) { |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 152 | Next(cq, ignore_ok); |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 155 | // This version of Verify stops after a certain deadline |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 156 | void Verify(CompletionQueue* cq, |
| 157 | std::chrono::system_clock::time_point deadline) { |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 158 | if (expectations_.empty()) { |
| 159 | bool ok; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 160 | void* got_tag; |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 161 | if (spin_) { |
| 162 | while (std::chrono::system_clock::now() < deadline) { |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 163 | EXPECT_EQ( |
| 164 | cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME)), |
| 165 | CompletionQueue::TIMEOUT); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 166 | } |
| 167 | } else { |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 168 | EXPECT_EQ(cq->AsyncNext(&got_tag, &ok, deadline), |
| 169 | CompletionQueue::TIMEOUT); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 170 | } |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 171 | } else { |
| 172 | while (!expectations_.empty()) { |
| 173 | bool ok; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 174 | void* got_tag; |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 175 | if (spin_) { |
| 176 | for (;;) { |
| 177 | GPR_ASSERT(std::chrono::system_clock::now() < deadline); |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 178 | auto r = |
| 179 | cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 180 | if (r == CompletionQueue::TIMEOUT) continue; |
| 181 | if (r == CompletionQueue::GOT_EVENT) break; |
| 182 | gpr_log(GPR_ERROR, "unexpected result from AsyncNext"); |
| 183 | abort(); |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 184 | } |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 185 | } else { |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 186 | EXPECT_EQ(cq->AsyncNext(&got_tag, &ok, deadline), |
| 187 | CompletionQueue::GOT_EVENT); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 188 | } |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 189 | auto it = expectations_.find(got_tag); |
| 190 | EXPECT_TRUE(it != expectations_.end()); |
| 191 | EXPECT_EQ(it->second, ok); |
| 192 | expectations_.erase(it); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | std::map<void*, bool> expectations_; |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 199 | bool spin_; |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 200 | }; |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 201 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 202 | class TestScenario { |
| 203 | public: |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 204 | TestScenario(bool non_block, const grpc::string& creds_type, |
| 205 | const grpc::string& content) |
| 206 | : disable_blocking(non_block), |
| 207 | credentials_type(creds_type), |
| 208 | message_content(content) {} |
| 209 | void Log() const { |
| 210 | gpr_log(GPR_INFO, |
| 211 | "Scenario: disable_blocking %d, credentials %s, message size %d", |
| 212 | disable_blocking, credentials_type.c_str(), message_content.size()); |
| 213 | } |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 214 | bool disable_blocking; |
| 215 | const grpc::string credentials_type; |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 216 | const grpc::string message_content; |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | class AsyncEnd2endTest : public ::testing::TestWithParam<TestScenario> { |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 220 | protected: |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 221 | AsyncEnd2endTest() { GetParam().Log(); } |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 222 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 223 | void SetUp() GRPC_OVERRIDE { |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 224 | poll_overrider_.reset(new PollingOverrider(!GetParam().disable_blocking)); |
Vijay Pai | 018879a | 2016-02-16 09:20:50 -0800 | [diff] [blame] | 225 | |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 226 | int port = grpc_pick_unused_port_or_die(); |
| 227 | server_address_ << "localhost:" << port; |
vjpai | 017ed62 | 2015-12-09 10:42:54 -0800 | [diff] [blame] | 228 | |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 229 | // Setup server |
| 230 | ServerBuilder builder; |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 231 | auto server_creds = GetServerCredentials(GetParam().credentials_type); |
| 232 | builder.AddListeningPort(server_address_.str(), server_creds); |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 233 | builder.RegisterService(&service_); |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 234 | cq_ = builder.AddCompletionQueue(); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 235 | server_ = builder.BuildAndStart(); |
Vijay Pai | b65eda4 | 2016-02-16 13:48:05 -0800 | [diff] [blame] | 236 | |
| 237 | gpr_tls_set(&g_is_async_end2end_test, 1); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 240 | void TearDown() GRPC_OVERRIDE { |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 241 | server_->Shutdown(); |
| 242 | void* ignored_tag; |
| 243 | bool ignored_ok; |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 244 | cq_->Shutdown(); |
| 245 | while (cq_->Next(&ignored_tag, &ignored_ok)) |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 246 | ; |
Vijay Pai | 018879a | 2016-02-16 09:20:50 -0800 | [diff] [blame] | 247 | poll_overrider_.reset(); |
Vijay Pai | b65eda4 | 2016-02-16 13:48:05 -0800 | [diff] [blame] | 248 | gpr_tls_set(&g_is_async_end2end_test, 0); |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 249 | } |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 250 | |
| 251 | void ResetStub() { |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 252 | ChannelArguments args; |
| 253 | auto channel_creds = |
| 254 | GetChannelCredentials(GetParam().credentials_type, &args); |
yang-g | 730055d | 2015-08-27 12:29:45 -0700 | [diff] [blame] | 255 | std::shared_ptr<Channel> channel = |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 256 | CreateCustomChannel(server_address_.str(), channel_creds, args); |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 257 | stub_ = grpc::testing::EchoTestService::NewStub(channel); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 260 | void SendRpc(int num_rpcs) { |
| 261 | for (int i = 0; i < num_rpcs; i++) { |
| 262 | EchoRequest send_request; |
| 263 | EchoRequest recv_request; |
| 264 | EchoResponse send_response; |
| 265 | EchoResponse recv_response; |
| 266 | Status recv_status; |
| 267 | |
| 268 | ClientContext cli_ctx; |
| 269 | ServerContext srv_ctx; |
| 270 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 271 | |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 272 | send_request.set_message(GetParam().message_content); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 273 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 274 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 275 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 276 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 277 | cq_.get(), tag(2)); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 278 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 279 | Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 280 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 281 | |
| 282 | send_response.set_message(recv_request.message()); |
| 283 | response_writer.Finish(send_response, Status::OK, tag(3)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 284 | Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 285 | |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 286 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 287 | Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 288 | |
| 289 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 290 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 294 | std::unique_ptr<ServerCompletionQueue> cq_; |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 295 | std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 296 | std::unique_ptr<Server> server_; |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 297 | grpc::testing::EchoTestService::AsyncService service_; |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 298 | std::ostringstream server_address_; |
vjpai | cf4daeb | 2016-02-15 02:33:54 -0800 | [diff] [blame] | 299 | |
Vijay Pai | 018879a | 2016-02-16 09:20:50 -0800 | [diff] [blame] | 300 | std::unique_ptr<PollingOverrider> poll_overrider_; |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 301 | }; |
| 302 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 303 | TEST_P(AsyncEnd2endTest, SimpleRpc) { |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 304 | ResetStub(); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 305 | SendRpc(1); |
| 306 | } |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame] | 307 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 308 | TEST_P(AsyncEnd2endTest, SequentialRpcs) { |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 309 | ResetStub(); |
| 310 | SendRpc(10); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 311 | } |
| 312 | |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 313 | // Test a simple RPC using the async version of Next |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 314 | TEST_P(AsyncEnd2endTest, AsyncNextRpc) { |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 315 | ResetStub(); |
| 316 | |
| 317 | EchoRequest send_request; |
| 318 | EchoRequest recv_request; |
| 319 | EchoResponse send_response; |
| 320 | EchoResponse recv_response; |
| 321 | Status recv_status; |
| 322 | |
| 323 | ClientContext cli_ctx; |
| 324 | ServerContext srv_ctx; |
| 325 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 326 | |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 327 | send_request.set_message(GetParam().message_content); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 328 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 329 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 330 | |
Yang Gao | 757afae | 2015-03-17 15:49:26 -0700 | [diff] [blame] | 331 | std::chrono::system_clock::time_point time_now( |
Craig Tiller | f51199f | 2015-05-08 09:32:53 -0700 | [diff] [blame] | 332 | std::chrono::system_clock::now()); |
| 333 | std::chrono::system_clock::time_point time_limit( |
| 334 | std::chrono::system_clock::now() + std::chrono::seconds(10)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 335 | Verifier(GetParam().disable_blocking).Verify(cq_.get(), time_now); |
| 336 | Verifier(GetParam().disable_blocking).Verify(cq_.get(), time_now); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 337 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 338 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 339 | cq_.get(), tag(2)); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 340 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 341 | Verifier(GetParam().disable_blocking) |
| 342 | .Expect(2, true) |
| 343 | .Verify(cq_.get(), time_limit); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 344 | EXPECT_EQ(send_request.message(), recv_request.message()); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 345 | |
| 346 | send_response.set_message(recv_request.message()); |
| 347 | response_writer.Finish(send_response, Status::OK, tag(3)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 348 | Verifier(GetParam().disable_blocking) |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 349 | .Expect(3, true) |
| 350 | .Verify(cq_.get(), std::chrono::system_clock::time_point::max()); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 351 | |
| 352 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 353 | Verifier(GetParam().disable_blocking) |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 354 | .Expect(4, true) |
| 355 | .Verify(cq_.get(), std::chrono::system_clock::time_point::max()); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 356 | |
| 357 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 358 | EXPECT_TRUE(recv_status.ok()); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 359 | } |
Yang Gao | 757afae | 2015-03-17 15:49:26 -0700 | [diff] [blame] | 360 | |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 361 | // Two pings and a final pong. |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 362 | TEST_P(AsyncEnd2endTest, SimpleClientStreaming) { |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 363 | ResetStub(); |
| 364 | |
| 365 | EchoRequest send_request; |
| 366 | EchoRequest recv_request; |
| 367 | EchoResponse send_response; |
| 368 | EchoResponse recv_response; |
| 369 | Status recv_status; |
| 370 | ClientContext cli_ctx; |
| 371 | ServerContext srv_ctx; |
| 372 | ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 373 | |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 374 | send_request.set_message(GetParam().message_content); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 375 | std::unique_ptr<ClientAsyncWriter<EchoRequest>> cli_stream( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 376 | stub_->AsyncRequestStream(&cli_ctx, &recv_response, cq_.get(), tag(1))); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 377 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 378 | service_.RequestRequestStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 379 | tag(2)); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 380 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 381 | Verifier(GetParam().disable_blocking) |
| 382 | .Expect(2, true) |
| 383 | .Expect(1, true) |
| 384 | .Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 385 | |
| 386 | cli_stream->Write(send_request, tag(3)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 387 | Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 388 | |
| 389 | srv_stream.Read(&recv_request, tag(4)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 390 | Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 391 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 392 | |
| 393 | cli_stream->Write(send_request, tag(5)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 394 | Verifier(GetParam().disable_blocking).Expect(5, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 395 | |
| 396 | srv_stream.Read(&recv_request, tag(6)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 397 | Verifier(GetParam().disable_blocking).Expect(6, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 398 | |
| 399 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 400 | cli_stream->WritesDone(tag(7)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 401 | Verifier(GetParam().disable_blocking).Expect(7, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 402 | |
| 403 | srv_stream.Read(&recv_request, tag(8)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 404 | Verifier(GetParam().disable_blocking).Expect(8, false).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 405 | |
| 406 | send_response.set_message(recv_request.message()); |
| 407 | srv_stream.Finish(send_response, Status::OK, tag(9)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 408 | Verifier(GetParam().disable_blocking).Expect(9, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 409 | |
| 410 | cli_stream->Finish(&recv_status, tag(10)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 411 | Verifier(GetParam().disable_blocking).Expect(10, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 412 | |
| 413 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 414 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 417 | // One ping, two pongs. |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 418 | TEST_P(AsyncEnd2endTest, SimpleServerStreaming) { |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 419 | 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 | ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx); |
| 429 | |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 430 | send_request.set_message(GetParam().message_content); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 431 | std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 432 | stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1))); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 433 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 434 | service_.RequestResponseStream(&srv_ctx, &recv_request, &srv_stream, |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 435 | cq_.get(), cq_.get(), tag(2)); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 436 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 437 | Verifier(GetParam().disable_blocking) |
| 438 | .Expect(1, true) |
| 439 | .Expect(2, true) |
| 440 | .Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 441 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 442 | |
| 443 | send_response.set_message(recv_request.message()); |
| 444 | srv_stream.Write(send_response, tag(3)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 445 | Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 446 | |
| 447 | cli_stream->Read(&recv_response, tag(4)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 448 | Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 449 | EXPECT_EQ(send_response.message(), recv_response.message()); |
| 450 | |
| 451 | srv_stream.Write(send_response, tag(5)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 452 | Verifier(GetParam().disable_blocking).Expect(5, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 453 | |
| 454 | cli_stream->Read(&recv_response, tag(6)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 455 | Verifier(GetParam().disable_blocking).Expect(6, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 456 | EXPECT_EQ(send_response.message(), recv_response.message()); |
| 457 | |
| 458 | srv_stream.Finish(Status::OK, tag(7)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 459 | Verifier(GetParam().disable_blocking).Expect(7, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 460 | |
| 461 | cli_stream->Read(&recv_response, tag(8)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 462 | Verifier(GetParam().disable_blocking).Expect(8, false).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 463 | |
| 464 | cli_stream->Finish(&recv_status, tag(9)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 465 | Verifier(GetParam().disable_blocking).Expect(9, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 466 | |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 467 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | // One ping, one pong. |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 471 | TEST_P(AsyncEnd2endTest, SimpleBidiStreaming) { |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 472 | ResetStub(); |
| 473 | |
| 474 | EchoRequest send_request; |
| 475 | EchoRequest recv_request; |
| 476 | EchoResponse send_response; |
| 477 | EchoResponse recv_response; |
| 478 | Status recv_status; |
| 479 | ClientContext cli_ctx; |
| 480 | ServerContext srv_ctx; |
| 481 | ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 482 | |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 483 | send_request.set_message(GetParam().message_content); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 484 | std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>> |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 485 | cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1))); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 486 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 487 | service_.RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 488 | tag(2)); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 489 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 490 | Verifier(GetParam().disable_blocking) |
| 491 | .Expect(1, true) |
| 492 | .Expect(2, true) |
| 493 | .Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 494 | |
| 495 | cli_stream->Write(send_request, tag(3)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 496 | Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 497 | |
| 498 | srv_stream.Read(&recv_request, tag(4)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 499 | Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 500 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 501 | |
| 502 | send_response.set_message(recv_request.message()); |
| 503 | srv_stream.Write(send_response, tag(5)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 504 | Verifier(GetParam().disable_blocking).Expect(5, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 505 | |
| 506 | cli_stream->Read(&recv_response, tag(6)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 507 | Verifier(GetParam().disable_blocking).Expect(6, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 508 | EXPECT_EQ(send_response.message(), recv_response.message()); |
| 509 | |
| 510 | cli_stream->WritesDone(tag(7)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 511 | Verifier(GetParam().disable_blocking).Expect(7, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 512 | |
| 513 | srv_stream.Read(&recv_request, tag(8)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 514 | Verifier(GetParam().disable_blocking).Expect(8, false).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 515 | |
| 516 | srv_stream.Finish(Status::OK, tag(9)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 517 | Verifier(GetParam().disable_blocking).Expect(9, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 518 | |
| 519 | cli_stream->Finish(&recv_status, tag(10)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 520 | Verifier(GetParam().disable_blocking).Expect(10, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 521 | |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 522 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 525 | // Metadata tests |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 526 | TEST_P(AsyncEnd2endTest, ClientInitialMetadataRpc) { |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 527 | ResetStub(); |
| 528 | |
| 529 | EchoRequest send_request; |
| 530 | EchoRequest recv_request; |
| 531 | EchoResponse send_response; |
| 532 | EchoResponse recv_response; |
| 533 | Status recv_status; |
| 534 | |
| 535 | ClientContext cli_ctx; |
| 536 | ServerContext srv_ctx; |
| 537 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 538 | |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 539 | send_request.set_message(GetParam().message_content); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 540 | std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
| 541 | std::pair<grpc::string, grpc::string> meta2("key2", "val2"); |
Craig Tiller | 6f87164 | 2016-02-03 16:15:31 -0800 | [diff] [blame] | 542 | std::pair<grpc::string, grpc::string> meta3("g.r.d-bin", "xyz"); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 543 | cli_ctx.AddMetadata(meta1.first, meta1.second); |
| 544 | cli_ctx.AddMetadata(meta2.first, meta2.second); |
Craig Tiller | 6f87164 | 2016-02-03 16:15:31 -0800 | [diff] [blame] | 545 | cli_ctx.AddMetadata(meta3.first, meta3.second); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 546 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 547 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 548 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 549 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 550 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 551 | cq_.get(), tag(2)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 552 | Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 553 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 554 | auto client_initial_metadata = srv_ctx.client_metadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 555 | EXPECT_EQ(meta1.second, |
| 556 | ToString(client_initial_metadata.find(meta1.first)->second)); |
| 557 | EXPECT_EQ(meta2.second, |
| 558 | ToString(client_initial_metadata.find(meta2.first)->second)); |
Craig Tiller | 6f87164 | 2016-02-03 16:15:31 -0800 | [diff] [blame] | 559 | EXPECT_EQ(meta3.second, |
| 560 | ToString(client_initial_metadata.find(meta3.first)->second)); |
Craig Tiller | 8bf2dca | 2015-07-10 13:08:41 -0700 | [diff] [blame] | 561 | EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2)); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 562 | |
| 563 | send_response.set_message(recv_request.message()); |
| 564 | response_writer.Finish(send_response, Status::OK, tag(3)); |
| 565 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 566 | Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 567 | |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 568 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 569 | Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 570 | |
| 571 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 572 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 575 | TEST_P(AsyncEnd2endTest, ServerInitialMetadataRpc) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 576 | ResetStub(); |
| 577 | |
| 578 | EchoRequest send_request; |
| 579 | EchoRequest recv_request; |
| 580 | EchoResponse send_response; |
| 581 | EchoResponse recv_response; |
| 582 | Status recv_status; |
| 583 | |
| 584 | ClientContext cli_ctx; |
| 585 | ServerContext srv_ctx; |
| 586 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 587 | |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 588 | send_request.set_message(GetParam().message_content); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 589 | std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
| 590 | std::pair<grpc::string, grpc::string> meta2("key2", "val2"); |
| 591 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 592 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 593 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 594 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 595 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 596 | cq_.get(), tag(2)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 597 | Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 598 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 599 | srv_ctx.AddInitialMetadata(meta1.first, meta1.second); |
| 600 | srv_ctx.AddInitialMetadata(meta2.first, meta2.second); |
| 601 | response_writer.SendInitialMetadata(tag(3)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 602 | Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 603 | |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 604 | response_reader->ReadInitialMetadata(tag(4)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 605 | Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 606 | auto server_initial_metadata = cli_ctx.GetServerInitialMetadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 607 | EXPECT_EQ(meta1.second, |
| 608 | ToString(server_initial_metadata.find(meta1.first)->second)); |
| 609 | EXPECT_EQ(meta2.second, |
| 610 | ToString(server_initial_metadata.find(meta2.first)->second)); |
vjpai | d5577aa | 2015-02-18 22:26:48 -0800 | [diff] [blame] | 611 | EXPECT_EQ(static_cast<size_t>(2), server_initial_metadata.size()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 612 | |
| 613 | send_response.set_message(recv_request.message()); |
| 614 | response_writer.Finish(send_response, Status::OK, tag(5)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 615 | Verifier(GetParam().disable_blocking).Expect(5, true).Verify(cq_.get()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 616 | |
| 617 | response_reader->Finish(&recv_response, &recv_status, tag(6)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 618 | Verifier(GetParam().disable_blocking).Expect(6, true).Verify(cq_.get()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 619 | |
| 620 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 621 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 624 | TEST_P(AsyncEnd2endTest, ServerTrailingMetadataRpc) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 625 | ResetStub(); |
| 626 | |
| 627 | EchoRequest send_request; |
| 628 | EchoRequest recv_request; |
| 629 | EchoResponse send_response; |
| 630 | EchoResponse recv_response; |
| 631 | Status recv_status; |
| 632 | |
| 633 | ClientContext cli_ctx; |
| 634 | ServerContext srv_ctx; |
| 635 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 636 | |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 637 | send_request.set_message(GetParam().message_content); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 638 | std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
| 639 | std::pair<grpc::string, grpc::string> meta2("key2", "val2"); |
| 640 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 641 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 642 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 643 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 644 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 645 | cq_.get(), tag(2)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 646 | Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 647 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 648 | response_writer.SendInitialMetadata(tag(3)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 649 | Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 650 | |
| 651 | send_response.set_message(recv_request.message()); |
| 652 | srv_ctx.AddTrailingMetadata(meta1.first, meta1.second); |
| 653 | srv_ctx.AddTrailingMetadata(meta2.first, meta2.second); |
| 654 | response_writer.Finish(send_response, Status::OK, tag(4)); |
| 655 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 656 | Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 657 | |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 658 | response_reader->Finish(&recv_response, &recv_status, tag(5)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 659 | Verifier(GetParam().disable_blocking).Expect(5, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 660 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 661 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 662 | auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 663 | EXPECT_EQ(meta1.second, |
| 664 | ToString(server_trailing_metadata.find(meta1.first)->second)); |
| 665 | EXPECT_EQ(meta2.second, |
| 666 | ToString(server_trailing_metadata.find(meta2.first)->second)); |
vjpai | d5577aa | 2015-02-18 22:26:48 -0800 | [diff] [blame] | 667 | EXPECT_EQ(static_cast<size_t>(2), server_trailing_metadata.size()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 670 | TEST_P(AsyncEnd2endTest, MetadataRpc) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 671 | ResetStub(); |
| 672 | |
| 673 | EchoRequest send_request; |
| 674 | EchoRequest recv_request; |
| 675 | EchoResponse send_response; |
| 676 | EchoResponse recv_response; |
| 677 | Status recv_status; |
| 678 | |
| 679 | ClientContext cli_ctx; |
| 680 | ServerContext srv_ctx; |
| 681 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 682 | |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 683 | send_request.set_message(GetParam().message_content); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 684 | std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 685 | std::pair<grpc::string, grpc::string> meta2( |
Vijay Pai | 92a928f | 2015-03-26 16:30:22 -0400 | [diff] [blame] | 686 | "key2-bin", |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 687 | grpc::string("\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc", 13)); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 688 | std::pair<grpc::string, grpc::string> meta3("key3", "val3"); |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 689 | std::pair<grpc::string, grpc::string> meta6( |
| 690 | "key4-bin", |
Vijay Pai | 92a928f | 2015-03-26 16:30:22 -0400 | [diff] [blame] | 691 | grpc::string("\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d", |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 692 | 14)); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 693 | std::pair<grpc::string, grpc::string> meta5("key5", "val5"); |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 694 | std::pair<grpc::string, grpc::string> meta4( |
| 695 | "key6-bin", |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 696 | grpc::string( |
| 697 | "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee", 15)); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 698 | |
| 699 | cli_ctx.AddMetadata(meta1.first, meta1.second); |
| 700 | cli_ctx.AddMetadata(meta2.first, meta2.second); |
| 701 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 702 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 703 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 704 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 705 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 706 | cq_.get(), tag(2)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 707 | Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 708 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 709 | auto client_initial_metadata = srv_ctx.client_metadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 710 | EXPECT_EQ(meta1.second, |
| 711 | ToString(client_initial_metadata.find(meta1.first)->second)); |
| 712 | EXPECT_EQ(meta2.second, |
| 713 | ToString(client_initial_metadata.find(meta2.first)->second)); |
Craig Tiller | 8bf2dca | 2015-07-10 13:08:41 -0700 | [diff] [blame] | 714 | EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2)); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 715 | |
| 716 | srv_ctx.AddInitialMetadata(meta3.first, meta3.second); |
| 717 | srv_ctx.AddInitialMetadata(meta4.first, meta4.second); |
| 718 | response_writer.SendInitialMetadata(tag(3)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 719 | Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 720 | response_reader->ReadInitialMetadata(tag(4)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 721 | Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 722 | auto server_initial_metadata = cli_ctx.GetServerInitialMetadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 723 | EXPECT_EQ(meta3.second, |
| 724 | ToString(server_initial_metadata.find(meta3.first)->second)); |
| 725 | EXPECT_EQ(meta4.second, |
| 726 | ToString(server_initial_metadata.find(meta4.first)->second)); |
Craig Tiller | 8bf2dca | 2015-07-10 13:08:41 -0700 | [diff] [blame] | 727 | EXPECT_GE(server_initial_metadata.size(), static_cast<size_t>(2)); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 728 | |
| 729 | send_response.set_message(recv_request.message()); |
| 730 | srv_ctx.AddTrailingMetadata(meta5.first, meta5.second); |
| 731 | srv_ctx.AddTrailingMetadata(meta6.first, meta6.second); |
| 732 | response_writer.Finish(send_response, Status::OK, tag(5)); |
| 733 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 734 | Verifier(GetParam().disable_blocking).Expect(5, true).Verify(cq_.get()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 735 | |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 736 | response_reader->Finish(&recv_response, &recv_status, tag(6)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 737 | Verifier(GetParam().disable_blocking).Expect(6, true).Verify(cq_.get()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 738 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 739 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 740 | auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 741 | EXPECT_EQ(meta5.second, |
| 742 | ToString(server_trailing_metadata.find(meta5.first)->second)); |
| 743 | EXPECT_EQ(meta6.second, |
| 744 | ToString(server_trailing_metadata.find(meta6.first)->second)); |
Craig Tiller | 8bf2dca | 2015-07-10 13:08:41 -0700 | [diff] [blame] | 745 | EXPECT_GE(server_trailing_metadata.size(), static_cast<size_t>(2)); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 746 | } |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 747 | |
| 748 | // Server uses AsyncNotifyWhenDone API to check for cancellation |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 749 | TEST_P(AsyncEnd2endTest, ServerCheckCancellation) { |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 750 | ResetStub(); |
| 751 | |
| 752 | EchoRequest send_request; |
| 753 | EchoRequest recv_request; |
| 754 | EchoResponse send_response; |
| 755 | EchoResponse recv_response; |
| 756 | Status recv_status; |
| 757 | |
| 758 | ClientContext cli_ctx; |
| 759 | ServerContext srv_ctx; |
| 760 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 761 | |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 762 | send_request.set_message(GetParam().message_content); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 763 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 764 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 765 | |
| 766 | srv_ctx.AsyncNotifyWhenDone(tag(5)); |
| 767 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 768 | cq_.get(), tag(2)); |
| 769 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 770 | Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 771 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 772 | |
| 773 | cli_ctx.TryCancel(); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 774 | Verifier(GetParam().disable_blocking).Expect(5, true).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 775 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 776 | |
| 777 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 778 | Verifier(GetParam().disable_blocking).Expect(4, false).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 779 | |
| 780 | EXPECT_EQ(StatusCode::CANCELLED, recv_status.error_code()); |
| 781 | } |
| 782 | |
| 783 | // Server uses AsyncNotifyWhenDone API to check for normal finish |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 784 | TEST_P(AsyncEnd2endTest, ServerCheckDone) { |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 785 | ResetStub(); |
| 786 | |
| 787 | EchoRequest send_request; |
| 788 | EchoRequest recv_request; |
| 789 | EchoResponse send_response; |
| 790 | EchoResponse recv_response; |
| 791 | Status recv_status; |
| 792 | |
| 793 | ClientContext cli_ctx; |
| 794 | ServerContext srv_ctx; |
| 795 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 796 | |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 797 | send_request.set_message(GetParam().message_content); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 798 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 799 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 800 | |
| 801 | srv_ctx.AsyncNotifyWhenDone(tag(5)); |
| 802 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 803 | cq_.get(), tag(2)); |
| 804 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 805 | Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 806 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 807 | |
| 808 | send_response.set_message(recv_request.message()); |
| 809 | response_writer.Finish(send_response, Status::OK, tag(3)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 810 | Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get()); |
| 811 | Verifier(GetParam().disable_blocking).Expect(5, true).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 812 | EXPECT_FALSE(srv_ctx.IsCancelled()); |
| 813 | |
| 814 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 815 | Verifier(GetParam().disable_blocking).Expect(4, true).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 816 | |
| 817 | EXPECT_EQ(send_response.message(), recv_response.message()); |
| 818 | EXPECT_TRUE(recv_status.ok()); |
| 819 | } |
| 820 | |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 821 | TEST_P(AsyncEnd2endTest, UnimplementedRpc) { |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 822 | ChannelArguments args; |
| 823 | auto channel_creds = |
| 824 | GetChannelCredentials(GetParam().credentials_type, &args); |
yang-g | 730055d | 2015-08-27 12:29:45 -0700 | [diff] [blame] | 825 | std::shared_ptr<Channel> channel = |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 826 | CreateCustomChannel(server_address_.str(), channel_creds, args); |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 827 | std::unique_ptr<grpc::testing::UnimplementedService::Stub> stub; |
| 828 | stub = grpc::testing::UnimplementedService::NewStub(channel); |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 829 | EchoRequest send_request; |
| 830 | EchoResponse recv_response; |
| 831 | Status recv_status; |
| 832 | |
| 833 | ClientContext cli_ctx; |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 834 | send_request.set_message(GetParam().message_content); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 835 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 836 | stub->AsyncUnimplemented(&cli_ctx, send_request, cq_.get())); |
| 837 | |
| 838 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 839 | Verifier(GetParam().disable_blocking).Expect(4, false).Verify(cq_.get()); |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 840 | |
| 841 | EXPECT_EQ(StatusCode::UNIMPLEMENTED, recv_status.error_code()); |
| 842 | EXPECT_EQ("", recv_status.error_message()); |
| 843 | } |
| 844 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 845 | // This class is for testing scenarios where RPCs are cancelled on the server |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 846 | // by calling ServerContext::TryCancel(). Server uses AsyncNotifyWhenDone |
| 847 | // API to check for cancellation |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 848 | class AsyncEnd2endServerTryCancelTest : public AsyncEnd2endTest { |
| 849 | protected: |
| 850 | typedef enum { |
| 851 | DO_NOT_CANCEL = 0, |
| 852 | CANCEL_BEFORE_PROCESSING, |
| 853 | CANCEL_DURING_PROCESSING, |
| 854 | CANCEL_AFTER_PROCESSING |
| 855 | } ServerTryCancelRequestPhase; |
| 856 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 857 | // Helper for testing client-streaming RPCs which are cancelled on the server. |
| 858 | // Depending on the value of server_try_cancel parameter, this will test one |
| 859 | // of the following three scenarios: |
| 860 | // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading |
| 861 | // any messages from the client |
| 862 | // |
| 863 | // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading |
| 864 | // messages from the client |
| 865 | // |
| 866 | // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all |
| 867 | // messages from the client (but before sending any status back to the |
| 868 | // client) |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 869 | void TestClientStreamingServerCancel( |
| 870 | ServerTryCancelRequestPhase server_try_cancel) { |
| 871 | ResetStub(); |
| 872 | |
| 873 | EchoRequest send_request; |
| 874 | EchoRequest recv_request; |
| 875 | EchoResponse send_response; |
| 876 | EchoResponse recv_response; |
| 877 | Status recv_status; |
| 878 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 879 | ClientContext cli_ctx; |
| 880 | ServerContext srv_ctx; |
| 881 | ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 882 | |
| 883 | // Initiate the 'RequestStream' call on client |
| 884 | std::unique_ptr<ClientAsyncWriter<EchoRequest>> cli_stream( |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 885 | stub_->AsyncRequestStream(&cli_ctx, &recv_response, cq_.get(), tag(1))); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 886 | Verifier(GetParam().disable_blocking).Expect(1, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 887 | |
| 888 | // On the server, request to be notified of 'RequestStream' calls |
| 889 | // and receive the 'RequestStream' call just made by the client |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 890 | srv_ctx.AsyncNotifyWhenDone(tag(11)); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 891 | service_.RequestRequestStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 892 | tag(2)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 893 | Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 894 | |
| 895 | // Client sends 3 messages (tags 3, 4 and 5) |
| 896 | for (int tag_idx = 3; tag_idx <= 5; tag_idx++) { |
| 897 | send_request.set_message("Ping " + std::to_string(tag_idx)); |
| 898 | cli_stream->Write(send_request, tag(tag_idx)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 899 | Verifier(GetParam().disable_blocking) |
| 900 | .Expect(tag_idx, true) |
| 901 | .Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 902 | } |
| 903 | cli_stream->WritesDone(tag(6)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 904 | Verifier(GetParam().disable_blocking).Expect(6, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 905 | |
| 906 | bool expected_server_cq_result = true; |
| 907 | bool ignore_cq_result = false; |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 908 | bool want_done_tag = false; |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 909 | |
| 910 | if (server_try_cancel == CANCEL_BEFORE_PROCESSING) { |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 911 | srv_ctx.TryCancel(); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 912 | Verifier(GetParam().disable_blocking).Expect(11, true).Verify(cq_.get()); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 913 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 914 | |
| 915 | // Since cancellation is done before server reads any results, we know |
| 916 | // for sure that all cq results will return false from this point forward |
| 917 | expected_server_cq_result = false; |
| 918 | } |
| 919 | |
| 920 | std::thread* server_try_cancel_thd = NULL; |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 921 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 922 | auto verif = Verifier(GetParam().disable_blocking); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 923 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 924 | if (server_try_cancel == CANCEL_DURING_PROCESSING) { |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 925 | server_try_cancel_thd = |
| 926 | new std::thread(&ServerContext::TryCancel, &srv_ctx); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 927 | // Server will cancel the RPC in a parallel thread while reading the |
| 928 | // requests from the client. Since the cancellation can happen at anytime, |
| 929 | // some of the cq results (i.e those until cancellation) might be true but |
| 930 | // its non deterministic. So better to ignore the cq results |
| 931 | ignore_cq_result = true; |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 932 | // Expect that we might possibly see the done tag that |
| 933 | // indicates cancellation completion in this case |
| 934 | want_done_tag = true; |
| 935 | verif.Expect(11, true); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | // Server reads 3 messages (tags 6, 7 and 8) |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 939 | // But if want_done_tag is true, we might also see tag 11 |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 940 | for (int tag_idx = 6; tag_idx <= 8; tag_idx++) { |
| 941 | srv_stream.Read(&recv_request, tag(tag_idx)); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 942 | // Note that we'll add something to the verifier and verify that |
| 943 | // something was seen, but it might be tag 11 and not what we |
| 944 | // just added |
| 945 | int got_tag = verif.Expect(tag_idx, expected_server_cq_result) |
| 946 | .Next(cq_.get(), ignore_cq_result); |
| 947 | GPR_ASSERT((got_tag == tag_idx) || (got_tag == 11 && want_done_tag)); |
| 948 | if (got_tag == 11) { |
| 949 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 950 | want_done_tag = false; |
| 951 | // Now get the other entry that we were waiting on |
| 952 | EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), tag_idx); |
| 953 | } |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | if (server_try_cancel_thd != NULL) { |
| 957 | server_try_cancel_thd->join(); |
| 958 | delete server_try_cancel_thd; |
| 959 | } |
| 960 | |
| 961 | if (server_try_cancel == CANCEL_AFTER_PROCESSING) { |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 962 | srv_ctx.TryCancel(); |
| 963 | want_done_tag = true; |
| 964 | verif.Expect(11, true); |
| 965 | } |
| 966 | |
| 967 | if (want_done_tag) { |
| 968 | verif.Verify(cq_.get()); |
| 969 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 970 | want_done_tag = false; |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 971 | } |
| 972 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 973 | // The RPC has been cancelled at this point for sure (i.e irrespective of |
| 974 | // the value of `server_try_cancel` is). So, from this point forward, we |
| 975 | // know that cq results are supposed to return false on server. |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 976 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 977 | // Server sends the final message and cancelled status (but the RPC is |
| 978 | // already cancelled at this point. So we expect the operation to fail) |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 979 | srv_stream.Finish(send_response, Status::CANCELLED, tag(9)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 980 | Verifier(GetParam().disable_blocking).Expect(9, false).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 981 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 982 | // Client will see the cancellation |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 983 | cli_stream->Finish(&recv_status, tag(10)); |
Sree Kuchibhotla | 369a04a | 2016-02-01 10:53:13 -0800 | [diff] [blame] | 984 | // TODO(sreek): The expectation here should be true. This is a bug (github |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 985 | // issue #4972) |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 986 | Verifier(GetParam().disable_blocking).Expect(10, false).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 987 | EXPECT_FALSE(recv_status.ok()); |
| 988 | EXPECT_EQ(::grpc::StatusCode::CANCELLED, recv_status.error_code()); |
| 989 | } |
| 990 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 991 | // Helper for testing server-streaming RPCs which are cancelled on the server. |
| 992 | // Depending on the value of server_try_cancel parameter, this will test one |
| 993 | // of the following three scenarios: |
| 994 | // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before sending |
| 995 | // any messages to the client |
| 996 | // |
| 997 | // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while sending |
| 998 | // messages to the client |
| 999 | // |
| 1000 | // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after sending all |
| 1001 | // messages to the client (but before sending any status back to the |
| 1002 | // client) |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1003 | void TestServerStreamingServerCancel( |
| 1004 | ServerTryCancelRequestPhase server_try_cancel) { |
| 1005 | ResetStub(); |
| 1006 | |
| 1007 | EchoRequest send_request; |
| 1008 | EchoRequest recv_request; |
| 1009 | EchoResponse send_response; |
| 1010 | EchoResponse recv_response; |
| 1011 | Status recv_status; |
| 1012 | ClientContext cli_ctx; |
| 1013 | ServerContext srv_ctx; |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1014 | ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx); |
| 1015 | |
| 1016 | send_request.set_message("Ping"); |
| 1017 | // Initiate the 'ResponseStream' call on the client |
| 1018 | std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream( |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 1019 | stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1))); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1020 | Verifier(GetParam().disable_blocking).Expect(1, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1021 | // On the server, request to be notified of 'ResponseStream' calls and |
| 1022 | // receive the call just made by the client |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1023 | srv_ctx.AsyncNotifyWhenDone(tag(11)); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1024 | service_.RequestResponseStream(&srv_ctx, &recv_request, &srv_stream, |
| 1025 | cq_.get(), cq_.get(), tag(2)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1026 | Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1027 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 1028 | |
| 1029 | bool expected_cq_result = true; |
| 1030 | bool ignore_cq_result = false; |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1031 | bool want_done_tag = false; |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1032 | |
| 1033 | if (server_try_cancel == CANCEL_BEFORE_PROCESSING) { |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1034 | srv_ctx.TryCancel(); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1035 | Verifier(GetParam().disable_blocking).Expect(11, true).Verify(cq_.get()); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1036 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1037 | |
| 1038 | // We know for sure that all cq results will be false from this point |
| 1039 | // since the server cancelled the RPC |
| 1040 | expected_cq_result = false; |
| 1041 | } |
| 1042 | |
| 1043 | std::thread* server_try_cancel_thd = NULL; |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1044 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1045 | auto verif = Verifier(GetParam().disable_blocking); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1046 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1047 | if (server_try_cancel == CANCEL_DURING_PROCESSING) { |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1048 | server_try_cancel_thd = |
| 1049 | new std::thread(&ServerContext::TryCancel, &srv_ctx); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1050 | |
| 1051 | // Server will cancel the RPC in a parallel thread while writing responses |
| 1052 | // to the client. Since the cancellation can happen at anytime, some of |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1053 | // the cq results (i.e those until cancellation) might be true but it is |
| 1054 | // non deterministic. So better to ignore the cq results |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1055 | ignore_cq_result = true; |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1056 | // Expect that we might possibly see the done tag that |
| 1057 | // indicates cancellation completion in this case |
| 1058 | want_done_tag = true; |
| 1059 | verif.Expect(11, true); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | // Server sends three messages (tags 3, 4 and 5) |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1063 | // But if want_done tag is true, we might also see tag 11 |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1064 | for (int tag_idx = 3; tag_idx <= 5; tag_idx++) { |
| 1065 | send_response.set_message("Pong " + std::to_string(tag_idx)); |
| 1066 | srv_stream.Write(send_response, tag(tag_idx)); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1067 | // Note that we'll add something to the verifier and verify that |
| 1068 | // something was seen, but it might be tag 11 and not what we |
| 1069 | // just added |
| 1070 | int got_tag = verif.Expect(tag_idx, expected_cq_result) |
| 1071 | .Next(cq_.get(), ignore_cq_result); |
| 1072 | GPR_ASSERT((got_tag == tag_idx) || (got_tag == 11 && want_done_tag)); |
| 1073 | if (got_tag == 11) { |
| 1074 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1075 | want_done_tag = false; |
| 1076 | // Now get the other entry that we were waiting on |
| 1077 | EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), tag_idx); |
| 1078 | } |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1081 | if (server_try_cancel_thd != NULL) { |
| 1082 | server_try_cancel_thd->join(); |
| 1083 | delete server_try_cancel_thd; |
| 1084 | } |
| 1085 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1086 | if (server_try_cancel == CANCEL_AFTER_PROCESSING) { |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1087 | srv_ctx.TryCancel(); |
| 1088 | want_done_tag = true; |
| 1089 | verif.Expect(11, true); |
yang-g | ad0df7b | 2016-02-22 10:00:20 -0800 | [diff] [blame] | 1090 | |
| 1091 | // Client reads may fail bacause it is notified that the stream is |
| 1092 | // cancelled. |
| 1093 | ignore_cq_result = true; |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1096 | if (want_done_tag) { |
| 1097 | verif.Verify(cq_.get()); |
| 1098 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1099 | want_done_tag = false; |
| 1100 | } |
| 1101 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1102 | // Client attemts to read the three messages from the server |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1103 | for (int tag_idx = 6; tag_idx <= 8; tag_idx++) { |
| 1104 | cli_stream->Read(&recv_response, tag(tag_idx)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1105 | Verifier(GetParam().disable_blocking) |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1106 | .Expect(tag_idx, expected_cq_result) |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 1107 | .Verify(cq_.get(), ignore_cq_result); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1110 | // The RPC has been cancelled at this point for sure (i.e irrespective of |
| 1111 | // the value of `server_try_cancel` is). So, from this point forward, we |
| 1112 | // know that cq results are supposed to return false on server. |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1113 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1114 | // Server finishes the stream (but the RPC is already cancelled) |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1115 | srv_stream.Finish(Status::CANCELLED, tag(9)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1116 | Verifier(GetParam().disable_blocking).Expect(9, false).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1117 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1118 | // Client will see the cancellation |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1119 | cli_stream->Finish(&recv_status, tag(10)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1120 | Verifier(GetParam().disable_blocking).Expect(10, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1121 | EXPECT_FALSE(recv_status.ok()); |
| 1122 | EXPECT_EQ(::grpc::StatusCode::CANCELLED, recv_status.error_code()); |
| 1123 | } |
| 1124 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1125 | // Helper for testing bidirectinal-streaming RPCs which are cancelled on the |
| 1126 | // server. |
| 1127 | // |
| 1128 | // Depending on the value of server_try_cancel parameter, this will |
| 1129 | // test one of the following three scenarios: |
| 1130 | // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/ |
| 1131 | // writing any messages from/to the client |
| 1132 | // |
| 1133 | // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading |
| 1134 | // messages from the client |
| 1135 | // |
| 1136 | // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all |
| 1137 | // messages from the client (but before sending any status back to the |
| 1138 | // client) |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1139 | void TestBidiStreamingServerCancel( |
| 1140 | ServerTryCancelRequestPhase server_try_cancel) { |
| 1141 | ResetStub(); |
| 1142 | |
| 1143 | EchoRequest send_request; |
| 1144 | EchoRequest recv_request; |
| 1145 | EchoResponse send_response; |
| 1146 | EchoResponse recv_response; |
| 1147 | Status recv_status; |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1148 | ClientContext cli_ctx; |
| 1149 | ServerContext srv_ctx; |
| 1150 | ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 1151 | |
| 1152 | // Initiate the call from the client side |
| 1153 | std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>> |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 1154 | cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1))); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1155 | Verifier(GetParam().disable_blocking).Expect(1, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1156 | |
| 1157 | // On the server, request to be notified of the 'BidiStream' call and |
| 1158 | // receive the call just made by the client |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1159 | srv_ctx.AsyncNotifyWhenDone(tag(11)); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1160 | service_.RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 1161 | tag(2)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1162 | Verifier(GetParam().disable_blocking).Expect(2, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1163 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1164 | // Client sends the first and the only message |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1165 | send_request.set_message("Ping"); |
| 1166 | cli_stream->Write(send_request, tag(3)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1167 | Verifier(GetParam().disable_blocking).Expect(3, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1168 | |
| 1169 | bool expected_cq_result = true; |
| 1170 | bool ignore_cq_result = false; |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1171 | bool want_done_tag = false; |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1172 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1173 | if (server_try_cancel == CANCEL_BEFORE_PROCESSING) { |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1174 | srv_ctx.TryCancel(); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1175 | Verifier(GetParam().disable_blocking).Expect(11, true).Verify(cq_.get()); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1176 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1177 | |
| 1178 | // We know for sure that all cq results will be false from this point |
| 1179 | // since the server cancelled the RPC |
| 1180 | expected_cq_result = false; |
| 1181 | } |
| 1182 | |
| 1183 | std::thread* server_try_cancel_thd = NULL; |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1184 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1185 | auto verif = Verifier(GetParam().disable_blocking); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1186 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1187 | if (server_try_cancel == CANCEL_DURING_PROCESSING) { |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1188 | server_try_cancel_thd = |
| 1189 | new std::thread(&ServerContext::TryCancel, &srv_ctx); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1190 | |
| 1191 | // Since server is going to cancel the RPC in a parallel thread, some of |
| 1192 | // the cq results (i.e those until the cancellation) might be true. Since |
| 1193 | // that number is non-deterministic, it is better to ignore the cq results |
| 1194 | ignore_cq_result = true; |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1195 | // Expect that we might possibly see the done tag that |
| 1196 | // indicates cancellation completion in this case |
| 1197 | want_done_tag = true; |
| 1198 | verif.Expect(11, true); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1199 | } |
| 1200 | |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1201 | int got_tag; |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1202 | srv_stream.Read(&recv_request, tag(4)); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1203 | verif.Expect(4, expected_cq_result); |
| 1204 | got_tag = verif.Next(cq_.get(), ignore_cq_result); |
| 1205 | GPR_ASSERT((got_tag == 4) || (got_tag == 11 && want_done_tag)); |
| 1206 | if (got_tag == 11) { |
| 1207 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1208 | want_done_tag = false; |
| 1209 | // Now get the other entry that we were waiting on |
| 1210 | EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 4); |
| 1211 | } |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1212 | |
| 1213 | send_response.set_message("Pong"); |
| 1214 | srv_stream.Write(send_response, tag(5)); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1215 | verif.Expect(5, expected_cq_result); |
| 1216 | got_tag = verif.Next(cq_.get(), ignore_cq_result); |
| 1217 | GPR_ASSERT((got_tag == 5) || (got_tag == 11 && want_done_tag)); |
| 1218 | if (got_tag == 11) { |
| 1219 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1220 | want_done_tag = false; |
| 1221 | // Now get the other entry that we were waiting on |
| 1222 | EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 5); |
| 1223 | } |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1224 | |
| 1225 | cli_stream->Read(&recv_response, tag(6)); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1226 | verif.Expect(6, expected_cq_result); |
| 1227 | got_tag = verif.Next(cq_.get(), ignore_cq_result); |
| 1228 | GPR_ASSERT((got_tag == 6) || (got_tag == 11 && want_done_tag)); |
| 1229 | if (got_tag == 11) { |
| 1230 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1231 | want_done_tag = false; |
| 1232 | // Now get the other entry that we were waiting on |
| 1233 | EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 6); |
| 1234 | } |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1235 | |
| 1236 | // This is expected to succeed in all cases |
| 1237 | cli_stream->WritesDone(tag(7)); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1238 | verif.Expect(7, true); |
| 1239 | got_tag = verif.Next(cq_.get(), ignore_cq_result); |
| 1240 | GPR_ASSERT((got_tag == 7) || (got_tag == 11 && want_done_tag)); |
| 1241 | if (got_tag == 11) { |
| 1242 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1243 | want_done_tag = false; |
| 1244 | // Now get the other entry that we were waiting on |
| 1245 | EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 7); |
| 1246 | } |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1247 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1248 | // This is expected to fail in all cases i.e for all values of |
Vijay Pai | 018879a | 2016-02-16 09:20:50 -0800 | [diff] [blame] | 1249 | // server_try_cancel. This is because at this point, either there are no |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1250 | // more msgs from the client (because client called WritesDone) or the RPC |
| 1251 | // is cancelled on the server |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1252 | srv_stream.Read(&recv_request, tag(8)); |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1253 | verif.Expect(8, false); |
| 1254 | got_tag = verif.Next(cq_.get(), ignore_cq_result); |
| 1255 | GPR_ASSERT((got_tag == 8) || (got_tag == 11 && want_done_tag)); |
| 1256 | if (got_tag == 11) { |
| 1257 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1258 | want_done_tag = false; |
| 1259 | // Now get the other entry that we were waiting on |
| 1260 | EXPECT_EQ(verif.Next(cq_.get(), ignore_cq_result), 8); |
| 1261 | } |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1262 | |
| 1263 | if (server_try_cancel_thd != NULL) { |
| 1264 | server_try_cancel_thd->join(); |
| 1265 | delete server_try_cancel_thd; |
| 1266 | } |
| 1267 | |
| 1268 | if (server_try_cancel == CANCEL_AFTER_PROCESSING) { |
Sree Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 1269 | srv_ctx.TryCancel(); |
| 1270 | want_done_tag = true; |
| 1271 | verif.Expect(11, true); |
| 1272 | } |
| 1273 | |
| 1274 | if (want_done_tag) { |
| 1275 | verif.Verify(cq_.get()); |
| 1276 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 1277 | want_done_tag = false; |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1278 | } |
| 1279 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1280 | // The RPC has been cancelled at this point for sure (i.e irrespective of |
| 1281 | // the value of `server_try_cancel` is). So, from this point forward, we |
| 1282 | // know that cq results are supposed to return false on server. |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1283 | |
| 1284 | srv_stream.Finish(Status::CANCELLED, tag(9)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1285 | Verifier(GetParam().disable_blocking).Expect(9, false).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1286 | |
| 1287 | cli_stream->Finish(&recv_status, tag(10)); |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1288 | Verifier(GetParam().disable_blocking).Expect(10, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1289 | EXPECT_FALSE(recv_status.ok()); |
| 1290 | EXPECT_EQ(grpc::StatusCode::CANCELLED, recv_status.error_code()); |
| 1291 | } |
| 1292 | }; |
| 1293 | |
| 1294 | TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelBefore) { |
| 1295 | TestClientStreamingServerCancel(CANCEL_BEFORE_PROCESSING); |
| 1296 | } |
| 1297 | |
| 1298 | TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelDuring) { |
| 1299 | TestClientStreamingServerCancel(CANCEL_DURING_PROCESSING); |
| 1300 | } |
| 1301 | |
| 1302 | TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelAfter) { |
| 1303 | TestClientStreamingServerCancel(CANCEL_AFTER_PROCESSING); |
| 1304 | } |
| 1305 | |
| 1306 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelBefore) { |
| 1307 | TestServerStreamingServerCancel(CANCEL_BEFORE_PROCESSING); |
| 1308 | } |
| 1309 | |
| 1310 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelDuring) { |
| 1311 | TestServerStreamingServerCancel(CANCEL_DURING_PROCESSING); |
| 1312 | } |
| 1313 | |
| 1314 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelAfter) { |
| 1315 | TestServerStreamingServerCancel(CANCEL_AFTER_PROCESSING); |
| 1316 | } |
| 1317 | |
| 1318 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelBefore) { |
| 1319 | TestBidiStreamingServerCancel(CANCEL_BEFORE_PROCESSING); |
| 1320 | } |
| 1321 | |
| 1322 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelDuring) { |
| 1323 | TestBidiStreamingServerCancel(CANCEL_DURING_PROCESSING); |
| 1324 | } |
| 1325 | |
| 1326 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelAfter) { |
| 1327 | TestBidiStreamingServerCancel(CANCEL_AFTER_PROCESSING); |
| 1328 | } |
| 1329 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1330 | std::vector<TestScenario> CreateTestScenarios(bool test_disable_blocking, |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 1331 | bool test_secure, |
| 1332 | int test_big_limit) { |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1333 | std::vector<TestScenario> scenarios; |
| 1334 | std::vector<grpc::string> credentials_types; |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 1335 | std::vector<grpc::string> messages; |
| 1336 | |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1337 | credentials_types.push_back(kInsecureCredentialsType); |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 1338 | auto sec_list = GetSecureCredentialsTypeList(); |
| 1339 | for (auto sec = sec_list.begin(); sec != sec_list.end(); sec++) { |
| 1340 | credentials_types.push_back(*sec); |
| 1341 | } |
| 1342 | |
| 1343 | messages.push_back("Hello"); |
| 1344 | for (int sz = 1; sz < test_big_limit; sz *= 2) { |
| 1345 | grpc::string big_msg; |
| 1346 | for (int i = 0; i < sz * 1024; i++) { |
| 1347 | char c = 'a' + (i % 26); |
| 1348 | big_msg += c; |
| 1349 | } |
| 1350 | messages.push_back(big_msg); |
| 1351 | } |
| 1352 | |
| 1353 | for (auto cred = credentials_types.begin(); cred != credentials_types.end(); |
| 1354 | ++cred) { |
| 1355 | for (auto msg = messages.begin(); msg != messages.end(); msg++) { |
| 1356 | scenarios.push_back(TestScenario(false, *cred, *msg)); |
| 1357 | if (test_disable_blocking) { |
| 1358 | scenarios.push_back(TestScenario(true, *cred, *msg)); |
| 1359 | } |
Vijay Pai | df8b62c | 2016-05-02 14:34:24 -0700 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | return scenarios; |
| 1363 | } |
| 1364 | |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 1365 | INSTANTIATE_TEST_CASE_P(AsyncEnd2end, AsyncEnd2endTest, |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 1366 | ::testing::ValuesIn(CreateTestScenarios(true, true, |
| 1367 | 1024))); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1368 | INSTANTIATE_TEST_CASE_P(AsyncEnd2endServerTryCancel, |
| 1369 | AsyncEnd2endServerTryCancelTest, |
Vijay Pai | d7b1e70 | 2016-05-02 15:10:21 -0700 | [diff] [blame^] | 1370 | ::testing::ValuesIn(CreateTestScenarios(false, false, |
| 1371 | 0))); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 1372 | |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 1373 | } // namespace |
| 1374 | } // namespace testing |
| 1375 | } // namespace grpc |
| 1376 | |
| 1377 | int main(int argc, char** argv) { |
| 1378 | grpc_test_init(argc, argv); |
Vijay Pai | b65eda4 | 2016-02-16 13:48:05 -0800 | [diff] [blame] | 1379 | gpr_tls_init(&g_is_async_end2end_test); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 1380 | ::testing::InitGoogleTest(&argc, argv); |
Vijay Pai | b65eda4 | 2016-02-16 13:48:05 -0800 | [diff] [blame] | 1381 | int ret = RUN_ALL_TESTS(); |
| 1382 | gpr_tls_destroy(&g_is_async_end2end_test); |
| 1383 | return ret; |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 1384 | } |