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