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