Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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 <thread> |
| 36 | |
| 37 | #include "test/core/util/test_config.h" |
| 38 | #include "test/cpp/util/echo_duplicate.pb.h" |
| 39 | #include "test/cpp/util/echo.pb.h" |
| 40 | #include "src/cpp/util/time.h" |
| 41 | #include <grpc++/channel_arguments.h> |
| 42 | #include <grpc++/channel_interface.h> |
| 43 | #include <grpc++/client_context.h> |
| 44 | #include <grpc++/create_channel.h> |
| 45 | #include <grpc++/credentials.h> |
| 46 | #include <grpc++/server.h> |
| 47 | #include <grpc++/server_builder.h> |
| 48 | #include <grpc++/server_context.h> |
| 49 | #include <grpc++/status.h> |
| 50 | #include <grpc++/stream.h> |
| 51 | #include "test/core/util/port.h" |
| 52 | #include <gtest/gtest.h> |
| 53 | |
| 54 | #include <grpc/grpc.h> |
| 55 | #include <grpc/support/thd.h> |
| 56 | #include <grpc/support/time.h> |
| 57 | |
| 58 | using grpc::cpp::test::util::EchoRequest; |
| 59 | using grpc::cpp::test::util::EchoResponse; |
| 60 | using std::chrono::system_clock; |
| 61 | |
| 62 | namespace grpc { |
| 63 | namespace testing { |
| 64 | |
| 65 | namespace { |
| 66 | |
| 67 | class End2endTest : public ::testing::Test { |
| 68 | protected: |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame^] | 69 | End2endTest() : service_(&srv_cq_) {} |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 70 | |
| 71 | void SetUp() override { |
| 72 | int port = grpc_pick_unused_port_or_die(); |
| 73 | server_address_ << "localhost:" << port; |
| 74 | // Setup server |
| 75 | ServerBuilder builder; |
| 76 | builder.AddPort(server_address_.str()); |
| 77 | builder.RegisterAsyncService(&service_); |
| 78 | server_ = builder.BuildAndStart(); |
| 79 | } |
| 80 | |
| 81 | void TearDown() override { server_->Shutdown(); } |
| 82 | |
| 83 | void ResetStub() { |
| 84 | std::shared_ptr<ChannelInterface> channel = |
| 85 | CreateChannel(server_address_.str(), ChannelArguments()); |
| 86 | stub_.reset(grpc::cpp::test::util::TestService::NewStub(channel)); |
| 87 | } |
| 88 | |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame^] | 89 | CompletionQueue cli_cq_; |
| 90 | CompletionQueue srv_cq_; |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 91 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_; |
| 92 | std::unique_ptr<Server> server_; |
| 93 | grpc::cpp::test::util::TestService::AsyncService service_; |
| 94 | std::ostringstream server_address_; |
| 95 | }; |
| 96 | |
| 97 | void* tag(int i) { |
| 98 | return (void*)(gpr_intptr)i; |
| 99 | } |
| 100 | |
| 101 | TEST_F(End2endTest, SimpleRpc) { |
| 102 | ResetStub(); |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame^] | 103 | |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 104 | EchoRequest send_request; |
| 105 | EchoRequest recv_request; |
| 106 | EchoResponse send_response; |
| 107 | EchoResponse recv_response; |
| 108 | Status recv_status; |
| 109 | ClientContext cli_ctx; |
| 110 | ServerContext srv_ctx; |
| 111 | grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx); |
| 112 | |
| 113 | send_request.set_message("Hello"); |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame^] | 114 | stub_->Echo( |
| 115 | &cli_ctx, send_request, &recv_response, &recv_status, &cli_cq_, tag(1)); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 116 | |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame^] | 117 | service_.RequestEcho( |
| 118 | &srv_ctx, &recv_request, &response_writer, &srv_cq_, tag(2)); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 119 | |
| 120 | void *got_tag; |
| 121 | bool ok; |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame^] | 122 | EXPECT_TRUE(srv_cq_.Next(&got_tag, &ok)); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 123 | EXPECT_TRUE(ok); |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame^] | 124 | EXPECT_EQ(tag(2), got_tag); |
| 125 | EXPECT_EQ(send_request.message(), recv_request.message()); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 126 | |
| 127 | send_response.set_message(recv_request.message()); |
| 128 | response_writer.Finish(send_response, Status::OK, tag(3)); |
| 129 | |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame^] | 130 | EXPECT_TRUE(srv_cq_.Next(&got_tag, &ok)); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 131 | EXPECT_TRUE(ok); |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame^] | 132 | EXPECT_EQ(tag(3), got_tag); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 133 | |
Yang Gao | bb84a30 | 2015-02-12 23:30:12 -0800 | [diff] [blame^] | 134 | EXPECT_TRUE(cli_cq_.Next(&got_tag, &ok)); |
| 135 | EXPECT_TRUE(ok); |
| 136 | EXPECT_EQ(tag(1), got_tag); |
| 137 | |
| 138 | EXPECT_EQ(send_response.message(), recv_response.message()); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 139 | EXPECT_TRUE(recv_status.IsOk()); |
| 140 | } |
| 141 | |
| 142 | } // namespace |
| 143 | } // namespace testing |
| 144 | } // namespace grpc |
| 145 | |
| 146 | int main(int argc, char** argv) { |
| 147 | grpc_test_init(argc, argv); |
| 148 | grpc_init(); |
| 149 | ::testing::InitGoogleTest(&argc, argv); |
| 150 | int result = RUN_ALL_TESTS(); |
| 151 | grpc_shutdown(); |
| 152 | google::protobuf::ShutdownProtobufLibrary(); |
| 153 | return result; |
| 154 | } |