Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -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 | |
Yang Gao | 69fe075 | 2015-06-01 14:32:38 -0700 | [diff] [blame] | 34 | #include <mutex> |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 35 | #include <thread> |
| 36 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 37 | #include <grpc++/channel.h> |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 38 | #include <grpc++/client_context.h> |
| 39 | #include <grpc++/create_channel.h> |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [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 | 0c23320 | 2015-05-06 07:42:43 -0700 | [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" |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 52 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 53 | using grpc::testing::EchoRequest; |
| 54 | using grpc::testing::EchoResponse; |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 55 | using std::chrono::system_clock; |
| 56 | |
| 57 | namespace grpc { |
| 58 | namespace testing { |
| 59 | |
| 60 | namespace { |
| 61 | |
| 62 | // When echo_deadline is requested, deadline seen in the ServerContext is set in |
| 63 | // the response in seconds. |
| 64 | void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request, |
| 65 | EchoResponse* response) { |
| 66 | if (request->has_param() && request->param().echo_deadline()) { |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 67 | gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 68 | if (context->deadline() != system_clock::time_point::max()) { |
| 69 | Timepoint2Timespec(context->deadline(), &deadline); |
| 70 | } |
| 71 | response->mutable_param()->set_request_deadline(deadline.tv_sec); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | } // namespace |
| 76 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 77 | class TestServiceImpl : public ::grpc::testing::EchoTestService::Service { |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 78 | public: |
| 79 | TestServiceImpl() : signal_client_(false) {} |
| 80 | |
| 81 | Status Echo(ServerContext* context, const EchoRequest* request, |
| 82 | EchoResponse* response) GRPC_OVERRIDE { |
| 83 | response->set_message(request->message()); |
| 84 | MaybeEchoDeadline(context, request, response); |
| 85 | if (request->has_param() && request->param().client_cancel_after_us()) { |
| 86 | { |
| 87 | std::unique_lock<std::mutex> lock(mu_); |
| 88 | signal_client_ = true; |
| 89 | } |
| 90 | while (!context->IsCancelled()) { |
David Garcia Quintas | feb67f6 | 2015-05-20 19:23:25 -0700 | [diff] [blame] | 91 | gpr_sleep_until(gpr_time_add( |
Craig Tiller | 20b5fe9 | 2015-07-06 10:43:50 -0700 | [diff] [blame] | 92 | gpr_now(GPR_CLOCK_REALTIME), |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 93 | gpr_time_from_micros(request->param().client_cancel_after_us(), |
| 94 | GPR_TIMESPAN))); |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 95 | } |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 96 | return Status::CANCELLED; |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 97 | } else if (request->has_param() && |
| 98 | request->param().server_cancel_after_us()) { |
David Garcia Quintas | feb67f6 | 2015-05-20 19:23:25 -0700 | [diff] [blame] | 99 | gpr_sleep_until(gpr_time_add( |
Craig Tiller | 20b5fe9 | 2015-07-06 10:43:50 -0700 | [diff] [blame] | 100 | gpr_now(GPR_CLOCK_REALTIME), |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 101 | gpr_time_from_micros(request->param().server_cancel_after_us(), |
| 102 | GPR_TIMESPAN))); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 103 | return Status::CANCELLED; |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 104 | } else { |
| 105 | EXPECT_FALSE(context->IsCancelled()); |
| 106 | } |
| 107 | return Status::OK; |
| 108 | } |
| 109 | |
| 110 | // Unimplemented is left unimplemented to test the returned error. |
| 111 | |
| 112 | Status RequestStream(ServerContext* context, |
| 113 | ServerReader<EchoRequest>* reader, |
| 114 | EchoResponse* response) GRPC_OVERRIDE { |
| 115 | EchoRequest request; |
| 116 | response->set_message(""); |
| 117 | while (reader->Read(&request)) { |
| 118 | response->mutable_message()->append(request.message()); |
| 119 | } |
| 120 | return Status::OK; |
| 121 | } |
| 122 | |
| 123 | // Return 3 messages. |
| 124 | // TODO(yangg) make it generic by adding a parameter into EchoRequest |
| 125 | Status ResponseStream(ServerContext* context, const EchoRequest* request, |
| 126 | ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE { |
| 127 | EchoResponse response; |
| 128 | response.set_message(request->message() + "0"); |
| 129 | writer->Write(response); |
| 130 | response.set_message(request->message() + "1"); |
| 131 | writer->Write(response); |
| 132 | response.set_message(request->message() + "2"); |
| 133 | writer->Write(response); |
| 134 | |
| 135 | return Status::OK; |
| 136 | } |
| 137 | |
| 138 | Status BidiStream(ServerContext* context, |
| 139 | ServerReaderWriter<EchoResponse, EchoRequest>* stream) |
| 140 | GRPC_OVERRIDE { |
| 141 | EchoRequest request; |
| 142 | EchoResponse response; |
| 143 | while (stream->Read(&request)) { |
| 144 | gpr_log(GPR_INFO, "recv msg %s", request.message().c_str()); |
| 145 | response.set_message(request.message()); |
| 146 | stream->Write(response); |
| 147 | } |
| 148 | return Status::OK; |
| 149 | } |
| 150 | |
| 151 | bool signal_client() { |
| 152 | std::unique_lock<std::mutex> lock(mu_); |
| 153 | return signal_client_; |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | bool signal_client_; |
| 158 | std::mutex mu_; |
| 159 | }; |
| 160 | |
| 161 | class TestServiceImplDupPkg |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 162 | : public ::grpc::testing::duplicate::EchoTestService::Service { |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 163 | public: |
| 164 | Status Echo(ServerContext* context, const EchoRequest* request, |
| 165 | EchoResponse* response) GRPC_OVERRIDE { |
| 166 | response->set_message("no package"); |
| 167 | return Status::OK; |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | class End2endTest : public ::testing::Test { |
| 172 | protected: |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 173 | End2endTest() : kMaxMessageSize_(8192) {} |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 174 | |
| 175 | void SetUp() GRPC_OVERRIDE { |
| 176 | int port = grpc_pick_unused_port_or_die(); |
| 177 | server_address_ << "localhost:" << port; |
| 178 | // Setup server |
| 179 | ServerBuilder builder; |
| 180 | builder.AddListeningPort(server_address_.str(), |
| 181 | InsecureServerCredentials()); |
| 182 | builder.RegisterService(&service_); |
| 183 | builder.SetMaxMessageSize( |
| 184 | kMaxMessageSize_); // For testing max message size. |
| 185 | builder.RegisterService(&dup_pkg_service_); |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 186 | server_ = builder.BuildAndStart(); |
| 187 | } |
| 188 | |
| 189 | void TearDown() GRPC_OVERRIDE { server_->Shutdown(); } |
| 190 | |
| 191 | void ResetStub() { |
yang-g | 730055d | 2015-08-27 12:29:45 -0700 | [diff] [blame] | 192 | std::shared_ptr<Channel> channel = |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 193 | CreateChannel(server_address_.str(), InsecureChannelCredentials()); |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 194 | stub_ = grpc::testing::EchoTestService::NewStub(channel); |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 197 | std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 198 | std::unique_ptr<Server> server_; |
| 199 | std::ostringstream server_address_; |
| 200 | const int kMaxMessageSize_; |
| 201 | TestServiceImpl service_; |
| 202 | TestServiceImplDupPkg dup_pkg_service_; |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 205 | static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs) { |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 206 | EchoRequest request; |
| 207 | EchoResponse response; |
| 208 | request.set_message("Hello"); |
| 209 | |
| 210 | for (int i = 0; i < num_rpcs; ++i) { |
| 211 | ClientContext context; |
| 212 | Status s = stub->Echo(&context, request, &response); |
| 213 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 214 | EXPECT_TRUE(s.ok()); |
Craig Tiller | 0c23320 | 2015-05-06 07:42:43 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | TEST_F(End2endTest, ThreadStress) { |
| 219 | ResetStub(); |
| 220 | std::vector<std::thread*> threads; |
| 221 | for (int i = 0; i < 100; ++i) { |
| 222 | threads.push_back(new std::thread(SendRpc, stub_.get(), 1000)); |
| 223 | } |
| 224 | for (int i = 0; i < 100; ++i) { |
| 225 | threads[i]->join(); |
| 226 | delete threads[i]; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | } // namespace testing |
| 231 | } // namespace grpc |
| 232 | |
| 233 | int main(int argc, char** argv) { |
| 234 | grpc_test_init(argc, argv); |
| 235 | ::testing::InitGoogleTest(&argc, argv); |
| 236 | return RUN_ALL_TESTS(); |
| 237 | } |