unittest.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. Copyright (c) 2020 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <string.h>
  18. #include <string>
  19. #include <gtest/gtest.h>
  20. #include "workflow/WFOperator.h"
  21. #include "workflow/WFFacilities.h"
  22. #include "test_pb.srpc.h"
  23. #include "test_thrift.srpc.h"
  24. using namespace srpc;
  25. using namespace unit;
  26. class ForceShutdown
  27. {
  28. public:
  29. ~ForceShutdown() { google::protobuf::ShutdownProtobufLibrary(); }
  30. } g_holder;
  31. class TestPBServiceImpl : public TestPB::Service
  32. {
  33. public:
  34. void Add(AddRequest *request, AddResponse *response, RPCContext *ctx) override
  35. {
  36. response->set_c(request->a() + request->b());
  37. }
  38. void Substr(SubstrRequest *request, SubstrResponse *response, RPCContext *ctx) override
  39. {
  40. if (request->has_length())
  41. response->set_str(std::string(request->str(), request->idx(), request->length()));
  42. else
  43. response->set_str(std::string(request->str(), request->idx()));
  44. }
  45. };
  46. class TestThriftServiceImpl : public TestThrift::Service
  47. {
  48. public:
  49. int32_t add(const int32_t a, const int32_t b) override
  50. {
  51. return a + b;
  52. }
  53. void substr(std::string& _return, const std::string& str, const int32_t idx, const int32_t length) override
  54. {
  55. if (length < 0)
  56. _return = std::string(str, idx);
  57. else
  58. _return = std::string(str, idx, length);
  59. }
  60. };
  61. template<class SERVER, class CLIENT>
  62. void test_pb(SERVER& server)
  63. {
  64. WFFacilities::WaitGroup wg(1);
  65. RPCClientParams client_params = RPC_CLIENT_PARAMS_DEFAULT;
  66. TestPBServiceImpl impl;
  67. server.add_service(&impl);
  68. EXPECT_TRUE(server.start("127.0.0.1", 9964) == 0) << "server start failed";
  69. client_params.host = "127.0.0.1";
  70. client_params.port = 9964;
  71. CLIENT client(&client_params);
  72. AddRequest req1;
  73. req1.set_a(123);
  74. req1.set_b(456);
  75. client.Add(&req1, [&client, &wg](AddResponse *response, RPCContext *ctx) {
  76. EXPECT_EQ(ctx->get_status_code(), RPCStatusOK);
  77. if (ctx->success())
  78. {
  79. EXPECT_EQ(response->c(), 123 + 456);
  80. SubstrRequest req2;
  81. req2.set_str("hello world!");
  82. req2.set_idx(6);
  83. client.Substr(&req2, [&wg](SubstrResponse *response, RPCContext *ctx) {
  84. EXPECT_EQ(ctx->get_status_code(), RPCStatusOK);
  85. EXPECT_TRUE(response->str() == "world!");
  86. wg.done();
  87. });
  88. }
  89. else
  90. {
  91. wg.done();
  92. }
  93. });
  94. wg.wait();
  95. AddResponse resp1;
  96. RPCSyncContext ctx1;
  97. client.Add(&req1, &resp1, &ctx1);
  98. EXPECT_EQ(ctx1.success, true);
  99. EXPECT_EQ(resp1.c(), 123 + 456);
  100. auto fr = client.async_Add(&req1);
  101. auto res = fr.get();
  102. EXPECT_EQ(res.second.success, true);
  103. EXPECT_EQ(res.first.c(), 123 + 456);
  104. server.stop();
  105. }
  106. template<class SERVER, class CLIENT>
  107. void test_thrift(SERVER& server)
  108. {
  109. WFFacilities::WaitGroup wg(1);
  110. RPCClientParams client_params = RPC_CLIENT_PARAMS_DEFAULT;
  111. TestThriftServiceImpl impl;
  112. server.add_service(&impl);
  113. EXPECT_TRUE(server.start("127.0.0.1", 9965) == 0) << "server start failed";
  114. client_params.host = "127.0.0.1";
  115. client_params.port = 9965;
  116. CLIENT client(&client_params);
  117. TestThrift::addRequest req1;
  118. req1.a = 123;
  119. req1.b = 456;
  120. client.add(&req1, [&client, &wg](TestThrift::addResponse *response, RPCContext *ctx) {
  121. EXPECT_EQ(ctx->get_status_code(), RPCStatusOK);
  122. if (ctx->success())
  123. {
  124. EXPECT_EQ(response->result, 123 + 456);
  125. TestThrift::substrRequest req2;
  126. req2.str = "hello world!";
  127. req2.idx = 6;
  128. req2.length = -1;
  129. client.substr(&req2, [&wg](TestThrift::substrResponse *response, RPCContext *ctx) {
  130. EXPECT_EQ(ctx->get_status_code(), RPCStatusOK);
  131. EXPECT_TRUE(response->result == "world!");
  132. wg.done();
  133. });
  134. }
  135. else
  136. {
  137. wg.done();
  138. }
  139. });
  140. wg.wait();
  141. int32_t c = client.add(123, 456);
  142. EXPECT_EQ(client.thrift_last_sync_success(), true);
  143. EXPECT_EQ(c, 123 + 456);
  144. client.send_add(123, 456);
  145. c = client.recv_add();
  146. EXPECT_EQ(client.thrift_last_sync_success(), true);
  147. EXPECT_EQ(c, 123 + 456);
  148. server.stop();
  149. }
  150. TEST(SRPC, unittest)
  151. {
  152. RPCServerParams server_params = RPC_SERVER_PARAMS_DEFAULT;
  153. SRPCServer server(&server_params);
  154. test_pb<SRPCServer, TestPB::SRPCClient>(server);
  155. test_thrift<SRPCServer, TestThrift::SRPCClient>(server);
  156. }
  157. TEST(SRPCHttp, unittest)
  158. {
  159. RPCServerParams server_params = RPC_SERVER_PARAMS_DEFAULT;
  160. SRPCHttpServer server(&server_params);
  161. test_pb<SRPCHttpServer, TestPB::SRPCHttpClient>(server);
  162. }
  163. TEST(BRPC, unittest)
  164. {
  165. RPCServerParams server_params = RPC_SERVER_PARAMS_DEFAULT;
  166. BRPCServer server(&server_params);
  167. test_pb<BRPCServer, TestPB::BRPCClient>(server);
  168. }
  169. TEST(Thrift, unittest)
  170. {
  171. RPCServerParams server_params = RPC_SERVER_PARAMS_DEFAULT;
  172. ThriftServer server(&server_params);
  173. test_thrift<ThriftServer, TestThrift::ThriftClient>(server);
  174. }
  175. TEST(ThriftHttp, unittest)
  176. {
  177. RPCServerParams server_params = RPC_SERVER_PARAMS_DEFAULT;
  178. ThriftHttpServer server(&server_params);
  179. test_thrift<ThriftHttpServer, TestThrift::ThriftHttpClient>(server);
  180. }
  181. TEST(SRPC_COMPRESS, unittest)
  182. {
  183. WFFacilities::WaitGroup wg(1);
  184. RPCServerParams server_params = RPC_SERVER_PARAMS_DEFAULT;
  185. RPCClientParams client_params = RPC_CLIENT_PARAMS_DEFAULT;
  186. SRPCServer server(&server_params);
  187. TestPBServiceImpl impl;
  188. server.add_service(&impl);
  189. EXPECT_TRUE(server.start("127.0.0.1", 9964) == 0) << "server start failed";
  190. client_params.host = "127.0.0.1";
  191. client_params.port = 9964;
  192. TestPB::SRPCClient client(&client_params);
  193. AddRequest req;
  194. req.set_a(123);
  195. req.set_b(456);
  196. auto&& cb = [](AddResponse *response, RPCContext *ctx) {
  197. EXPECT_EQ(ctx->get_status_code(), RPCStatusOK);
  198. EXPECT_EQ(ctx->success(), true);
  199. EXPECT_EQ(response->c(), 123 + 456);
  200. };
  201. auto *t1 = client.create_Add_task(cb);
  202. auto *t2 = client.create_Add_task(cb);
  203. auto *t3 = client.create_Add_task(cb);
  204. auto *t4 = client.create_Add_task(cb);
  205. t1->set_compress_type(RPCCompressSnappy);
  206. t2->set_compress_type(RPCCompressGzip);
  207. t3->set_compress_type(RPCCompressZlib);
  208. t4->set_compress_type(RPCCompressLz4);
  209. t1->serialize_input(&req);
  210. t2->serialize_input(&req);
  211. t3->serialize_input(&req);
  212. t4->serialize_input(&req);
  213. auto& par = *t1 * t2 * t3 * t4;
  214. par.set_callback([&wg](const ParallelWork *par) {
  215. wg.done();
  216. });
  217. par.start();
  218. wg.wait();
  219. server.stop();
  220. }