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