server_builder_plugin_test.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <thread>
  19. #include <gtest/gtest.h>
  20. #include "absl/memory/memory.h"
  21. #include <grpc/grpc.h>
  22. #include <grpcpp/channel.h>
  23. #include <grpcpp/client_context.h>
  24. #include <grpcpp/create_channel.h>
  25. #include <grpcpp/impl/server_builder_option.h>
  26. #include <grpcpp/impl/server_builder_plugin.h>
  27. #include <grpcpp/impl/server_initializer.h>
  28. #include <grpcpp/security/credentials.h>
  29. #include <grpcpp/security/server_credentials.h>
  30. #include <grpcpp/server.h>
  31. #include <grpcpp/server_builder.h>
  32. #include <grpcpp/server_context.h>
  33. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  34. #include "test/core/util/port.h"
  35. #include "test/core/util/test_config.h"
  36. #include "test/cpp/end2end/test_service_impl.h"
  37. #define PLUGIN_NAME "TestServerBuilderPlugin"
  38. namespace grpc {
  39. namespace testing {
  40. class TestServerBuilderPlugin : public ServerBuilderPlugin {
  41. public:
  42. TestServerBuilderPlugin() : service_(new TestServiceImpl()) {
  43. init_server_is_called_ = false;
  44. finish_is_called_ = false;
  45. change_arguments_is_called_ = false;
  46. register_service_ = false;
  47. }
  48. std::string name() override { return PLUGIN_NAME; }
  49. void InitServer(ServerInitializer* si) override {
  50. init_server_is_called_ = true;
  51. if (register_service_) {
  52. si->RegisterService(service_);
  53. }
  54. }
  55. void Finish(ServerInitializer* /*si*/) override { finish_is_called_ = true; }
  56. void ChangeArguments(const std::string& /*name*/, void* /*value*/) override {
  57. change_arguments_is_called_ = true;
  58. }
  59. bool has_async_methods() const override {
  60. if (register_service_) {
  61. return service_->has_async_methods();
  62. }
  63. return false;
  64. }
  65. bool has_sync_methods() const override {
  66. if (register_service_) {
  67. return service_->has_synchronous_methods();
  68. }
  69. return false;
  70. }
  71. void SetRegisterService() { register_service_ = true; }
  72. bool init_server_is_called() { return init_server_is_called_; }
  73. bool finish_is_called() { return finish_is_called_; }
  74. bool change_arguments_is_called() { return change_arguments_is_called_; }
  75. private:
  76. bool init_server_is_called_;
  77. bool finish_is_called_;
  78. bool change_arguments_is_called_;
  79. bool register_service_;
  80. std::shared_ptr<TestServiceImpl> service_;
  81. };
  82. class InsertPluginServerBuilderOption : public ServerBuilderOption {
  83. public:
  84. InsertPluginServerBuilderOption() { register_service_ = false; }
  85. void UpdateArguments(ChannelArguments* /*arg*/) override {}
  86. void UpdatePlugins(
  87. std::vector<std::unique_ptr<ServerBuilderPlugin>>* plugins) override {
  88. plugins->clear();
  89. std::unique_ptr<TestServerBuilderPlugin> plugin(
  90. new TestServerBuilderPlugin());
  91. if (register_service_) plugin->SetRegisterService();
  92. plugins->emplace_back(std::move(plugin));
  93. }
  94. void SetRegisterService() { register_service_ = true; }
  95. private:
  96. bool register_service_;
  97. };
  98. std::unique_ptr<ServerBuilderPlugin> CreateTestServerBuilderPlugin() {
  99. return std::unique_ptr<ServerBuilderPlugin>(new TestServerBuilderPlugin());
  100. }
  101. // Force AddServerBuilderPlugin() to be called at static initialization time.
  102. struct StaticTestPluginInitializer {
  103. StaticTestPluginInitializer() {
  104. grpc::ServerBuilder::InternalAddPluginFactory(
  105. &CreateTestServerBuilderPlugin);
  106. }
  107. } static_plugin_initializer_test_;
  108. // When the param boolean is true, the ServerBuilder plugin will be added at the
  109. // time of static initialization. When it's false, the ServerBuilder plugin will
  110. // be added using ServerBuilder::SetOption().
  111. class ServerBuilderPluginTest : public ::testing::TestWithParam<bool> {
  112. public:
  113. ServerBuilderPluginTest() {}
  114. void SetUp() override {
  115. port_ = grpc_pick_unused_port_or_die();
  116. builder_ = absl::make_unique<ServerBuilder>();
  117. }
  118. void InsertPlugin() {
  119. if (GetParam()) {
  120. // Add ServerBuilder plugin in static initialization
  121. CheckPresent();
  122. } else {
  123. // Add ServerBuilder plugin using ServerBuilder::SetOption()
  124. builder_->SetOption(std::unique_ptr<ServerBuilderOption>(
  125. new InsertPluginServerBuilderOption()));
  126. }
  127. }
  128. void InsertPluginWithTestService() {
  129. if (GetParam()) {
  130. // Add ServerBuilder plugin in static initialization
  131. auto plugin = CheckPresent();
  132. EXPECT_TRUE(plugin);
  133. plugin->SetRegisterService();
  134. } else {
  135. // Add ServerBuilder plugin using ServerBuilder::SetOption()
  136. std::unique_ptr<InsertPluginServerBuilderOption> option(
  137. new InsertPluginServerBuilderOption());
  138. option->SetRegisterService();
  139. builder_->SetOption(std::move(option));
  140. }
  141. }
  142. void StartServer() {
  143. std::string server_address = "localhost:" + to_string(port_);
  144. builder_->AddListeningPort(server_address, InsecureServerCredentials());
  145. // we run some tests without a service, and for those we need to supply a
  146. // frequently polled completion queue
  147. cq_ = builder_->AddCompletionQueue();
  148. cq_thread_ = new std::thread(&ServerBuilderPluginTest::RunCQ, this);
  149. server_ = builder_->BuildAndStart();
  150. EXPECT_TRUE(CheckPresent());
  151. }
  152. void ResetStub() {
  153. string target = "dns:localhost:" + to_string(port_);
  154. channel_ = grpc::CreateChannel(target, InsecureChannelCredentials());
  155. stub_ = grpc::testing::EchoTestService::NewStub(channel_);
  156. }
  157. void TearDown() override {
  158. auto plugin = CheckPresent();
  159. EXPECT_TRUE(plugin);
  160. EXPECT_TRUE(plugin->init_server_is_called());
  161. EXPECT_TRUE(plugin->finish_is_called());
  162. server_->Shutdown();
  163. cq_->Shutdown();
  164. cq_thread_->join();
  165. delete cq_thread_;
  166. }
  167. string to_string(const int number) {
  168. std::stringstream strs;
  169. strs << number;
  170. return strs.str();
  171. }
  172. protected:
  173. std::shared_ptr<Channel> channel_;
  174. std::unique_ptr<ServerBuilder> builder_;
  175. std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
  176. std::unique_ptr<ServerCompletionQueue> cq_;
  177. std::unique_ptr<Server> server_;
  178. std::thread* cq_thread_;
  179. TestServiceImpl service_;
  180. int port_;
  181. private:
  182. TestServerBuilderPlugin* CheckPresent() {
  183. auto it = builder_->plugins_.begin();
  184. for (; it != builder_->plugins_.end(); it++) {
  185. if ((*it)->name() == PLUGIN_NAME) break;
  186. }
  187. if (it != builder_->plugins_.end()) {
  188. return static_cast<TestServerBuilderPlugin*>(it->get());
  189. } else {
  190. return nullptr;
  191. }
  192. }
  193. void RunCQ() {
  194. void* tag;
  195. bool ok;
  196. while (cq_->Next(&tag, &ok)) {
  197. }
  198. }
  199. };
  200. TEST_P(ServerBuilderPluginTest, PluginWithoutServiceTest) {
  201. InsertPlugin();
  202. StartServer();
  203. }
  204. TEST_P(ServerBuilderPluginTest, PluginWithServiceTest) {
  205. InsertPluginWithTestService();
  206. StartServer();
  207. ResetStub();
  208. EchoRequest request;
  209. EchoResponse response;
  210. request.set_message("Hello hello hello hello");
  211. ClientContext context;
  212. context.set_compression_algorithm(GRPC_COMPRESS_GZIP);
  213. Status s = stub_->Echo(&context, request, &response);
  214. EXPECT_EQ(response.message(), request.message());
  215. EXPECT_TRUE(s.ok());
  216. }
  217. INSTANTIATE_TEST_SUITE_P(ServerBuilderPluginTest, ServerBuilderPluginTest,
  218. ::testing::Values(false, true));
  219. } // namespace testing
  220. } // namespace grpc
  221. int main(int argc, char** argv) {
  222. grpc::testing::TestEnvironment env(argc, argv);
  223. ::testing::InitGoogleTest(&argc, argv);
  224. return RUN_ALL_TESTS();
  225. }