end2end_test.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #include <thread>
  34. #include "src/cpp/server/rpc_service_method.h"
  35. #include "test/cpp/util/echo.pb.h"
  36. #include "net/util/netutil.h"
  37. #include <grpc++/channel_interface.h>
  38. #include <grpc++/client_context.h>
  39. #include <grpc++/create_channel.h>
  40. #include <grpc++/server.h>
  41. #include <grpc++/server_builder.h>
  42. #include <grpc++/server_context.h>
  43. #include <grpc++/status.h>
  44. #include <grpc++/stream.h>
  45. #include <gtest/gtest.h>
  46. #include <grpc/grpc.h>
  47. #include <grpc/support/thd.h>
  48. using grpc::cpp::test::util::EchoRequest;
  49. using grpc::cpp::test::util::EchoResponse;
  50. using grpc::cpp::test::util::TestService;
  51. namespace grpc {
  52. class TestServiceImpl : public TestService::Service {
  53. public:
  54. Status Echo(ServerContext* context, const EchoRequest* request,
  55. EchoResponse* response) {
  56. response->set_message(request->message());
  57. return Status::OK;
  58. }
  59. // Unimplemented is left unimplemented to test the returned error.
  60. Status RequestStream(ServerContext* context,
  61. ServerReader<EchoRequest>* reader,
  62. EchoResponse* response) {
  63. EchoRequest request;
  64. response->set_message("");
  65. while (reader->Read(&request)) {
  66. response->mutable_message()->append(request.message());
  67. }
  68. return Status::OK;
  69. }
  70. // Return 3 messages.
  71. // TODO(yangg) make it generic by adding a parameter into EchoRequest
  72. Status ResponseStream(ServerContext* context, const EchoRequest* request,
  73. ServerWriter<EchoResponse>* writer) {
  74. EchoResponse response;
  75. response.set_message(request->message() + "0");
  76. writer->Write(response);
  77. response.set_message(request->message() + "1");
  78. writer->Write(response);
  79. response.set_message(request->message() + "2");
  80. writer->Write(response);
  81. return Status::OK;
  82. }
  83. Status BidiStream(ServerContext* context,
  84. ServerReaderWriter<EchoResponse, EchoRequest>* stream) {
  85. EchoRequest request;
  86. EchoResponse response;
  87. while (stream->Read(&request)) {
  88. gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
  89. response.set_message(request.message());
  90. stream->Write(response);
  91. }
  92. return Status::OK;
  93. }
  94. };
  95. class End2endTest : public ::testing::Test {
  96. protected:
  97. void SetUp() override {
  98. int port = PickUnusedPortOrDie();
  99. server_address_ << "localhost:" << port;
  100. // Setup server
  101. ServerBuilder builder;
  102. builder.AddPort(server_address_.str());
  103. builder.RegisterService(service_.service());
  104. server_ = builder.BuildAndStart();
  105. }
  106. void TearDown() override {
  107. server_->Shutdown();
  108. }
  109. std::unique_ptr<Server> server_;
  110. std::ostringstream server_address_;
  111. TestServiceImpl service_;
  112. };
  113. static void SendRpc(const grpc::string& server_address, int num_rpcs) {
  114. std::shared_ptr<ChannelInterface> channel =
  115. CreateChannel(server_address);
  116. TestService::Stub* stub = TestService::NewStub(channel);
  117. EchoRequest request;
  118. EchoResponse response;
  119. request.set_message("Hello");
  120. for (int i = 0; i < num_rpcs; ++i) {
  121. ClientContext context;
  122. Status s = stub->Echo(&context, request, &response);
  123. EXPECT_EQ(response.message(), request.message());
  124. EXPECT_TRUE(s.IsOk());
  125. }
  126. delete stub;
  127. }
  128. TEST_F(End2endTest, SimpleRpc) {
  129. SendRpc(server_address_.str(), 1);
  130. }
  131. TEST_F(End2endTest, MultipleRpcs) {
  132. vector<std::thread*> threads;
  133. for (int i = 0; i < 10; ++i) {
  134. threads.push_back(new std::thread(SendRpc, server_address_.str(), 10));
  135. }
  136. for (int i = 0; i < 10; ++i) {
  137. threads[i]->join();
  138. delete threads[i];
  139. }
  140. }
  141. // Set a 10us deadline and make sure proper error is returned.
  142. TEST_F(End2endTest, RpcDeadlineExpires) {
  143. std::shared_ptr<ChannelInterface> channel =
  144. CreateChannel(server_address_.str());
  145. TestService::Stub* stub = TestService::NewStub(channel);
  146. EchoRequest request;
  147. EchoResponse response;
  148. request.set_message("Hello");
  149. ClientContext context;
  150. std::chrono::system_clock::time_point deadline =
  151. std::chrono::system_clock::now() + std::chrono::microseconds(10);
  152. context.set_absolute_deadline(deadline);
  153. Status s = stub->Echo(&context, request, &response);
  154. // TODO(yangg) use correct error code when b/18793983 is fixed.
  155. // EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.code());
  156. EXPECT_EQ(StatusCode::CANCELLED, s.code());
  157. delete stub;
  158. }
  159. TEST_F(End2endTest, UnimplementedRpc) {
  160. std::shared_ptr<ChannelInterface> channel =
  161. CreateChannel(server_address_.str());
  162. TestService::Stub* stub = TestService::NewStub(channel);
  163. EchoRequest request;
  164. EchoResponse response;
  165. request.set_message("Hello");
  166. ClientContext context;
  167. Status s = stub->Unimplemented(&context, request, &response);
  168. EXPECT_FALSE(s.IsOk());
  169. EXPECT_EQ(s.code(), grpc::StatusCode::UNIMPLEMENTED);
  170. EXPECT_EQ(s.details(), "");
  171. EXPECT_EQ(response.message(), "");
  172. delete stub;
  173. }
  174. TEST_F(End2endTest, RequestStreamOneRequest) {
  175. std::shared_ptr<ChannelInterface> channel =
  176. CreateChannel(server_address_.str());
  177. TestService::Stub* stub = TestService::NewStub(channel);
  178. EchoRequest request;
  179. EchoResponse response;
  180. ClientContext context;
  181. ClientWriter<EchoRequest>* stream = stub->RequestStream(&context, &response);
  182. request.set_message("hello");
  183. EXPECT_TRUE(stream->Write(request));
  184. stream->WritesDone();
  185. Status s = stream->Wait();
  186. EXPECT_EQ(response.message(), request.message());
  187. EXPECT_TRUE(s.IsOk());
  188. delete stream;
  189. delete stub;
  190. }
  191. TEST_F(End2endTest, RequestStreamTwoRequests) {
  192. std::shared_ptr<ChannelInterface> channel =
  193. CreateChannel(server_address_.str());
  194. TestService::Stub* stub = TestService::NewStub(channel);
  195. EchoRequest request;
  196. EchoResponse response;
  197. ClientContext context;
  198. ClientWriter<EchoRequest>* stream = stub->RequestStream(&context, &response);
  199. request.set_message("hello");
  200. EXPECT_TRUE(stream->Write(request));
  201. EXPECT_TRUE(stream->Write(request));
  202. stream->WritesDone();
  203. Status s = stream->Wait();
  204. EXPECT_EQ(response.message(), "hellohello");
  205. EXPECT_TRUE(s.IsOk());
  206. delete stream;
  207. delete stub;
  208. }
  209. TEST_F(End2endTest, ResponseStream) {
  210. std::shared_ptr<ChannelInterface> channel =
  211. CreateChannel(server_address_.str());
  212. TestService::Stub* stub = TestService::NewStub(channel);
  213. EchoRequest request;
  214. EchoResponse response;
  215. ClientContext context;
  216. request.set_message("hello");
  217. ClientReader<EchoResponse>* stream = stub->ResponseStream(&context, &request);
  218. EXPECT_TRUE(stream->Read(&response));
  219. EXPECT_EQ(response.message(), request.message() + "0");
  220. EXPECT_TRUE(stream->Read(&response));
  221. EXPECT_EQ(response.message(), request.message() + "1");
  222. EXPECT_TRUE(stream->Read(&response));
  223. EXPECT_EQ(response.message(), request.message() + "2");
  224. EXPECT_FALSE(stream->Read(&response));
  225. Status s = stream->Wait();
  226. EXPECT_TRUE(s.IsOk());
  227. delete stream;
  228. delete stub;
  229. }
  230. TEST_F(End2endTest, BidiStream) {
  231. std::shared_ptr<ChannelInterface> channel =
  232. CreateChannel(server_address_.str());
  233. TestService::Stub* stub = TestService::NewStub(channel);
  234. EchoRequest request;
  235. EchoResponse response;
  236. ClientContext context;
  237. grpc::string msg("hello");
  238. ClientReaderWriter<EchoRequest, EchoResponse>* stream =
  239. stub->BidiStream(&context);
  240. request.set_message(msg + "0");
  241. EXPECT_TRUE(stream->Write(request));
  242. EXPECT_TRUE(stream->Read(&response));
  243. EXPECT_EQ(response.message(), request.message());
  244. request.set_message(msg + "1");
  245. EXPECT_TRUE(stream->Write(request));
  246. EXPECT_TRUE(stream->Read(&response));
  247. EXPECT_EQ(response.message(), request.message());
  248. request.set_message(msg + "2");
  249. EXPECT_TRUE(stream->Write(request));
  250. EXPECT_TRUE(stream->Read(&response));
  251. EXPECT_EQ(response.message(), request.message());
  252. stream->WritesDone();
  253. EXPECT_FALSE(stream->Read(&response));
  254. Status s = stream->Wait();
  255. EXPECT_TRUE(s.IsOk());
  256. delete stream;
  257. delete stub;
  258. }
  259. } // namespace grpc
  260. int main(int argc, char** argv) {
  261. grpc_init();
  262. ::testing::InitGoogleTest(&argc, argv);
  263. int result = RUN_ALL_TESTS();
  264. grpc_shutdown();
  265. return result;
  266. }