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