blob: d2b83ded67dc274f01d6d8c4c9ba927f47514182 [file] [log] [blame]
Craig Tiller0c233202015-05-06 07:42:43 -07001/*
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 Gao69fe0752015-06-01 14:32:38 -070034#include <mutex>
Craig Tiller0c233202015-05-06 07:42:43 -070035#include <thread>
36
yang-g8c2be9f2015-08-19 16:28:09 -070037#include <grpc++/channel.h>
Craig Tiller0c233202015-05-06 07:42:43 -070038#include <grpc++/client_context.h>
39#include <grpc++/create_channel.h>
Craig Tiller0c233202015-05-06 07:42:43 -070040#include <grpc++/server.h>
41#include <grpc++/server_builder.h>
42#include <grpc++/server_context.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080043#include <grpc/grpc.h>
44#include <grpc/support/thd.h>
45#include <grpc/support/time.h>
Craig Tiller0c233202015-05-06 07:42:43 -070046#include <gtest/gtest.h>
47
Craig Tiller1b4e3302015-12-17 16:35:00 -080048#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
49#include "src/proto/grpc/testing/echo.grpc.pb.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080050#include "test/core/util/port.h"
51#include "test/core/util/test_config.h"
Craig Tiller0c233202015-05-06 07:42:43 -070052
Craig Tiller1b4e3302015-12-17 16:35:00 -080053using grpc::testing::EchoRequest;
54using grpc::testing::EchoResponse;
Craig Tiller0c233202015-05-06 07:42:43 -070055using std::chrono::system_clock;
56
57namespace grpc {
58namespace testing {
59
60namespace {
61
62// When echo_deadline is requested, deadline seen in the ServerContext is set in
63// the response in seconds.
64void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request,
65 EchoResponse* response) {
66 if (request->has_param() && request->param().echo_deadline()) {
Craig Tiller354398f2015-07-13 09:16:03 -070067 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
Craig Tiller0c233202015-05-06 07:42:43 -070068 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 Kuchibhotla5a05f512016-01-13 22:43:20 -080077class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
Craig Tiller0c233202015-05-06 07:42:43 -070078 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 Quintasfeb67f62015-05-20 19:23:25 -070091 gpr_sleep_until(gpr_time_add(
Craig Tiller20b5fe92015-07-06 10:43:50 -070092 gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -070093 gpr_time_from_micros(request->param().client_cancel_after_us(),
94 GPR_TIMESPAN)));
Craig Tiller0c233202015-05-06 07:42:43 -070095 }
Yang Gaoc1a2c312015-06-16 10:59:46 -070096 return Status::CANCELLED;
Craig Tiller0c233202015-05-06 07:42:43 -070097 } else if (request->has_param() &&
98 request->param().server_cancel_after_us()) {
David Garcia Quintasfeb67f62015-05-20 19:23:25 -070099 gpr_sleep_until(gpr_time_add(
Craig Tiller20b5fe92015-07-06 10:43:50 -0700100 gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700101 gpr_time_from_micros(request->param().server_cancel_after_us(),
102 GPR_TIMESPAN)));
Yang Gaoc1a2c312015-06-16 10:59:46 -0700103 return Status::CANCELLED;
Craig Tiller0c233202015-05-06 07:42:43 -0700104 } 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
161class TestServiceImplDupPkg
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800162 : public ::grpc::testing::duplicate::EchoTestService::Service {
Craig Tiller0c233202015-05-06 07:42:43 -0700163 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
171class End2endTest : public ::testing::Test {
172 protected:
Vijay Paie8a7e302015-08-24 10:52:33 -0700173 End2endTest() : kMaxMessageSize_(8192) {}
Craig Tiller0c233202015-05-06 07:42:43 -0700174
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 Tiller0c233202015-05-06 07:42:43 -0700186 server_ = builder.BuildAndStart();
187 }
188
189 void TearDown() GRPC_OVERRIDE { server_->Shutdown(); }
190
191 void ResetStub() {
yang-g730055d2015-08-27 12:29:45 -0700192 std::shared_ptr<Channel> channel =
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700193 CreateChannel(server_address_.str(), InsecureChannelCredentials());
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800194 stub_ = grpc::testing::EchoTestService::NewStub(channel);
Craig Tiller0c233202015-05-06 07:42:43 -0700195 }
196
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800197 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Craig Tiller0c233202015-05-06 07:42:43 -0700198 std::unique_ptr<Server> server_;
199 std::ostringstream server_address_;
200 const int kMaxMessageSize_;
201 TestServiceImpl service_;
202 TestServiceImplDupPkg dup_pkg_service_;
Craig Tiller0c233202015-05-06 07:42:43 -0700203};
204
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800205static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs) {
Craig Tiller0c233202015-05-06 07:42:43 -0700206 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 Gaoc1a2c312015-06-16 10:59:46 -0700214 EXPECT_TRUE(s.ok());
Craig Tiller0c233202015-05-06 07:42:43 -0700215 }
216}
217
218TEST_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
233int main(int argc, char** argv) {
234 grpc_test_init(argc, argv);
235 ::testing::InitGoogleTest(&argc, argv);
236 return RUN_ALL_TESTS();
237}