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