Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 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 | |
| 34 | #include <chrono> |
| 35 | #include <memory> |
| 36 | |
| 37 | #include "test/core/util/test_config.h" |
| 38 | #include "test/cpp/util/echo.pb.h" |
| 39 | #include "src/cpp/util/time.h" |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame^] | 40 | #include <grpc++/async_generic_service.h> |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 41 | #include <grpc++/async_unary_call.h> |
| 42 | #include <grpc++/byte_buffer.h> |
| 43 | #include <grpc++/channel_arguments.h> |
| 44 | #include <grpc++/channel_interface.h> |
| 45 | #include <grpc++/client_context.h> |
| 46 | #include <grpc++/create_channel.h> |
| 47 | #include <grpc++/credentials.h> |
| 48 | #include <grpc++/server.h> |
| 49 | #include <grpc++/server_builder.h> |
| 50 | #include <grpc++/server_context.h> |
| 51 | #include <grpc++/server_credentials.h> |
| 52 | #include <grpc++/slice.h> |
| 53 | #include <grpc++/status.h> |
| 54 | #include <grpc++/stream.h> |
| 55 | #include "test/core/util/port.h" |
| 56 | #include <gtest/gtest.h> |
| 57 | |
| 58 | #include <grpc/grpc.h> |
| 59 | #include <grpc/support/thd.h> |
| 60 | #include <grpc/support/time.h> |
| 61 | |
| 62 | using grpc::cpp::test::util::EchoRequest; |
| 63 | using grpc::cpp::test::util::EchoResponse; |
| 64 | using std::chrono::system_clock; |
| 65 | |
| 66 | namespace grpc { |
| 67 | namespace testing { |
| 68 | namespace { |
| 69 | |
| 70 | void* tag(int i) { return (void*)(gpr_intptr)i; } |
| 71 | |
| 72 | void verify_ok(CompletionQueue* cq, int i, bool expect_ok) { |
| 73 | bool ok; |
| 74 | void* got_tag; |
| 75 | EXPECT_TRUE(cq->Next(&got_tag, &ok)); |
| 76 | EXPECT_EQ(expect_ok, ok); |
| 77 | EXPECT_EQ(tag(i), got_tag); |
| 78 | } |
| 79 | |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 80 | class GenericEnd2endTest : public ::testing::Test { |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 81 | protected: |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 82 | GenericEnd2endTest() {} |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 83 | |
| 84 | void SetUp() GRPC_OVERRIDE { |
| 85 | int port = grpc_pick_unused_port_or_die(); |
| 86 | server_address_ << "localhost:" << port; |
| 87 | // Setup server |
| 88 | ServerBuilder builder; |
| 89 | builder.AddPort(server_address_.str(), InsecureServerCredentials()); |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame^] | 90 | builder.RegisterAsyncGenericService(&generic_service_); |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 91 | server_ = builder.BuildAndStart(); |
| 92 | } |
| 93 | |
| 94 | void TearDown() GRPC_OVERRIDE { |
| 95 | server_->Shutdown(); |
| 96 | void* ignored_tag; |
| 97 | bool ignored_ok; |
| 98 | cli_cq_.Shutdown(); |
| 99 | srv_cq_.Shutdown(); |
| 100 | while (cli_cq_.Next(&ignored_tag, &ignored_ok)) |
| 101 | ; |
| 102 | while (srv_cq_.Next(&ignored_tag, &ignored_ok)) |
| 103 | ; |
| 104 | } |
| 105 | |
| 106 | void ResetStub() { |
| 107 | std::shared_ptr<ChannelInterface> channel = |
| 108 | CreateChannel(server_address_.str(), InsecureCredentials(), ChannelArguments()); |
| 109 | stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel)); |
| 110 | } |
| 111 | |
| 112 | void server_ok(int i) { verify_ok(&srv_cq_, i, true); } |
| 113 | void client_ok(int i) { verify_ok(&cli_cq_, i, true); } |
| 114 | void server_fail(int i) { verify_ok(&srv_cq_, i, false); } |
| 115 | void client_fail(int i) { verify_ok(&cli_cq_, i, false); } |
| 116 | |
| 117 | void SendRpc(int num_rpcs) { |
| 118 | for (int i = 0; i < num_rpcs; i++) { |
| 119 | EchoRequest send_request; |
| 120 | EchoRequest recv_request; |
| 121 | EchoResponse send_response; |
| 122 | EchoResponse recv_response; |
| 123 | Status recv_status; |
| 124 | |
| 125 | ClientContext cli_ctx; |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 126 | GenericServerContext srv_ctx; |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame^] | 127 | GenericServerAsyncReaderWriter stream(&srv_ctx); |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 128 | |
| 129 | send_request.set_message("Hello"); |
| 130 | std::unique_ptr<ClientAsyncResponseReader<EchoResponse> > response_reader( |
| 131 | stub_->AsyncEcho(&cli_ctx, send_request, &cli_cq_, tag(1))); |
| 132 | client_ok(1); |
| 133 | |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 134 | generic_service_.RequestCall(&srv_ctx, &stream, &srv_cq_, tag(2)); |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 135 | |
| 136 | verify_ok(server_->cq(), 2, true); |
| 137 | EXPECT_EQ(server_address_.str(), srv_ctx.host()); |
| 138 | EXPECT_EQ("/grpc.cpp.test.util.TestService/Echo", srv_ctx.method()); |
| 139 | ByteBuffer recv_buffer; |
| 140 | stream.Read(&recv_buffer, tag(3)); |
| 141 | server_ok(3); |
| 142 | std::vector<Slice> slices; |
| 143 | recv_buffer.Dump(&slices); |
| 144 | EXPECT_EQ(1, slices.size()); // FIXME(yangg) handle real vector |
| 145 | grpc::string buf(reinterpret_cast<const char*>(slices[0].begin()), |
| 146 | slices[0].size()); |
| 147 | EXPECT_TRUE(recv_request.ParseFromString(buf)); |
| 148 | EXPECT_EQ(send_request.message(), recv_request.message()); |
| 149 | |
| 150 | send_response.set_message(recv_request.message()); |
| 151 | buf.clear(); |
| 152 | send_response.SerializeToString(&buf); |
| 153 | gpr_slice s = gpr_slice_from_copied_string(buf.c_str()); |
| 154 | Slice slice(s, Slice::STEAL_REF); |
| 155 | ByteBuffer send_buffer(&slice, 1); |
| 156 | stream.Write(send_buffer, tag(4)); |
| 157 | server_ok(4); |
| 158 | |
| 159 | stream.Finish(Status::OK, tag(5)); |
| 160 | server_ok(5); |
| 161 | |
| 162 | response_reader->Finish(&recv_response, &recv_status, tag(4)); |
| 163 | client_ok(4); |
| 164 | |
| 165 | EXPECT_EQ(send_response.message(), recv_response.message()); |
| 166 | EXPECT_TRUE(recv_status.IsOk()); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | CompletionQueue cli_cq_; |
| 171 | CompletionQueue srv_cq_; |
| 172 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_; |
| 173 | std::unique_ptr<Server> server_; |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame^] | 174 | AsyncGenericService generic_service_; |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 175 | std::ostringstream server_address_; |
| 176 | }; |
| 177 | |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 178 | TEST_F(GenericEnd2endTest, SimpleRpc) { |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 179 | ResetStub(); |
| 180 | SendRpc(1); |
| 181 | } |
| 182 | |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 183 | TEST_F(GenericEnd2endTest, SequentialRpcs) { |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 184 | ResetStub(); |
| 185 | SendRpc(10); |
| 186 | } |
| 187 | |
| 188 | // // Two pings and a final pong. |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 189 | // TEST_F(GenericEnd2endTest, SimpleClientStreaming) { |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 190 | // ResetStub(); |
| 191 | // |
| 192 | // EchoRequest send_request; |
| 193 | // EchoRequest recv_request; |
| 194 | // EchoResponse send_response; |
| 195 | // EchoResponse recv_response; |
| 196 | // Status recv_status; |
| 197 | // ClientContext cli_ctx; |
| 198 | // ServerContext srv_ctx; |
| 199 | // ServerAsyncReader<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 200 | // |
| 201 | // send_request.set_message("Hello"); |
| 202 | // std::unique_ptr<ClientAsyncWriter<EchoRequest> > cli_stream( |
| 203 | // stub_->AsyncRequestStream(&cli_ctx, &recv_response, &cli_cq_, tag(1))); |
| 204 | // |
| 205 | // service_.RequestRequestStream(&srv_ctx, &srv_stream, &srv_cq_, tag(2)); |
| 206 | // |
| 207 | // server_ok(2); |
| 208 | // client_ok(1); |
| 209 | // |
| 210 | // cli_stream->Write(send_request, tag(3)); |
| 211 | // client_ok(3); |
| 212 | // |
| 213 | // srv_stream.Read(&recv_request, tag(4)); |
| 214 | // server_ok(4); |
| 215 | // EXPECT_EQ(send_request.message(), recv_request.message()); |
| 216 | // |
| 217 | // cli_stream->Write(send_request, tag(5)); |
| 218 | // client_ok(5); |
| 219 | // |
| 220 | // srv_stream.Read(&recv_request, tag(6)); |
| 221 | // server_ok(6); |
| 222 | // |
| 223 | // EXPECT_EQ(send_request.message(), recv_request.message()); |
| 224 | // cli_stream->WritesDone(tag(7)); |
| 225 | // client_ok(7); |
| 226 | // |
| 227 | // srv_stream.Read(&recv_request, tag(8)); |
| 228 | // server_fail(8); |
| 229 | // |
| 230 | // send_response.set_message(recv_request.message()); |
| 231 | // srv_stream.Finish(send_response, Status::OK, tag(9)); |
| 232 | // server_ok(9); |
| 233 | // |
| 234 | // cli_stream->Finish(&recv_status, tag(10)); |
| 235 | // client_ok(10); |
| 236 | // |
| 237 | // EXPECT_EQ(send_response.message(), recv_response.message()); |
| 238 | // EXPECT_TRUE(recv_status.IsOk()); |
| 239 | // } |
| 240 | // |
| 241 | // // One ping, two pongs. |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 242 | // TEST_F(GenericEnd2endTest, SimpleServerStreaming) { |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 243 | // ResetStub(); |
| 244 | // |
| 245 | // EchoRequest send_request; |
| 246 | // EchoRequest recv_request; |
| 247 | // EchoResponse send_response; |
| 248 | // EchoResponse recv_response; |
| 249 | // Status recv_status; |
| 250 | // ClientContext cli_ctx; |
| 251 | // ServerContext srv_ctx; |
| 252 | // ServerAsyncWriter<EchoResponse> srv_stream(&srv_ctx); |
| 253 | // |
| 254 | // send_request.set_message("Hello"); |
| 255 | // std::unique_ptr<ClientAsyncReader<EchoResponse> > cli_stream( |
| 256 | // stub_->AsyncResponseStream(&cli_ctx, send_request, &cli_cq_, tag(1))); |
| 257 | // |
| 258 | // service_.RequestResponseStream(&srv_ctx, &recv_request, &srv_stream, &srv_cq_, |
| 259 | // tag(2)); |
| 260 | // |
| 261 | // server_ok(2); |
| 262 | // client_ok(1); |
| 263 | // EXPECT_EQ(send_request.message(), recv_request.message()); |
| 264 | // |
| 265 | // send_response.set_message(recv_request.message()); |
| 266 | // srv_stream.Write(send_response, tag(3)); |
| 267 | // server_ok(3); |
| 268 | // |
| 269 | // cli_stream->Read(&recv_response, tag(4)); |
| 270 | // client_ok(4); |
| 271 | // EXPECT_EQ(send_response.message(), recv_response.message()); |
| 272 | // |
| 273 | // srv_stream.Write(send_response, tag(5)); |
| 274 | // server_ok(5); |
| 275 | // |
| 276 | // cli_stream->Read(&recv_response, tag(6)); |
| 277 | // client_ok(6); |
| 278 | // EXPECT_EQ(send_response.message(), recv_response.message()); |
| 279 | // |
| 280 | // srv_stream.Finish(Status::OK, tag(7)); |
| 281 | // server_ok(7); |
| 282 | // |
| 283 | // cli_stream->Read(&recv_response, tag(8)); |
| 284 | // client_fail(8); |
| 285 | // |
| 286 | // cli_stream->Finish(&recv_status, tag(9)); |
| 287 | // client_ok(9); |
| 288 | // |
| 289 | // EXPECT_TRUE(recv_status.IsOk()); |
| 290 | // } |
| 291 | // |
| 292 | // // One ping, one pong. |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 293 | // TEST_F(GenericEnd2endTest, SimpleBidiStreaming) { |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 294 | // ResetStub(); |
| 295 | // |
| 296 | // EchoRequest send_request; |
| 297 | // EchoRequest recv_request; |
| 298 | // EchoResponse send_response; |
| 299 | // EchoResponse recv_response; |
| 300 | // Status recv_status; |
| 301 | // ClientContext cli_ctx; |
| 302 | // ServerContext srv_ctx; |
| 303 | // ServerAsyncReaderWriter<EchoResponse, EchoRequest> srv_stream(&srv_ctx); |
| 304 | // |
| 305 | // send_request.set_message("Hello"); |
| 306 | // std::unique_ptr<ClientAsyncReaderWriter<EchoRequest, EchoResponse> > |
| 307 | // cli_stream(stub_->AsyncBidiStream(&cli_ctx, &cli_cq_, tag(1))); |
| 308 | // |
| 309 | // service_.RequestBidiStream(&srv_ctx, &srv_stream, &srv_cq_, tag(2)); |
| 310 | // |
| 311 | // server_ok(2); |
| 312 | // client_ok(1); |
| 313 | // |
| 314 | // cli_stream->Write(send_request, tag(3)); |
| 315 | // client_ok(3); |
| 316 | // |
| 317 | // srv_stream.Read(&recv_request, tag(4)); |
| 318 | // server_ok(4); |
| 319 | // EXPECT_EQ(send_request.message(), recv_request.message()); |
| 320 | // |
| 321 | // send_response.set_message(recv_request.message()); |
| 322 | // srv_stream.Write(send_response, tag(5)); |
| 323 | // server_ok(5); |
| 324 | // |
| 325 | // cli_stream->Read(&recv_response, tag(6)); |
| 326 | // client_ok(6); |
| 327 | // EXPECT_EQ(send_response.message(), recv_response.message()); |
| 328 | // |
| 329 | // cli_stream->WritesDone(tag(7)); |
| 330 | // client_ok(7); |
| 331 | // |
| 332 | // srv_stream.Read(&recv_request, tag(8)); |
| 333 | // server_fail(8); |
| 334 | // |
| 335 | // srv_stream.Finish(Status::OK, tag(9)); |
| 336 | // server_ok(9); |
| 337 | // |
| 338 | // cli_stream->Finish(&recv_status, tag(10)); |
| 339 | // client_ok(10); |
| 340 | // |
| 341 | // EXPECT_TRUE(recv_status.IsOk()); |
| 342 | // } |
| 343 | // |
| 344 | // // Metadata tests |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 345 | // TEST_F(GenericEnd2endTest, ClientInitialMetadataRpc) { |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 346 | // ResetStub(); |
| 347 | // |
| 348 | // EchoRequest send_request; |
| 349 | // EchoRequest recv_request; |
| 350 | // EchoResponse send_response; |
| 351 | // EchoResponse recv_response; |
| 352 | // Status recv_status; |
| 353 | // |
| 354 | // ClientContext cli_ctx; |
| 355 | // ServerContext srv_ctx; |
| 356 | // grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 357 | // |
| 358 | // send_request.set_message("Hello"); |
| 359 | // std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
| 360 | // std::pair<grpc::string, grpc::string> meta2("key2", "val2"); |
| 361 | // cli_ctx.AddMetadata(meta1.first, meta1.second); |
| 362 | // cli_ctx.AddMetadata(meta2.first, meta2.second); |
| 363 | // |
| 364 | // std::unique_ptr<ClientAsyncResponseReader<EchoResponse> > response_reader( |
| 365 | // stub_->AsyncEcho(&cli_ctx, send_request, &cli_cq_, tag(1))); |
| 366 | // |
| 367 | // service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, &srv_cq_, |
| 368 | // tag(2)); |
| 369 | // server_ok(2); |
| 370 | // EXPECT_EQ(send_request.message(), recv_request.message()); |
| 371 | // auto client_initial_metadata = srv_ctx.client_metadata(); |
| 372 | // EXPECT_EQ(meta1.second, client_initial_metadata.find(meta1.first)->second); |
| 373 | // EXPECT_EQ(meta2.second, client_initial_metadata.find(meta2.first)->second); |
| 374 | // EXPECT_EQ(static_cast<size_t>(2), client_initial_metadata.size()); |
| 375 | // client_ok(1); |
| 376 | // |
| 377 | // send_response.set_message(recv_request.message()); |
| 378 | // response_writer.Finish(send_response, Status::OK, tag(3)); |
| 379 | // |
| 380 | // server_ok(3); |
| 381 | // |
| 382 | // response_reader->Finish(&recv_response, &recv_status, tag(4)); |
| 383 | // client_ok(4); |
| 384 | // |
| 385 | // EXPECT_EQ(send_response.message(), recv_response.message()); |
| 386 | // EXPECT_TRUE(recv_status.IsOk()); |
| 387 | // } |
| 388 | // |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 389 | // TEST_F(GenericEnd2endTest, ServerInitialMetadataRpc) { |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 390 | // ResetStub(); |
| 391 | // |
| 392 | // EchoRequest send_request; |
| 393 | // EchoRequest recv_request; |
| 394 | // EchoResponse send_response; |
| 395 | // EchoResponse recv_response; |
| 396 | // Status recv_status; |
| 397 | // |
| 398 | // ClientContext cli_ctx; |
| 399 | // ServerContext srv_ctx; |
| 400 | // grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 401 | // |
| 402 | // send_request.set_message("Hello"); |
| 403 | // std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
| 404 | // std::pair<grpc::string, grpc::string> meta2("key2", "val2"); |
| 405 | // |
| 406 | // std::unique_ptr<ClientAsyncResponseReader<EchoResponse> > response_reader( |
| 407 | // stub_->AsyncEcho(&cli_ctx, send_request, &cli_cq_, tag(1))); |
| 408 | // |
| 409 | // service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, &srv_cq_, |
| 410 | // tag(2)); |
| 411 | // server_ok(2); |
| 412 | // EXPECT_EQ(send_request.message(), recv_request.message()); |
| 413 | // srv_ctx.AddInitialMetadata(meta1.first, meta1.second); |
| 414 | // srv_ctx.AddInitialMetadata(meta2.first, meta2.second); |
| 415 | // client_ok(1); |
| 416 | // response_writer.SendInitialMetadata(tag(3)); |
| 417 | // server_ok(3); |
| 418 | // |
| 419 | // response_reader->ReadInitialMetadata(tag(4)); |
| 420 | // client_ok(4); |
| 421 | // auto server_initial_metadata = cli_ctx.GetServerInitialMetadata(); |
| 422 | // EXPECT_EQ(meta1.second, server_initial_metadata.find(meta1.first)->second); |
| 423 | // EXPECT_EQ(meta2.second, server_initial_metadata.find(meta2.first)->second); |
| 424 | // EXPECT_EQ(static_cast<size_t>(2), server_initial_metadata.size()); |
| 425 | // |
| 426 | // send_response.set_message(recv_request.message()); |
| 427 | // response_writer.Finish(send_response, Status::OK, tag(5)); |
| 428 | // server_ok(5); |
| 429 | // |
| 430 | // response_reader->Finish(&recv_response, &recv_status, tag(6)); |
| 431 | // client_ok(6); |
| 432 | // |
| 433 | // EXPECT_EQ(send_response.message(), recv_response.message()); |
| 434 | // EXPECT_TRUE(recv_status.IsOk()); |
| 435 | // } |
| 436 | // |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 437 | // TEST_F(GenericEnd2endTest, ServerTrailingMetadataRpc) { |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 438 | // ResetStub(); |
| 439 | // |
| 440 | // EchoRequest send_request; |
| 441 | // EchoRequest recv_request; |
| 442 | // EchoResponse send_response; |
| 443 | // EchoResponse recv_response; |
| 444 | // Status recv_status; |
| 445 | // |
| 446 | // ClientContext cli_ctx; |
| 447 | // ServerContext srv_ctx; |
| 448 | // grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 449 | // |
| 450 | // send_request.set_message("Hello"); |
| 451 | // std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
| 452 | // std::pair<grpc::string, grpc::string> meta2("key2", "val2"); |
| 453 | // |
| 454 | // std::unique_ptr<ClientAsyncResponseReader<EchoResponse> > response_reader( |
| 455 | // stub_->AsyncEcho(&cli_ctx, send_request, &cli_cq_, tag(1))); |
| 456 | // |
| 457 | // service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, &srv_cq_, |
| 458 | // tag(2)); |
| 459 | // server_ok(2); |
| 460 | // EXPECT_EQ(send_request.message(), recv_request.message()); |
| 461 | // response_writer.SendInitialMetadata(tag(3)); |
| 462 | // server_ok(3); |
| 463 | // client_ok(1); |
| 464 | // |
| 465 | // send_response.set_message(recv_request.message()); |
| 466 | // srv_ctx.AddTrailingMetadata(meta1.first, meta1.second); |
| 467 | // srv_ctx.AddTrailingMetadata(meta2.first, meta2.second); |
| 468 | // response_writer.Finish(send_response, Status::OK, tag(4)); |
| 469 | // |
| 470 | // server_ok(4); |
| 471 | // |
| 472 | // response_reader->Finish(&recv_response, &recv_status, tag(5)); |
| 473 | // client_ok(5); |
| 474 | // EXPECT_EQ(send_response.message(), recv_response.message()); |
| 475 | // EXPECT_TRUE(recv_status.IsOk()); |
| 476 | // auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata(); |
| 477 | // EXPECT_EQ(meta1.second, server_trailing_metadata.find(meta1.first)->second); |
| 478 | // EXPECT_EQ(meta2.second, server_trailing_metadata.find(meta2.first)->second); |
| 479 | // EXPECT_EQ(static_cast<size_t>(2), server_trailing_metadata.size()); |
| 480 | // } |
| 481 | // |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 482 | // TEST_F(GenericEnd2endTest, MetadataRpc) { |
Yang Gao | 0cb6c25 | 2015-03-11 00:30:57 -0700 | [diff] [blame] | 483 | // ResetStub(); |
| 484 | // |
| 485 | // EchoRequest send_request; |
| 486 | // EchoRequest recv_request; |
| 487 | // EchoResponse send_response; |
| 488 | // EchoResponse recv_response; |
| 489 | // Status recv_status; |
| 490 | // |
| 491 | // ClientContext cli_ctx; |
| 492 | // ServerContext srv_ctx; |
| 493 | // grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 494 | // |
| 495 | // send_request.set_message("Hello"); |
| 496 | // std::pair<grpc::string, grpc::string> meta1("key1", "val1"); |
| 497 | // std::pair<grpc::string, grpc::string> meta2( |
| 498 | // "key2-bin", {"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc", 13}); |
| 499 | // std::pair<grpc::string, grpc::string> meta3("key3", "val3"); |
| 500 | // std::pair<grpc::string, grpc::string> meta6( |
| 501 | // "key4-bin", |
| 502 | // {"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d", 14}); |
| 503 | // std::pair<grpc::string, grpc::string> meta5("key5", "val5"); |
| 504 | // std::pair<grpc::string, grpc::string> meta4( |
| 505 | // "key6-bin", |
| 506 | // {"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee", 15}); |
| 507 | // |
| 508 | // cli_ctx.AddMetadata(meta1.first, meta1.second); |
| 509 | // cli_ctx.AddMetadata(meta2.first, meta2.second); |
| 510 | // |
| 511 | // std::unique_ptr<ClientAsyncResponseReader<EchoResponse> > response_reader( |
| 512 | // stub_->AsyncEcho(&cli_ctx, send_request, &cli_cq_, tag(1))); |
| 513 | // |
| 514 | // service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, &srv_cq_, |
| 515 | // tag(2)); |
| 516 | // server_ok(2); |
| 517 | // EXPECT_EQ(send_request.message(), recv_request.message()); |
| 518 | // auto client_initial_metadata = srv_ctx.client_metadata(); |
| 519 | // EXPECT_EQ(meta1.second, client_initial_metadata.find(meta1.first)->second); |
| 520 | // EXPECT_EQ(meta2.second, client_initial_metadata.find(meta2.first)->second); |
| 521 | // EXPECT_EQ(static_cast<size_t>(2), client_initial_metadata.size()); |
| 522 | // client_ok(1); |
| 523 | // |
| 524 | // srv_ctx.AddInitialMetadata(meta3.first, meta3.second); |
| 525 | // srv_ctx.AddInitialMetadata(meta4.first, meta4.second); |
| 526 | // response_writer.SendInitialMetadata(tag(3)); |
| 527 | // server_ok(3); |
| 528 | // response_reader->ReadInitialMetadata(tag(4)); |
| 529 | // client_ok(4); |
| 530 | // auto server_initial_metadata = cli_ctx.GetServerInitialMetadata(); |
| 531 | // EXPECT_EQ(meta3.second, server_initial_metadata.find(meta3.first)->second); |
| 532 | // EXPECT_EQ(meta4.second, server_initial_metadata.find(meta4.first)->second); |
| 533 | // EXPECT_EQ(static_cast<size_t>(2), server_initial_metadata.size()); |
| 534 | // |
| 535 | // send_response.set_message(recv_request.message()); |
| 536 | // srv_ctx.AddTrailingMetadata(meta5.first, meta5.second); |
| 537 | // srv_ctx.AddTrailingMetadata(meta6.first, meta6.second); |
| 538 | // response_writer.Finish(send_response, Status::OK, tag(5)); |
| 539 | // |
| 540 | // server_ok(5); |
| 541 | // |
| 542 | // response_reader->Finish(&recv_response, &recv_status, tag(6)); |
| 543 | // client_ok(6); |
| 544 | // EXPECT_EQ(send_response.message(), recv_response.message()); |
| 545 | // EXPECT_TRUE(recv_status.IsOk()); |
| 546 | // auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata(); |
| 547 | // EXPECT_EQ(meta5.second, server_trailing_metadata.find(meta5.first)->second); |
| 548 | // EXPECT_EQ(meta6.second, server_trailing_metadata.find(meta6.first)->second); |
| 549 | // EXPECT_EQ(static_cast<size_t>(2), server_trailing_metadata.size()); |
| 550 | // } |
| 551 | } // namespace |
| 552 | } // namespace testing |
| 553 | } // namespace grpc |
| 554 | |
| 555 | int main(int argc, char** argv) { |
| 556 | grpc_test_init(argc, argv); |
| 557 | grpc_init(); |
| 558 | ::testing::InitGoogleTest(&argc, argv); |
| 559 | int result = RUN_ALL_TESTS(); |
| 560 | grpc_shutdown(); |
| 561 | google::protobuf::ShutdownProtobufLibrary(); |
| 562 | return result; |
| 563 | } |