Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | 3466c4b | 2016-01-12 10:26:04 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, 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> |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 46 | #include <gtest/gtest.h> |
| 47 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 48 | #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 49 | #include "src/proto/grpc/testing/echo.grpc.pb.h" |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 50 | #include "test/core/util/port.h" |
| 51 | #include "test/core/util/test_config.h" |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 52 | #include "test/cpp/util/string_ref_helper.h" |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 53 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 54 | #ifdef GPR_POSIX_SOCKET |
| 55 | #include "src/core/iomgr/pollset_posix.h" |
| 56 | #endif |
| 57 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 58 | using grpc::testing::EchoRequest; |
| 59 | using grpc::testing::EchoResponse; |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 60 | using std::chrono::system_clock; |
| 61 | |
| 62 | namespace grpc { |
| 63 | namespace testing { |
| 64 | |
| 65 | namespace { |
| 66 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 67 | void* tag(int i) { return (void*)(intptr_t)i; } |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 68 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 69 | #ifdef GPR_POSIX_SOCKET |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 70 | static int assert_non_blocking_poll(struct pollfd* pfds, nfds_t nfds, |
| 71 | int timeout) { |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 72 | GPR_ASSERT(timeout == 0); |
| 73 | return poll(pfds, nfds, timeout); |
| 74 | } |
| 75 | |
| 76 | class PollOverride { |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 77 | public: |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 78 | PollOverride(grpc_poll_function_type f) { |
| 79 | prev_ = grpc_poll_function; |
| 80 | grpc_poll_function = f; |
| 81 | } |
| 82 | |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 83 | ~PollOverride() { grpc_poll_function = prev_; } |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | grpc_poll_function_type prev_; |
| 87 | }; |
| 88 | |
| 89 | class PollingCheckRegion : public PollOverride { |
| 90 | public: |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 91 | explicit PollingCheckRegion(bool allow_blocking) |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 92 | : PollOverride(allow_blocking ? poll : assert_non_blocking_poll) {} |
| 93 | }; |
| 94 | #else |
| 95 | class PollingCheckRegion { |
| 96 | public: |
| 97 | explicit PollingCheckRegion(bool allow_blocking) {} |
| 98 | }; |
| 99 | #endif |
| 100 | |
| 101 | class Verifier : public PollingCheckRegion { |
| 102 | public: |
| 103 | explicit Verifier(bool spin) : PollingCheckRegion(!spin), spin_(spin) {} |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 104 | Verifier& Expect(int i, bool expect_ok) { |
| 105 | expectations_[tag(i)] = expect_ok; |
| 106 | return *this; |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 107 | } |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 108 | |
| 109 | void Verify(CompletionQueue* cq) { Verify(cq, false); } |
| 110 | |
| 111 | void Verify(CompletionQueue* cq, bool ignore_ok) { |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 112 | GPR_ASSERT(!expectations_.empty()); |
| 113 | while (!expectations_.empty()) { |
| 114 | bool ok; |
| 115 | void* got_tag; |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 116 | if (spin_) { |
| 117 | for (;;) { |
| 118 | auto r = cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME)); |
| 119 | if (r == CompletionQueue::TIMEOUT) continue; |
| 120 | if (r == CompletionQueue::GOT_EVENT) break; |
| 121 | gpr_log(GPR_ERROR, "unexpected result from AsyncNext"); |
| 122 | abort(); |
| 123 | } |
| 124 | } else { |
| 125 | EXPECT_TRUE(cq->Next(&got_tag, &ok)); |
| 126 | } |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 127 | auto it = expectations_.find(got_tag); |
| 128 | EXPECT_TRUE(it != expectations_.end()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 129 | if (!ignore_ok) { |
| 130 | EXPECT_EQ(it->second, ok); |
| 131 | } |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 132 | expectations_.erase(it); |
| 133 | } |
| 134 | } |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 135 | void Verify(CompletionQueue* cq, |
| 136 | std::chrono::system_clock::time_point deadline) { |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 137 | if (expectations_.empty()) { |
| 138 | bool ok; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 139 | void* got_tag; |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 140 | if (spin_) { |
| 141 | while (std::chrono::system_clock::now() < deadline) { |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 142 | EXPECT_EQ( |
| 143 | cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME)), |
| 144 | CompletionQueue::TIMEOUT); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 145 | } |
| 146 | } else { |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 147 | EXPECT_EQ(cq->AsyncNext(&got_tag, &ok, deadline), |
| 148 | CompletionQueue::TIMEOUT); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 149 | } |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 150 | } else { |
| 151 | while (!expectations_.empty()) { |
| 152 | bool ok; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 153 | void* got_tag; |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 154 | if (spin_) { |
| 155 | for (;;) { |
| 156 | GPR_ASSERT(std::chrono::system_clock::now() < deadline); |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 157 | auto r = |
| 158 | cq->AsyncNext(&got_tag, &ok, gpr_time_0(GPR_CLOCK_REALTIME)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 159 | if (r == CompletionQueue::TIMEOUT) continue; |
| 160 | if (r == CompletionQueue::GOT_EVENT) break; |
| 161 | gpr_log(GPR_ERROR, "unexpected result from AsyncNext"); |
| 162 | abort(); |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 163 | } |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 164 | } else { |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 165 | EXPECT_EQ(cq->AsyncNext(&got_tag, &ok, deadline), |
| 166 | CompletionQueue::GOT_EVENT); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 167 | } |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 168 | auto it = expectations_.find(got_tag); |
| 169 | EXPECT_TRUE(it != expectations_.end()); |
| 170 | EXPECT_EQ(it->second, ok); |
| 171 | expectations_.erase(it); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | private: |
| 177 | std::map<void*, bool> expectations_; |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 178 | bool spin_; |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 179 | }; |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 180 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 181 | class AsyncEnd2endTest : public ::testing::TestWithParam<bool> { |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 182 | protected: |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 183 | AsyncEnd2endTest() {} |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 184 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 185 | void SetUp() GRPC_OVERRIDE { |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 186 | int port = grpc_pick_unused_port_or_die(); |
| 187 | server_address_ << "localhost:" << port; |
vjpai | 017ed62 | 2015-12-09 10:42:54 -0800 | [diff] [blame] | 188 | |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 189 | // Setup server |
| 190 | ServerBuilder builder; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 191 | builder.AddListeningPort(server_address_.str(), |
| 192 | grpc::InsecureServerCredentials()); |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 193 | builder.RegisterService(&service_); |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 194 | cq_ = builder.AddCompletionQueue(); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 195 | server_ = builder.BuildAndStart(); |
| 196 | } |
| 197 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 198 | void TearDown() GRPC_OVERRIDE { |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 199 | server_->Shutdown(); |
| 200 | void* ignored_tag; |
| 201 | bool ignored_ok; |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 202 | cq_->Shutdown(); |
| 203 | while (cq_->Next(&ignored_tag, &ignored_ok)) |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 204 | ; |
| 205 | } |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 206 | |
| 207 | void ResetStub() { |
yang-g | 730055d | 2015-08-27 12:29:45 -0700 | [diff] [blame] | 208 | std::shared_ptr<Channel> channel = |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 209 | CreateChannel(server_address_.str(), InsecureChannelCredentials()); |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 210 | stub_ = grpc::testing::EchoTestService::NewStub(channel); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 213 | void SendRpc(int num_rpcs) { |
| 214 | for (int i = 0; i < num_rpcs; i++) { |
| 215 | EchoRequest send_request; |
| 216 | EchoRequest recv_request; |
| 217 | EchoResponse send_response; |
| 218 | EchoResponse recv_response; |
| 219 | Status recv_status; |
| 220 | |
| 221 | ClientContext cli_ctx; |
| 222 | ServerContext srv_ctx; |
| 223 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 224 | |
| 225 | send_request.set_message("Hello"); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 226 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 227 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 228 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 229 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 230 | cq_.get(), tag(2)); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 231 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 232 | Verifier(GetParam()).Expect(2, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 233 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 234 | |
| 235 | send_response.set_message(recv_request.message()); |
| 236 | response_writer.Finish(send_response, Status::OK, tag(3)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 237 | Verifier(GetParam()).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 238 | |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 239 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 240 | Verifier(GetParam()).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 241 | |
| 242 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 243 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 247 | std::unique_ptr<ServerCompletionQueue> cq_; |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 248 | std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 249 | std::unique_ptr<Server> server_; |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 250 | grpc::testing::EchoTestService::AsyncService service_; |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 251 | std::ostringstream server_address_; |
| 252 | }; |
| 253 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 254 | TEST_P(AsyncEnd2endTest, SimpleRpc) { |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 255 | ResetStub(); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 256 | SendRpc(1); |
| 257 | } |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame] | 258 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 259 | TEST_P(AsyncEnd2endTest, SequentialRpcs) { |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 260 | ResetStub(); |
| 261 | SendRpc(10); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 262 | } |
| 263 | |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 264 | // Test a simple RPC using the async version of Next |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 265 | TEST_P(AsyncEnd2endTest, AsyncNextRpc) { |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 266 | ResetStub(); |
| 267 | |
| 268 | EchoRequest send_request; |
| 269 | EchoRequest recv_request; |
| 270 | EchoResponse send_response; |
| 271 | EchoResponse recv_response; |
| 272 | Status recv_status; |
| 273 | |
| 274 | ClientContext cli_ctx; |
| 275 | ServerContext srv_ctx; |
| 276 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 277 | |
| 278 | send_request.set_message("Hello"); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 279 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 280 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 281 | |
Yang Gao | 757afae | 2015-03-17 15:49:26 -0700 | [diff] [blame] | 282 | std::chrono::system_clock::time_point time_now( |
Craig Tiller | f51199f | 2015-05-08 09:32:53 -0700 | [diff] [blame] | 283 | std::chrono::system_clock::now()); |
| 284 | std::chrono::system_clock::time_point time_limit( |
| 285 | std::chrono::system_clock::now() + std::chrono::seconds(10)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 286 | Verifier(GetParam()).Verify(cq_.get(), time_now); |
| 287 | Verifier(GetParam()).Verify(cq_.get(), time_now); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 288 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 289 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 290 | cq_.get(), tag(2)); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 291 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 292 | Verifier(GetParam()).Expect(2, true).Verify(cq_.get(), time_limit); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 293 | EXPECT_EQ(send_request.message(), recv_request.message()); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 294 | |
| 295 | send_response.set_message(recv_request.message()); |
| 296 | response_writer.Finish(send_response, Status::OK, tag(3)); |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 297 | Verifier(GetParam()) |
| 298 | .Expect(3, true) |
| 299 | .Verify(cq_.get(), std::chrono::system_clock::time_point::max()); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 300 | |
| 301 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 302 | Verifier(GetParam()) |
| 303 | .Expect(4, true) |
| 304 | .Verify(cq_.get(), std::chrono::system_clock::time_point::max()); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 305 | |
| 306 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 307 | EXPECT_TRUE(recv_status.ok()); |
vjpai | 7aadf46 | 2015-03-16 23:58:44 -0700 | [diff] [blame] | 308 | } |
Yang Gao | 757afae | 2015-03-17 15:49:26 -0700 | [diff] [blame] | 309 | |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 310 | // Two pings and a final pong. |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 311 | TEST_P(AsyncEnd2endTest, SimpleClientStreaming) { |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 312 | ResetStub(); |
| 313 | |
| 314 | EchoRequest send_request; |
| 315 | EchoRequest recv_request; |
| 316 | EchoResponse send_response; |
| 317 | EchoResponse recv_response; |
| 318 | Status recv_status; |
| 319 | ClientContext cli_ctx; |
| 320 | ServerContext srv_ctx; |
| 321 | ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 322 | |
| 323 | send_request.set_message("Hello"); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 324 | std::unique_ptr<ClientAsyncWriter<EchoRequest>> cli_stream( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 325 | stub_->AsyncRequestStream(&cli_ctx, &recv_response, cq_.get(), tag(1))); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 326 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 327 | service_.RequestRequestStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 328 | tag(2)); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 329 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 330 | Verifier(GetParam()).Expect(2, true).Expect(1, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 331 | |
| 332 | cli_stream->Write(send_request, tag(3)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 333 | Verifier(GetParam()).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 334 | |
| 335 | srv_stream.Read(&recv_request, tag(4)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 336 | Verifier(GetParam()).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 337 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 338 | |
| 339 | cli_stream->Write(send_request, tag(5)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 340 | Verifier(GetParam()).Expect(5, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 341 | |
| 342 | srv_stream.Read(&recv_request, tag(6)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 343 | Verifier(GetParam()).Expect(6, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 344 | |
| 345 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 346 | cli_stream->WritesDone(tag(7)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 347 | Verifier(GetParam()).Expect(7, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 348 | |
| 349 | srv_stream.Read(&recv_request, tag(8)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 350 | Verifier(GetParam()).Expect(8, false).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 351 | |
| 352 | send_response.set_message(recv_request.message()); |
| 353 | srv_stream.Finish(send_response, Status::OK, tag(9)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 354 | Verifier(GetParam()).Expect(9, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 355 | |
| 356 | cli_stream->Finish(&recv_status, tag(10)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 357 | Verifier(GetParam()).Expect(10, true).Verify(cq_.get()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 358 | |
| 359 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 360 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 363 | // One ping, two pongs. |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 364 | TEST_P(AsyncEnd2endTest, SimpleServerStreaming) { |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 365 | ResetStub(); |
| 366 | |
| 367 | EchoRequest send_request; |
| 368 | EchoRequest recv_request; |
| 369 | EchoResponse send_response; |
| 370 | EchoResponse recv_response; |
| 371 | Status recv_status; |
| 372 | ClientContext cli_ctx; |
| 373 | ServerContext srv_ctx; |
| 374 | ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx); |
| 375 | |
| 376 | send_request.set_message("Hello"); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 377 | std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 378 | stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1))); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 379 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 380 | service_.RequestResponseStream(&srv_ctx, &recv_request, &srv_stream, |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 381 | cq_.get(), cq_.get(), tag(2)); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 382 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 383 | Verifier(GetParam()).Expect(1, true).Expect(2, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 384 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 385 | |
| 386 | send_response.set_message(recv_request.message()); |
| 387 | srv_stream.Write(send_response, tag(3)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 388 | Verifier(GetParam()).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 389 | |
| 390 | cli_stream->Read(&recv_response, tag(4)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 391 | Verifier(GetParam()).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 392 | EXPECT_EQ(send_response.message(), recv_response.message()); |
| 393 | |
| 394 | srv_stream.Write(send_response, tag(5)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 395 | Verifier(GetParam()).Expect(5, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 396 | |
| 397 | cli_stream->Read(&recv_response, tag(6)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 398 | Verifier(GetParam()).Expect(6, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 399 | EXPECT_EQ(send_response.message(), recv_response.message()); |
| 400 | |
| 401 | srv_stream.Finish(Status::OK, tag(7)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 402 | Verifier(GetParam()).Expect(7, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 403 | |
| 404 | cli_stream->Read(&recv_response, tag(8)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 405 | Verifier(GetParam()).Expect(8, false).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 406 | |
| 407 | cli_stream->Finish(&recv_status, tag(9)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 408 | Verifier(GetParam()).Expect(9, true).Verify(cq_.get()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 409 | |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 410 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 0e0d8e1 | 2015-02-13 14:40:41 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // One ping, one pong. |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 414 | TEST_P(AsyncEnd2endTest, SimpleBidiStreaming) { |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 415 | ResetStub(); |
| 416 | |
| 417 | EchoRequest send_request; |
| 418 | EchoRequest recv_request; |
| 419 | EchoResponse send_response; |
| 420 | EchoResponse recv_response; |
| 421 | Status recv_status; |
| 422 | ClientContext cli_ctx; |
| 423 | ServerContext srv_ctx; |
| 424 | ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 425 | |
| 426 | send_request.set_message("Hello"); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 427 | std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>> |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 428 | cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1))); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 429 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 430 | service_.RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 431 | tag(2)); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 432 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 433 | Verifier(GetParam()).Expect(1, true).Expect(2, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 434 | |
| 435 | cli_stream->Write(send_request, tag(3)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 436 | Verifier(GetParam()).Expect(3, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 437 | |
| 438 | srv_stream.Read(&recv_request, tag(4)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 439 | Verifier(GetParam()).Expect(4, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 440 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 441 | |
| 442 | send_response.set_message(recv_request.message()); |
| 443 | srv_stream.Write(send_response, tag(5)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 444 | Verifier(GetParam()).Expect(5, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 445 | |
| 446 | cli_stream->Read(&recv_response, tag(6)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 447 | Verifier(GetParam()).Expect(6, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 448 | EXPECT_EQ(send_response.message(), recv_response.message()); |
| 449 | |
| 450 | cli_stream->WritesDone(tag(7)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 451 | Verifier(GetParam()).Expect(7, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 452 | |
| 453 | srv_stream.Read(&recv_request, tag(8)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 454 | Verifier(GetParam()).Expect(8, false).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 455 | |
| 456 | srv_stream.Finish(Status::OK, tag(9)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 457 | Verifier(GetParam()).Expect(9, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 458 | |
| 459 | cli_stream->Finish(&recv_status, tag(10)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 460 | Verifier(GetParam()).Expect(10, true).Verify(cq_.get()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 461 | |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 462 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | c05b6cb | 2015-02-13 00:34:10 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 465 | // Metadata tests |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 466 | TEST_P(AsyncEnd2endTest, ClientInitialMetadataRpc) { |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 467 | ResetStub(); |
| 468 | |
| 469 | EchoRequest send_request; |
| 470 | EchoRequest recv_request; |
| 471 | EchoResponse send_response; |
| 472 | EchoResponse recv_response; |
| 473 | Status recv_status; |
| 474 | |
| 475 | ClientContext cli_ctx; |
| 476 | ServerContext srv_ctx; |
| 477 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 478 | |
| 479 | send_request.set_message("Hello"); |
| 480 | std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
| 481 | std::pair<grpc::string, grpc::string> meta2("key2", "val2"); |
Craig Tiller | 6f87164 | 2016-02-03 16:15:31 -0800 | [diff] [blame^] | 482 | std::pair<grpc::string, grpc::string> meta3("g.r.d-bin", "xyz"); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 483 | cli_ctx.AddMetadata(meta1.first, meta1.second); |
| 484 | cli_ctx.AddMetadata(meta2.first, meta2.second); |
Craig Tiller | 6f87164 | 2016-02-03 16:15:31 -0800 | [diff] [blame^] | 485 | cli_ctx.AddMetadata(meta3.first, meta3.second); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 486 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 487 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 488 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 489 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 490 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 491 | cq_.get(), tag(2)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 492 | Verifier(GetParam()).Expect(2, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 493 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 494 | auto client_initial_metadata = srv_ctx.client_metadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 495 | EXPECT_EQ(meta1.second, |
| 496 | ToString(client_initial_metadata.find(meta1.first)->second)); |
| 497 | EXPECT_EQ(meta2.second, |
| 498 | ToString(client_initial_metadata.find(meta2.first)->second)); |
Craig Tiller | 6f87164 | 2016-02-03 16:15:31 -0800 | [diff] [blame^] | 499 | EXPECT_EQ(meta3.second, |
| 500 | ToString(client_initial_metadata.find(meta3.first)->second)); |
Craig Tiller | 8bf2dca | 2015-07-10 13:08:41 -0700 | [diff] [blame] | 501 | EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2)); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 502 | |
| 503 | send_response.set_message(recv_request.message()); |
| 504 | response_writer.Finish(send_response, Status::OK, tag(3)); |
| 505 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 506 | Verifier(GetParam()).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 507 | |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 508 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 509 | Verifier(GetParam()).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 510 | |
| 511 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 512 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 515 | TEST_P(AsyncEnd2endTest, ServerInitialMetadataRpc) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 516 | ResetStub(); |
| 517 | |
| 518 | EchoRequest send_request; |
| 519 | EchoRequest recv_request; |
| 520 | EchoResponse send_response; |
| 521 | EchoResponse recv_response; |
| 522 | Status recv_status; |
| 523 | |
| 524 | ClientContext cli_ctx; |
| 525 | ServerContext srv_ctx; |
| 526 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 527 | |
| 528 | send_request.set_message("Hello"); |
| 529 | std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
| 530 | std::pair<grpc::string, grpc::string> meta2("key2", "val2"); |
| 531 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 532 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 533 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 534 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 535 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 536 | cq_.get(), tag(2)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 537 | Verifier(GetParam()).Expect(2, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 538 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 539 | srv_ctx.AddInitialMetadata(meta1.first, meta1.second); |
| 540 | srv_ctx.AddInitialMetadata(meta2.first, meta2.second); |
| 541 | response_writer.SendInitialMetadata(tag(3)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 542 | Verifier(GetParam()).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 543 | |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 544 | response_reader->ReadInitialMetadata(tag(4)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 545 | Verifier(GetParam()).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 546 | auto server_initial_metadata = cli_ctx.GetServerInitialMetadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 547 | EXPECT_EQ(meta1.second, |
| 548 | ToString(server_initial_metadata.find(meta1.first)->second)); |
| 549 | EXPECT_EQ(meta2.second, |
| 550 | ToString(server_initial_metadata.find(meta2.first)->second)); |
vjpai | d5577aa | 2015-02-18 22:26:48 -0800 | [diff] [blame] | 551 | EXPECT_EQ(static_cast<size_t>(2), server_initial_metadata.size()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 552 | |
| 553 | send_response.set_message(recv_request.message()); |
| 554 | response_writer.Finish(send_response, Status::OK, tag(5)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 555 | Verifier(GetParam()).Expect(5, true).Verify(cq_.get()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 556 | |
| 557 | response_reader->Finish(&recv_response, &recv_status, tag(6)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 558 | Verifier(GetParam()).Expect(6, true).Verify(cq_.get()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 559 | |
| 560 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 561 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 564 | TEST_P(AsyncEnd2endTest, ServerTrailingMetadataRpc) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 565 | ResetStub(); |
| 566 | |
| 567 | EchoRequest send_request; |
| 568 | EchoRequest recv_request; |
| 569 | EchoResponse send_response; |
| 570 | EchoResponse recv_response; |
| 571 | Status recv_status; |
| 572 | |
| 573 | ClientContext cli_ctx; |
| 574 | ServerContext srv_ctx; |
| 575 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 576 | |
| 577 | send_request.set_message("Hello"); |
| 578 | std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
| 579 | std::pair<grpc::string, grpc::string> meta2("key2", "val2"); |
| 580 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 581 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 582 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 583 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 584 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 585 | cq_.get(), tag(2)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 586 | Verifier(GetParam()).Expect(2, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 587 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 588 | response_writer.SendInitialMetadata(tag(3)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 589 | Verifier(GetParam()).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 590 | |
| 591 | send_response.set_message(recv_request.message()); |
| 592 | srv_ctx.AddTrailingMetadata(meta1.first, meta1.second); |
| 593 | srv_ctx.AddTrailingMetadata(meta2.first, meta2.second); |
| 594 | response_writer.Finish(send_response, Status::OK, tag(4)); |
| 595 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 596 | Verifier(GetParam()).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 597 | |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 598 | response_reader->Finish(&recv_response, &recv_status, tag(5)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 599 | Verifier(GetParam()).Expect(5, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 600 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 601 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 602 | auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 603 | EXPECT_EQ(meta1.second, |
| 604 | ToString(server_trailing_metadata.find(meta1.first)->second)); |
| 605 | EXPECT_EQ(meta2.second, |
| 606 | ToString(server_trailing_metadata.find(meta2.first)->second)); |
vjpai | d5577aa | 2015-02-18 22:26:48 -0800 | [diff] [blame] | 607 | EXPECT_EQ(static_cast<size_t>(2), server_trailing_metadata.size()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 610 | TEST_P(AsyncEnd2endTest, MetadataRpc) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 611 | ResetStub(); |
| 612 | |
| 613 | EchoRequest send_request; |
| 614 | EchoRequest recv_request; |
| 615 | EchoResponse send_response; |
| 616 | EchoResponse recv_response; |
| 617 | Status recv_status; |
| 618 | |
| 619 | ClientContext cli_ctx; |
| 620 | ServerContext srv_ctx; |
| 621 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 622 | |
| 623 | send_request.set_message("Hello"); |
| 624 | std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 625 | std::pair<grpc::string, grpc::string> meta2( |
Vijay Pai | 92a928f | 2015-03-26 16:30:22 -0400 | [diff] [blame] | 626 | "key2-bin", |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 627 | 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] | 628 | std::pair<grpc::string, grpc::string> meta3("key3", "val3"); |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 629 | std::pair<grpc::string, grpc::string> meta6( |
| 630 | "key4-bin", |
Vijay Pai | 92a928f | 2015-03-26 16:30:22 -0400 | [diff] [blame] | 631 | 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] | 632 | 14)); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 633 | std::pair<grpc::string, grpc::string> meta5("key5", "val5"); |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 634 | std::pair<grpc::string, grpc::string> meta4( |
| 635 | "key6-bin", |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 636 | grpc::string( |
| 637 | "\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] | 638 | |
| 639 | cli_ctx.AddMetadata(meta1.first, meta1.second); |
| 640 | cli_ctx.AddMetadata(meta2.first, meta2.second); |
| 641 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 642 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 643 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 644 | |
Craig Tiller | 06cf3cc | 2015-05-13 13:11:01 -0700 | [diff] [blame] | 645 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 646 | cq_.get(), tag(2)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 647 | Verifier(GetParam()).Expect(2, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 648 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 649 | auto client_initial_metadata = srv_ctx.client_metadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 650 | EXPECT_EQ(meta1.second, |
| 651 | ToString(client_initial_metadata.find(meta1.first)->second)); |
| 652 | EXPECT_EQ(meta2.second, |
| 653 | ToString(client_initial_metadata.find(meta2.first)->second)); |
Craig Tiller | 8bf2dca | 2015-07-10 13:08:41 -0700 | [diff] [blame] | 654 | EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2)); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 655 | |
| 656 | srv_ctx.AddInitialMetadata(meta3.first, meta3.second); |
| 657 | srv_ctx.AddInitialMetadata(meta4.first, meta4.second); |
| 658 | response_writer.SendInitialMetadata(tag(3)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 659 | Verifier(GetParam()).Expect(3, true).Verify(cq_.get()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 660 | response_reader->ReadInitialMetadata(tag(4)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 661 | Verifier(GetParam()).Expect(4, true).Verify(cq_.get()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 662 | auto server_initial_metadata = cli_ctx.GetServerInitialMetadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 663 | EXPECT_EQ(meta3.second, |
| 664 | ToString(server_initial_metadata.find(meta3.first)->second)); |
| 665 | EXPECT_EQ(meta4.second, |
| 666 | ToString(server_initial_metadata.find(meta4.first)->second)); |
Craig Tiller | 8bf2dca | 2015-07-10 13:08:41 -0700 | [diff] [blame] | 667 | EXPECT_GE(server_initial_metadata.size(), static_cast<size_t>(2)); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 668 | |
| 669 | send_response.set_message(recv_request.message()); |
| 670 | srv_ctx.AddTrailingMetadata(meta5.first, meta5.second); |
| 671 | srv_ctx.AddTrailingMetadata(meta6.first, meta6.second); |
| 672 | response_writer.Finish(send_response, Status::OK, tag(5)); |
| 673 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 674 | Verifier(GetParam()).Expect(5, true).Verify(cq_.get()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 675 | |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 676 | response_reader->Finish(&recv_response, &recv_status, tag(6)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 677 | Verifier(GetParam()).Expect(6, true).Verify(cq_.get()); |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 678 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 679 | EXPECT_TRUE(recv_status.ok()); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 680 | auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata(); |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 681 | EXPECT_EQ(meta5.second, |
| 682 | ToString(server_trailing_metadata.find(meta5.first)->second)); |
| 683 | EXPECT_EQ(meta6.second, |
| 684 | ToString(server_trailing_metadata.find(meta6.first)->second)); |
Craig Tiller | 8bf2dca | 2015-07-10 13:08:41 -0700 | [diff] [blame] | 685 | EXPECT_GE(server_trailing_metadata.size(), static_cast<size_t>(2)); |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 686 | } |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 687 | |
| 688 | // Server uses AsyncNotifyWhenDone API to check for cancellation |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 689 | TEST_P(AsyncEnd2endTest, ServerCheckCancellation) { |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 690 | ResetStub(); |
| 691 | |
| 692 | EchoRequest send_request; |
| 693 | EchoRequest recv_request; |
| 694 | EchoResponse send_response; |
| 695 | EchoResponse recv_response; |
| 696 | Status recv_status; |
| 697 | |
| 698 | ClientContext cli_ctx; |
| 699 | ServerContext srv_ctx; |
| 700 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 701 | |
| 702 | send_request.set_message("Hello"); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 703 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 704 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 705 | |
| 706 | srv_ctx.AsyncNotifyWhenDone(tag(5)); |
| 707 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 708 | cq_.get(), tag(2)); |
| 709 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 710 | Verifier(GetParam()).Expect(2, true).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 711 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 712 | |
| 713 | cli_ctx.TryCancel(); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 714 | Verifier(GetParam()).Expect(5, true).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 715 | EXPECT_TRUE(srv_ctx.IsCancelled()); |
| 716 | |
| 717 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 718 | Verifier(GetParam()).Expect(4, false).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 719 | |
| 720 | EXPECT_EQ(StatusCode::CANCELLED, recv_status.error_code()); |
| 721 | } |
| 722 | |
| 723 | // Server uses AsyncNotifyWhenDone API to check for normal finish |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 724 | TEST_P(AsyncEnd2endTest, ServerCheckDone) { |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 725 | ResetStub(); |
| 726 | |
| 727 | EchoRequest send_request; |
| 728 | EchoRequest recv_request; |
| 729 | EchoResponse send_response; |
| 730 | EchoResponse recv_response; |
| 731 | Status recv_status; |
| 732 | |
| 733 | ClientContext cli_ctx; |
| 734 | ServerContext srv_ctx; |
| 735 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 736 | |
| 737 | send_request.set_message("Hello"); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 738 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 739 | stub_->AsyncEcho(&cli_ctx, send_request, cq_.get())); |
| 740 | |
| 741 | srv_ctx.AsyncNotifyWhenDone(tag(5)); |
| 742 | service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, cq_.get(), |
| 743 | cq_.get(), tag(2)); |
| 744 | |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 745 | Verifier(GetParam()).Expect(2, true).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 746 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 747 | |
| 748 | send_response.set_message(recv_request.message()); |
| 749 | response_writer.Finish(send_response, Status::OK, tag(3)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 750 | Verifier(GetParam()).Expect(3, true).Verify(cq_.get()); |
| 751 | Verifier(GetParam()).Expect(5, true).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 752 | EXPECT_FALSE(srv_ctx.IsCancelled()); |
| 753 | |
| 754 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 755 | Verifier(GetParam()).Expect(4, true).Verify(cq_.get()); |
yang-g | b335256 | 2015-08-04 14:42:06 -0700 | [diff] [blame] | 756 | |
| 757 | EXPECT_EQ(send_response.message(), recv_response.message()); |
| 758 | EXPECT_TRUE(recv_status.ok()); |
| 759 | } |
| 760 | |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 761 | TEST_P(AsyncEnd2endTest, UnimplementedRpc) { |
yang-g | 730055d | 2015-08-27 12:29:45 -0700 | [diff] [blame] | 762 | std::shared_ptr<Channel> channel = |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 763 | CreateChannel(server_address_.str(), InsecureChannelCredentials()); |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 764 | std::unique_ptr<grpc::testing::UnimplementedService::Stub> stub; |
| 765 | stub = grpc::testing::UnimplementedService::NewStub(channel); |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 766 | EchoRequest send_request; |
| 767 | EchoResponse recv_response; |
| 768 | Status recv_status; |
| 769 | |
| 770 | ClientContext cli_ctx; |
| 771 | send_request.set_message("Hello"); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 772 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader( |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 773 | stub->AsyncUnimplemented(&cli_ctx, send_request, cq_.get())); |
| 774 | |
| 775 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 776 | Verifier(GetParam()).Expect(4, false).Verify(cq_.get()); |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 777 | |
| 778 | EXPECT_EQ(StatusCode::UNIMPLEMENTED, recv_status.error_code()); |
| 779 | EXPECT_EQ("", recv_status.error_message()); |
| 780 | } |
| 781 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 782 | // This class is for testing scenarios where RPCs are cancelled on the server |
| 783 | // by calling ServerContext::TryCancel() |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 784 | class AsyncEnd2endServerTryCancelTest : public AsyncEnd2endTest { |
| 785 | protected: |
| 786 | typedef enum { |
| 787 | DO_NOT_CANCEL = 0, |
| 788 | CANCEL_BEFORE_PROCESSING, |
| 789 | CANCEL_DURING_PROCESSING, |
| 790 | CANCEL_AFTER_PROCESSING |
| 791 | } ServerTryCancelRequestPhase; |
| 792 | |
| 793 | void ServerTryCancel(ServerContext* context) { |
| 794 | EXPECT_FALSE(context->IsCancelled()); |
| 795 | context->TryCancel(); |
| 796 | gpr_log(GPR_INFO, "Server called TryCancel()"); |
| 797 | EXPECT_TRUE(context->IsCancelled()); |
| 798 | } |
| 799 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 800 | // Helper for testing client-streaming RPCs which are cancelled on the server. |
| 801 | // Depending on the value of server_try_cancel parameter, this will test one |
| 802 | // of the following three scenarios: |
| 803 | // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading |
| 804 | // any messages from the client |
| 805 | // |
| 806 | // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading |
| 807 | // messages from the client |
| 808 | // |
| 809 | // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all |
| 810 | // messages from the client (but before sending any status back to the |
| 811 | // client) |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 812 | void TestClientStreamingServerCancel( |
| 813 | ServerTryCancelRequestPhase server_try_cancel) { |
| 814 | ResetStub(); |
| 815 | |
| 816 | EchoRequest send_request; |
| 817 | EchoRequest recv_request; |
| 818 | EchoResponse send_response; |
| 819 | EchoResponse recv_response; |
| 820 | Status recv_status; |
| 821 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 822 | ClientContext cli_ctx; |
| 823 | ServerContext srv_ctx; |
| 824 | ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 825 | |
| 826 | // Initiate the 'RequestStream' call on client |
| 827 | std::unique_ptr<ClientAsyncWriter<EchoRequest>> cli_stream( |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 828 | stub_->AsyncRequestStream(&cli_ctx, &recv_response, cq_.get(), tag(1))); |
| 829 | Verifier(GetParam()).Expect(1, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 830 | |
| 831 | // On the server, request to be notified of 'RequestStream' calls |
| 832 | // and receive the 'RequestStream' call just made by the client |
| 833 | service_.RequestRequestStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 834 | tag(2)); |
| 835 | Verifier(GetParam()).Expect(2, true).Verify(cq_.get()); |
| 836 | |
| 837 | // Client sends 3 messages (tags 3, 4 and 5) |
| 838 | for (int tag_idx = 3; tag_idx <= 5; tag_idx++) { |
| 839 | send_request.set_message("Ping " + std::to_string(tag_idx)); |
| 840 | cli_stream->Write(send_request, tag(tag_idx)); |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 841 | Verifier(GetParam()).Expect(tag_idx, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 842 | } |
| 843 | cli_stream->WritesDone(tag(6)); |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 844 | Verifier(GetParam()).Expect(6, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 845 | |
| 846 | bool expected_server_cq_result = true; |
| 847 | bool ignore_cq_result = false; |
| 848 | |
| 849 | if (server_try_cancel == CANCEL_BEFORE_PROCESSING) { |
| 850 | ServerTryCancel(&srv_ctx); |
| 851 | |
| 852 | // Since cancellation is done before server reads any results, we know |
| 853 | // for sure that all cq results will return false from this point forward |
| 854 | expected_server_cq_result = false; |
| 855 | } |
| 856 | |
| 857 | std::thread* server_try_cancel_thd = NULL; |
| 858 | if (server_try_cancel == CANCEL_DURING_PROCESSING) { |
| 859 | server_try_cancel_thd = new std::thread( |
| 860 | &AsyncEnd2endServerTryCancelTest::ServerTryCancel, this, &srv_ctx); |
| 861 | // Server will cancel the RPC in a parallel thread while reading the |
| 862 | // requests from the client. Since the cancellation can happen at anytime, |
| 863 | // some of the cq results (i.e those until cancellation) might be true but |
| 864 | // its non deterministic. So better to ignore the cq results |
| 865 | ignore_cq_result = true; |
| 866 | } |
| 867 | |
| 868 | // Server reads 3 messages (tags 6, 7 and 8) |
| 869 | for (int tag_idx = 6; tag_idx <= 8; tag_idx++) { |
| 870 | srv_stream.Read(&recv_request, tag(tag_idx)); |
| 871 | Verifier(GetParam()) |
| 872 | .Expect(tag_idx, expected_server_cq_result) |
| 873 | .Verify(cq_.get(), ignore_cq_result); |
| 874 | } |
| 875 | |
| 876 | if (server_try_cancel_thd != NULL) { |
| 877 | server_try_cancel_thd->join(); |
| 878 | delete server_try_cancel_thd; |
| 879 | } |
| 880 | |
| 881 | if (server_try_cancel == CANCEL_AFTER_PROCESSING) { |
| 882 | ServerTryCancel(&srv_ctx); |
| 883 | } |
| 884 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 885 | // The RPC has been cancelled at this point for sure (i.e irrespective of |
| 886 | // the value of `server_try_cancel` is). So, from this point forward, we |
| 887 | // know that cq results are supposed to return false on server. |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 888 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 889 | // Server sends the final message and cancelled status (but the RPC is |
| 890 | // already cancelled at this point. So we expect the operation to fail) |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 891 | srv_stream.Finish(send_response, Status::CANCELLED, tag(9)); |
| 892 | Verifier(GetParam()).Expect(9, false).Verify(cq_.get()); |
| 893 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 894 | // Client will see the cancellation |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 895 | cli_stream->Finish(&recv_status, tag(10)); |
Sree Kuchibhotla | 369a04a | 2016-02-01 10:53:13 -0800 | [diff] [blame] | 896 | // 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] | 897 | // issue #4972) |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 898 | Verifier(GetParam()).Expect(10, false).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 899 | EXPECT_FALSE(recv_status.ok()); |
| 900 | EXPECT_EQ(::grpc::StatusCode::CANCELLED, recv_status.error_code()); |
| 901 | } |
| 902 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 903 | // Helper for testing server-streaming RPCs which are cancelled on the server. |
| 904 | // Depending on the value of server_try_cancel parameter, this will test one |
| 905 | // of the following three scenarios: |
| 906 | // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before sending |
| 907 | // any messages to the client |
| 908 | // |
| 909 | // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while sending |
| 910 | // messages to the client |
| 911 | // |
| 912 | // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after sending all |
| 913 | // messages to the client (but before sending any status back to the |
| 914 | // client) |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 915 | void TestServerStreamingServerCancel( |
| 916 | ServerTryCancelRequestPhase server_try_cancel) { |
| 917 | ResetStub(); |
| 918 | |
| 919 | EchoRequest send_request; |
| 920 | EchoRequest recv_request; |
| 921 | EchoResponse send_response; |
| 922 | EchoResponse recv_response; |
| 923 | Status recv_status; |
| 924 | ClientContext cli_ctx; |
| 925 | ServerContext srv_ctx; |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 926 | ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx); |
| 927 | |
| 928 | send_request.set_message("Ping"); |
| 929 | // Initiate the 'ResponseStream' call on the client |
| 930 | std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream( |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 931 | stub_->AsyncResponseStream(&cli_ctx, send_request, cq_.get(), tag(1))); |
| 932 | Verifier(GetParam()).Expect(1, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 933 | // On the server, request to be notified of 'ResponseStream' calls and |
| 934 | // receive the call just made by the client |
| 935 | service_.RequestResponseStream(&srv_ctx, &recv_request, &srv_stream, |
| 936 | cq_.get(), cq_.get(), tag(2)); |
| 937 | Verifier(GetParam()).Expect(2, true).Verify(cq_.get()); |
| 938 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 939 | |
| 940 | bool expected_cq_result = true; |
| 941 | bool ignore_cq_result = false; |
| 942 | |
| 943 | if (server_try_cancel == CANCEL_BEFORE_PROCESSING) { |
| 944 | ServerTryCancel(&srv_ctx); |
| 945 | |
| 946 | // We know for sure that all cq results will be false from this point |
| 947 | // since the server cancelled the RPC |
| 948 | expected_cq_result = false; |
| 949 | } |
| 950 | |
| 951 | std::thread* server_try_cancel_thd = NULL; |
| 952 | if (server_try_cancel == CANCEL_DURING_PROCESSING) { |
| 953 | server_try_cancel_thd = new std::thread( |
| 954 | &AsyncEnd2endServerTryCancelTest::ServerTryCancel, this, &srv_ctx); |
| 955 | |
| 956 | // Server will cancel the RPC in a parallel thread while writing responses |
| 957 | // to the client. Since the cancellation can happen at anytime, some of |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 958 | // the cq results (i.e those until cancellation) might be true but it is |
| 959 | // non deterministic. So better to ignore the cq results |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 960 | ignore_cq_result = true; |
| 961 | } |
| 962 | |
| 963 | // Server sends three messages (tags 3, 4 and 5) |
| 964 | for (int tag_idx = 3; tag_idx <= 5; tag_idx++) { |
| 965 | send_response.set_message("Pong " + std::to_string(tag_idx)); |
| 966 | srv_stream.Write(send_response, tag(tag_idx)); |
| 967 | Verifier(GetParam()) |
| 968 | .Expect(tag_idx, expected_cq_result) |
| 969 | .Verify(cq_.get(), ignore_cq_result); |
| 970 | } |
| 971 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 972 | if (server_try_cancel_thd != NULL) { |
| 973 | server_try_cancel_thd->join(); |
| 974 | delete server_try_cancel_thd; |
| 975 | } |
| 976 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 977 | if (server_try_cancel == CANCEL_AFTER_PROCESSING) { |
| 978 | ServerTryCancel(&srv_ctx); |
| 979 | } |
| 980 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 981 | // Client attemts to read the three messages from the server |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 982 | for (int tag_idx = 6; tag_idx <= 8; tag_idx++) { |
| 983 | cli_stream->Read(&recv_response, tag(tag_idx)); |
| 984 | Verifier(GetParam()) |
| 985 | .Expect(tag_idx, expected_cq_result) |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 986 | .Verify(cq_.get(), ignore_cq_result); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 989 | // The RPC has been cancelled at this point for sure (i.e irrespective of |
| 990 | // the value of `server_try_cancel` is). So, from this point forward, we |
| 991 | // know that cq results are supposed to return false on server. |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 992 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 993 | // Server finishes the stream (but the RPC is already cancelled) |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 994 | srv_stream.Finish(Status::CANCELLED, tag(9)); |
| 995 | Verifier(GetParam()).Expect(9, false).Verify(cq_.get()); |
| 996 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 997 | // Client will see the cancellation |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 998 | cli_stream->Finish(&recv_status, tag(10)); |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 999 | Verifier(GetParam()).Expect(10, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1000 | EXPECT_FALSE(recv_status.ok()); |
| 1001 | EXPECT_EQ(::grpc::StatusCode::CANCELLED, recv_status.error_code()); |
| 1002 | } |
| 1003 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1004 | // Helper for testing bidirectinal-streaming RPCs which are cancelled on the |
| 1005 | // server. |
| 1006 | // |
| 1007 | // Depending on the value of server_try_cancel parameter, this will |
| 1008 | // test one of the following three scenarios: |
| 1009 | // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/ |
| 1010 | // writing any messages from/to the client |
| 1011 | // |
| 1012 | // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading |
| 1013 | // messages from the client |
| 1014 | // |
| 1015 | // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all |
| 1016 | // messages from the client (but before sending any status back to the |
| 1017 | // client) |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1018 | void TestBidiStreamingServerCancel( |
| 1019 | ServerTryCancelRequestPhase server_try_cancel) { |
| 1020 | ResetStub(); |
| 1021 | |
| 1022 | EchoRequest send_request; |
| 1023 | EchoRequest recv_request; |
| 1024 | EchoResponse send_response; |
| 1025 | EchoResponse recv_response; |
| 1026 | Status recv_status; |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1027 | ClientContext cli_ctx; |
| 1028 | ServerContext srv_ctx; |
| 1029 | ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 1030 | |
| 1031 | // Initiate the call from the client side |
| 1032 | std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse>> |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 1033 | cli_stream(stub_->AsyncBidiStream(&cli_ctx, cq_.get(), tag(1))); |
| 1034 | Verifier(GetParam()).Expect(1, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1035 | |
| 1036 | // On the server, request to be notified of the 'BidiStream' call and |
| 1037 | // receive the call just made by the client |
| 1038 | service_.RequestBidiStream(&srv_ctx, &srv_stream, cq_.get(), cq_.get(), |
| 1039 | tag(2)); |
| 1040 | Verifier(GetParam()).Expect(2, true).Verify(cq_.get()); |
| 1041 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1042 | // Client sends the first and the only message |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1043 | send_request.set_message("Ping"); |
| 1044 | cli_stream->Write(send_request, tag(3)); |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 1045 | Verifier(GetParam()).Expect(3, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1046 | |
| 1047 | bool expected_cq_result = true; |
| 1048 | bool ignore_cq_result = false; |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1049 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1050 | if (server_try_cancel == CANCEL_BEFORE_PROCESSING) { |
| 1051 | ServerTryCancel(&srv_ctx); |
| 1052 | |
| 1053 | // We know for sure that all cq results will be false from this point |
| 1054 | // since the server cancelled the RPC |
| 1055 | expected_cq_result = false; |
| 1056 | } |
| 1057 | |
| 1058 | std::thread* server_try_cancel_thd = NULL; |
| 1059 | if (server_try_cancel == CANCEL_DURING_PROCESSING) { |
| 1060 | server_try_cancel_thd = new std::thread( |
| 1061 | &AsyncEnd2endServerTryCancelTest::ServerTryCancel, this, &srv_ctx); |
| 1062 | |
| 1063 | // Since server is going to cancel the RPC in a parallel thread, some of |
| 1064 | // the cq results (i.e those until the cancellation) might be true. Since |
| 1065 | // that number is non-deterministic, it is better to ignore the cq results |
| 1066 | ignore_cq_result = true; |
| 1067 | } |
| 1068 | |
| 1069 | srv_stream.Read(&recv_request, tag(4)); |
| 1070 | Verifier(GetParam()) |
| 1071 | .Expect(4, expected_cq_result) |
| 1072 | .Verify(cq_.get(), ignore_cq_result); |
| 1073 | |
| 1074 | send_response.set_message("Pong"); |
| 1075 | srv_stream.Write(send_response, tag(5)); |
| 1076 | Verifier(GetParam()) |
| 1077 | .Expect(5, expected_cq_result) |
| 1078 | .Verify(cq_.get(), ignore_cq_result); |
| 1079 | |
| 1080 | cli_stream->Read(&recv_response, tag(6)); |
| 1081 | Verifier(GetParam()) |
| 1082 | .Expect(6, expected_cq_result) |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 1083 | .Verify(cq_.get(), ignore_cq_result); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1084 | |
| 1085 | // This is expected to succeed in all cases |
| 1086 | cli_stream->WritesDone(tag(7)); |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 1087 | Verifier(GetParam()).Expect(7, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1088 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1089 | // This is expected to fail in all cases i.e for all values of |
| 1090 | // server_try_cancel. This is becasue at this point, either there are no |
| 1091 | // more msgs from the client (because client called WritesDone) or the RPC |
| 1092 | // is cancelled on the server |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1093 | srv_stream.Read(&recv_request, tag(8)); |
| 1094 | Verifier(GetParam()).Expect(8, false).Verify(cq_.get()); |
| 1095 | |
| 1096 | if (server_try_cancel_thd != NULL) { |
| 1097 | server_try_cancel_thd->join(); |
| 1098 | delete server_try_cancel_thd; |
| 1099 | } |
| 1100 | |
| 1101 | if (server_try_cancel == CANCEL_AFTER_PROCESSING) { |
| 1102 | ServerTryCancel(&srv_ctx); |
| 1103 | } |
| 1104 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 1105 | // The RPC has been cancelled at this point for sure (i.e irrespective of |
| 1106 | // the value of `server_try_cancel` is). So, from this point forward, we |
| 1107 | // know that cq results are supposed to return false on server. |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1108 | |
| 1109 | srv_stream.Finish(Status::CANCELLED, tag(9)); |
| 1110 | Verifier(GetParam()).Expect(9, false).Verify(cq_.get()); |
| 1111 | |
| 1112 | cli_stream->Finish(&recv_status, tag(10)); |
Sree Kuchibhotla | 4fb5908 | 2016-01-29 11:16:24 -0800 | [diff] [blame] | 1113 | Verifier(GetParam()).Expect(10, true).Verify(cq_.get()); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1114 | EXPECT_FALSE(recv_status.ok()); |
| 1115 | EXPECT_EQ(grpc::StatusCode::CANCELLED, recv_status.error_code()); |
| 1116 | } |
| 1117 | }; |
| 1118 | |
| 1119 | TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelBefore) { |
| 1120 | TestClientStreamingServerCancel(CANCEL_BEFORE_PROCESSING); |
| 1121 | } |
| 1122 | |
| 1123 | TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelDuring) { |
| 1124 | TestClientStreamingServerCancel(CANCEL_DURING_PROCESSING); |
| 1125 | } |
| 1126 | |
| 1127 | TEST_P(AsyncEnd2endServerTryCancelTest, ClientStreamingServerTryCancelAfter) { |
| 1128 | TestClientStreamingServerCancel(CANCEL_AFTER_PROCESSING); |
| 1129 | } |
| 1130 | |
| 1131 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelBefore) { |
| 1132 | TestServerStreamingServerCancel(CANCEL_BEFORE_PROCESSING); |
| 1133 | } |
| 1134 | |
| 1135 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelDuring) { |
| 1136 | TestServerStreamingServerCancel(CANCEL_DURING_PROCESSING); |
| 1137 | } |
| 1138 | |
| 1139 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerStreamingServerTryCancelAfter) { |
| 1140 | TestServerStreamingServerCancel(CANCEL_AFTER_PROCESSING); |
| 1141 | } |
| 1142 | |
| 1143 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelBefore) { |
| 1144 | TestBidiStreamingServerCancel(CANCEL_BEFORE_PROCESSING); |
| 1145 | } |
| 1146 | |
| 1147 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelDuring) { |
| 1148 | TestBidiStreamingServerCancel(CANCEL_DURING_PROCESSING); |
| 1149 | } |
| 1150 | |
| 1151 | TEST_P(AsyncEnd2endServerTryCancelTest, ServerBidiStreamingTryCancelAfter) { |
| 1152 | TestBidiStreamingServerCancel(CANCEL_AFTER_PROCESSING); |
| 1153 | } |
| 1154 | |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 1155 | INSTANTIATE_TEST_CASE_P(AsyncEnd2end, AsyncEnd2endTest, |
| 1156 | ::testing::Values(false, true)); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1157 | INSTANTIATE_TEST_CASE_P(AsyncEnd2endServerTryCancel, |
| 1158 | AsyncEnd2endServerTryCancelTest, |
| 1159 | ::testing::Values(false)); |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 1160 | |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 1161 | } // namespace |
| 1162 | } // namespace testing |
| 1163 | } // namespace grpc |
| 1164 | |
| 1165 | int main(int argc, char** argv) { |
| 1166 | grpc_test_init(argc, argv); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 1167 | ::testing::InitGoogleTest(&argc, argv); |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 1168 | return RUN_ALL_TESTS(); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 1169 | } |