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