OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 <memory> |
| 35 |
| 36 #include <grpc++/channel.h> |
| 37 #include <grpc++/client_context.h> |
| 38 #include <grpc++/create_channel.h> |
| 39 #include <grpc++/generic/async_generic_service.h> |
| 40 #include <grpc++/generic/generic_stub.h> |
| 41 #include <grpc++/impl/proto_utils.h> |
| 42 #include <grpc++/server.h> |
| 43 #include <grpc++/server_builder.h> |
| 44 #include <grpc++/server_context.h> |
| 45 #include <grpc++/support/slice.h> |
| 46 #include <grpc/grpc.h> |
| 47 #include <grpc/support/thd.h> |
| 48 #include <grpc/support/time.h> |
| 49 #include <gtest/gtest.h> |
| 50 |
| 51 #include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 52 #include "test/core/util/port.h" |
| 53 #include "test/core/util/test_config.h" |
| 54 #include "test/cpp/util/byte_buffer_proto_helper.h" |
| 55 |
| 56 using grpc::testing::EchoRequest; |
| 57 using grpc::testing::EchoResponse; |
| 58 using std::chrono::system_clock; |
| 59 |
| 60 namespace grpc { |
| 61 namespace testing { |
| 62 namespace { |
| 63 |
| 64 void* tag(int i) { return (void*)(intptr_t)i; } |
| 65 |
| 66 void verify_ok(CompletionQueue* cq, int i, bool expect_ok) { |
| 67 bool ok; |
| 68 void* got_tag; |
| 69 EXPECT_TRUE(cq->Next(&got_tag, &ok)); |
| 70 EXPECT_EQ(expect_ok, ok); |
| 71 EXPECT_EQ(tag(i), got_tag); |
| 72 } |
| 73 |
| 74 class GenericEnd2endTest : public ::testing::Test { |
| 75 protected: |
| 76 GenericEnd2endTest() : server_host_("localhost") {} |
| 77 |
| 78 void SetUp() GRPC_OVERRIDE { |
| 79 int port = grpc_pick_unused_port_or_die(); |
| 80 server_address_ << server_host_ << ":" << port; |
| 81 // Setup server |
| 82 ServerBuilder builder; |
| 83 builder.AddListeningPort(server_address_.str(), |
| 84 InsecureServerCredentials()); |
| 85 builder.RegisterAsyncGenericService(&generic_service_); |
| 86 // Include a second call to RegisterAsyncGenericService to make sure that |
| 87 // we get an error in the log, since it is not allowed to have 2 async |
| 88 // generic services |
| 89 builder.RegisterAsyncGenericService(&generic_service_); |
| 90 srv_cq_ = builder.AddCompletionQueue(); |
| 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<Channel> channel = |
| 108 CreateChannel(server_address_.str(), InsecureChannelCredentials()); |
| 109 generic_stub_.reset(new GenericStub(channel)); |
| 110 } |
| 111 |
| 112 void server_ok(int i) { verify_ok(srv_cq_.get(), i, true); } |
| 113 void client_ok(int i) { verify_ok(&cli_cq_, i, true); } |
| 114 void server_fail(int i) { verify_ok(srv_cq_.get(), i, false); } |
| 115 void client_fail(int i) { verify_ok(&cli_cq_, i, false); } |
| 116 |
| 117 void SendRpc(int num_rpcs) { |
| 118 const grpc::string kMethodName("/grpc.cpp.test.util.EchoTestService/Echo"); |
| 119 for (int i = 0; i < num_rpcs; i++) { |
| 120 EchoRequest send_request; |
| 121 EchoRequest recv_request; |
| 122 EchoResponse send_response; |
| 123 EchoResponse recv_response; |
| 124 Status recv_status; |
| 125 |
| 126 ClientContext cli_ctx; |
| 127 GenericServerContext srv_ctx; |
| 128 GenericServerAsyncReaderWriter stream(&srv_ctx); |
| 129 |
| 130 // The string needs to be long enough to test heap-based slice. |
| 131 send_request.set_message("Hello world. Hello world. Hello world."); |
| 132 std::unique_ptr<GenericClientAsyncReaderWriter> call = |
| 133 generic_stub_->Call(&cli_ctx, kMethodName, &cli_cq_, tag(1)); |
| 134 client_ok(1); |
| 135 std::unique_ptr<ByteBuffer> send_buffer = |
| 136 SerializeToByteBuffer(&send_request); |
| 137 call->Write(*send_buffer, tag(2)); |
| 138 client_ok(2); |
| 139 call->WritesDone(tag(3)); |
| 140 client_ok(3); |
| 141 |
| 142 generic_service_.RequestCall(&srv_ctx, &stream, srv_cq_.get(), |
| 143 srv_cq_.get(), tag(4)); |
| 144 |
| 145 verify_ok(srv_cq_.get(), 4, true); |
| 146 EXPECT_EQ(server_host_, srv_ctx.host().substr(0, server_host_.length())); |
| 147 EXPECT_EQ(kMethodName, srv_ctx.method()); |
| 148 ByteBuffer recv_buffer; |
| 149 stream.Read(&recv_buffer, tag(5)); |
| 150 server_ok(5); |
| 151 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_request)); |
| 152 EXPECT_EQ(send_request.message(), recv_request.message()); |
| 153 |
| 154 send_response.set_message(recv_request.message()); |
| 155 send_buffer = SerializeToByteBuffer(&send_response); |
| 156 stream.Write(*send_buffer, tag(6)); |
| 157 server_ok(6); |
| 158 |
| 159 stream.Finish(Status::OK, tag(7)); |
| 160 server_ok(7); |
| 161 |
| 162 recv_buffer.Clear(); |
| 163 call->Read(&recv_buffer, tag(8)); |
| 164 client_ok(8); |
| 165 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_response)); |
| 166 |
| 167 call->Finish(&recv_status, tag(9)); |
| 168 client_ok(9); |
| 169 |
| 170 EXPECT_EQ(send_response.message(), recv_response.message()); |
| 171 EXPECT_TRUE(recv_status.ok()); |
| 172 } |
| 173 } |
| 174 |
| 175 CompletionQueue cli_cq_; |
| 176 std::unique_ptr<ServerCompletionQueue> srv_cq_; |
| 177 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
| 178 std::unique_ptr<grpc::GenericStub> generic_stub_; |
| 179 std::unique_ptr<Server> server_; |
| 180 AsyncGenericService generic_service_; |
| 181 const grpc::string server_host_; |
| 182 std::ostringstream server_address_; |
| 183 }; |
| 184 |
| 185 TEST_F(GenericEnd2endTest, SimpleRpc) { |
| 186 ResetStub(); |
| 187 SendRpc(1); |
| 188 } |
| 189 |
| 190 TEST_F(GenericEnd2endTest, SequentialRpcs) { |
| 191 ResetStub(); |
| 192 SendRpc(10); |
| 193 } |
| 194 |
| 195 // One ping, one pong. |
| 196 TEST_F(GenericEnd2endTest, SimpleBidiStreaming) { |
| 197 ResetStub(); |
| 198 |
| 199 const grpc::string kMethodName( |
| 200 "/grpc.cpp.test.util.EchoTestService/BidiStream"); |
| 201 EchoRequest send_request; |
| 202 EchoRequest recv_request; |
| 203 EchoResponse send_response; |
| 204 EchoResponse recv_response; |
| 205 Status recv_status; |
| 206 ClientContext cli_ctx; |
| 207 GenericServerContext srv_ctx; |
| 208 GenericServerAsyncReaderWriter srv_stream(&srv_ctx); |
| 209 |
| 210 cli_ctx.set_compression_algorithm(GRPC_COMPRESS_GZIP); |
| 211 send_request.set_message("Hello"); |
| 212 std::unique_ptr<GenericClientAsyncReaderWriter> cli_stream = |
| 213 generic_stub_->Call(&cli_ctx, kMethodName, &cli_cq_, tag(1)); |
| 214 client_ok(1); |
| 215 |
| 216 generic_service_.RequestCall(&srv_ctx, &srv_stream, srv_cq_.get(), |
| 217 srv_cq_.get(), tag(2)); |
| 218 |
| 219 verify_ok(srv_cq_.get(), 2, true); |
| 220 EXPECT_EQ(server_host_, srv_ctx.host().substr(0, server_host_.length())); |
| 221 EXPECT_EQ(kMethodName, srv_ctx.method()); |
| 222 |
| 223 std::unique_ptr<ByteBuffer> send_buffer = |
| 224 SerializeToByteBuffer(&send_request); |
| 225 cli_stream->Write(*send_buffer, tag(3)); |
| 226 client_ok(3); |
| 227 |
| 228 ByteBuffer recv_buffer; |
| 229 srv_stream.Read(&recv_buffer, tag(4)); |
| 230 server_ok(4); |
| 231 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_request)); |
| 232 EXPECT_EQ(send_request.message(), recv_request.message()); |
| 233 |
| 234 send_response.set_message(recv_request.message()); |
| 235 send_buffer = SerializeToByteBuffer(&send_response); |
| 236 srv_stream.Write(*send_buffer, tag(5)); |
| 237 server_ok(5); |
| 238 |
| 239 cli_stream->Read(&recv_buffer, tag(6)); |
| 240 client_ok(6); |
| 241 EXPECT_TRUE(ParseFromByteBuffer(&recv_buffer, &recv_response)); |
| 242 EXPECT_EQ(send_response.message(), recv_response.message()); |
| 243 |
| 244 cli_stream->WritesDone(tag(7)); |
| 245 client_ok(7); |
| 246 |
| 247 srv_stream.Read(&recv_buffer, tag(8)); |
| 248 server_fail(8); |
| 249 |
| 250 srv_stream.Finish(Status::OK, tag(9)); |
| 251 server_ok(9); |
| 252 |
| 253 cli_stream->Finish(&recv_status, tag(10)); |
| 254 client_ok(10); |
| 255 |
| 256 EXPECT_EQ(send_response.message(), recv_response.message()); |
| 257 EXPECT_TRUE(recv_status.ok()); |
| 258 } |
| 259 |
| 260 } // namespace |
| 261 } // namespace testing |
| 262 } // namespace grpc |
| 263 |
| 264 int main(int argc, char** argv) { |
| 265 grpc_test_init(argc, argv); |
| 266 ::testing::InitGoogleTest(&argc, argv); |
| 267 return RUN_ALL_TESTS(); |
| 268 } |
OLD | NEW |