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