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