Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / test / cpp / end2end / server_builder_plugin_test.cc
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
19 #include <thread>
20
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
34 #include "src/proto/grpc/testing/echo.grpc.pb.h"
35 #include "test/core/util/port.h"
36 #include "test/core/util/test_config.h"
37 #include "test/cpp/end2end/test_service_impl.h"
38
39 #include <gtest/gtest.h>
40
41 #define PLUGIN_NAME "TestServerBuilderPlugin"
42
43 namespace grpc {
44 namespace testing {
45
46 class TestServerBuilderPlugin : public ServerBuilderPlugin {
47  public:
48   TestServerBuilderPlugin() : service_(new TestServiceImpl()) {
49     init_server_is_called_ = false;
50     finish_is_called_ = false;
51     change_arguments_is_called_ = false;
52     register_service_ = false;
53   }
54
55   grpc::string name() override { return PLUGIN_NAME; }
56
57   void InitServer(ServerInitializer* si) override {
58     init_server_is_called_ = true;
59     if (register_service_) {
60       si->RegisterService(service_);
61     }
62   }
63
64   void Finish(ServerInitializer* si) override { finish_is_called_ = true; }
65
66   void ChangeArguments(const grpc::string& name, void* value) override {
67     change_arguments_is_called_ = true;
68   }
69
70   bool has_async_methods() const override {
71     if (register_service_) {
72       return service_->has_async_methods();
73     }
74     return false;
75   }
76
77   bool has_sync_methods() const override {
78     if (register_service_) {
79       return service_->has_synchronous_methods();
80     }
81     return false;
82   }
83
84   void SetRegisterService() { register_service_ = true; }
85
86   bool init_server_is_called() { return init_server_is_called_; }
87   bool finish_is_called() { return finish_is_called_; }
88   bool change_arguments_is_called() { return change_arguments_is_called_; }
89
90  private:
91   bool init_server_is_called_;
92   bool finish_is_called_;
93   bool change_arguments_is_called_;
94   bool register_service_;
95   std::shared_ptr<TestServiceImpl> service_;
96 };
97
98 class InsertPluginServerBuilderOption : public ServerBuilderOption {
99  public:
100   InsertPluginServerBuilderOption() { register_service_ = false; }
101
102   void UpdateArguments(ChannelArguments* arg) override {}
103
104   void UpdatePlugins(
105       std::vector<std::unique_ptr<ServerBuilderPlugin>>* plugins) override {
106     plugins->clear();
107
108     std::unique_ptr<TestServerBuilderPlugin> plugin(
109         new TestServerBuilderPlugin());
110     if (register_service_) plugin->SetRegisterService();
111     plugins->emplace_back(std::move(plugin));
112   }
113
114   void SetRegisterService() { register_service_ = true; }
115
116  private:
117   bool register_service_;
118 };
119
120 std::unique_ptr<ServerBuilderPlugin> CreateTestServerBuilderPlugin() {
121   return std::unique_ptr<ServerBuilderPlugin>(new TestServerBuilderPlugin());
122 }
123
124 void AddTestServerBuilderPlugin() {
125   static bool already_here = false;
126   if (already_here) return;
127   already_here = true;
128   ::grpc::ServerBuilder::InternalAddPluginFactory(
129       &CreateTestServerBuilderPlugin);
130 }
131
132 // Force AddServerBuilderPlugin() to be called at static initialization time.
133 struct StaticTestPluginInitializer {
134   StaticTestPluginInitializer() { AddTestServerBuilderPlugin(); }
135 } static_plugin_initializer_test_;
136
137 // When the param boolean is true, the ServerBuilder plugin will be added at the
138 // time of static initialization. When it's false, the ServerBuilder plugin will
139 // be added using ServerBuilder::SetOption().
140 class ServerBuilderPluginTest : public ::testing::TestWithParam<bool> {
141  public:
142   ServerBuilderPluginTest() {}
143
144   void SetUp() override {
145     port_ = grpc_pick_unused_port_or_die();
146     builder_.reset(new ServerBuilder());
147   }
148
149   void InsertPlugin() {
150     if (GetParam()) {
151       // Add ServerBuilder plugin in static initialization
152       CheckPresent();
153     } else {
154       // Add ServerBuilder plugin using ServerBuilder::SetOption()
155       builder_->SetOption(std::unique_ptr<ServerBuilderOption>(
156           new InsertPluginServerBuilderOption()));
157     }
158   }
159
160   void InsertPluginWithTestService() {
161     if (GetParam()) {
162       // Add ServerBuilder plugin in static initialization
163       auto plugin = CheckPresent();
164       EXPECT_TRUE(plugin);
165       plugin->SetRegisterService();
166     } else {
167       // Add ServerBuilder plugin using ServerBuilder::SetOption()
168       std::unique_ptr<InsertPluginServerBuilderOption> option(
169           new InsertPluginServerBuilderOption());
170       option->SetRegisterService();
171       builder_->SetOption(std::move(option));
172     }
173   }
174
175   void StartServer() {
176     grpc::string server_address = "localhost:" + to_string(port_);
177     builder_->AddListeningPort(server_address, InsecureServerCredentials());
178     // we run some tests without a service, and for those we need to supply a
179     // frequently polled completion queue
180     cq_ = builder_->AddCompletionQueue();
181     cq_thread_ = new std::thread(&ServerBuilderPluginTest::RunCQ, this);
182     server_ = builder_->BuildAndStart();
183     EXPECT_TRUE(CheckPresent());
184   }
185
186   void ResetStub() {
187     string target = "dns:localhost:" + to_string(port_);
188     channel_ = grpc::CreateChannel(target, InsecureChannelCredentials());
189     stub_ = grpc::testing::EchoTestService::NewStub(channel_);
190   }
191
192   void TearDown() override {
193     auto plugin = CheckPresent();
194     EXPECT_TRUE(plugin);
195     EXPECT_TRUE(plugin->init_server_is_called());
196     EXPECT_TRUE(plugin->finish_is_called());
197     server_->Shutdown();
198     cq_->Shutdown();
199     cq_thread_->join();
200     delete cq_thread_;
201   }
202
203   string to_string(const int number) {
204     std::stringstream strs;
205     strs << number;
206     return strs.str();
207   }
208
209  protected:
210   std::shared_ptr<Channel> channel_;
211   std::unique_ptr<ServerBuilder> builder_;
212   std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
213   std::unique_ptr<ServerCompletionQueue> cq_;
214   std::unique_ptr<Server> server_;
215   std::thread* cq_thread_;
216   TestServiceImpl service_;
217   int port_;
218
219  private:
220   TestServerBuilderPlugin* CheckPresent() {
221     auto it = builder_->plugins_.begin();
222     for (; it != builder_->plugins_.end(); it++) {
223       if ((*it)->name() == PLUGIN_NAME) break;
224     }
225     if (it != builder_->plugins_.end()) {
226       return static_cast<TestServerBuilderPlugin*>(it->get());
227     } else {
228       return nullptr;
229     }
230   }
231
232   void RunCQ() {
233     void* tag;
234     bool ok;
235     while (cq_->Next(&tag, &ok))
236       ;
237   }
238 };
239
240 TEST_P(ServerBuilderPluginTest, PluginWithoutServiceTest) {
241   InsertPlugin();
242   StartServer();
243 }
244
245 TEST_P(ServerBuilderPluginTest, PluginWithServiceTest) {
246   InsertPluginWithTestService();
247   StartServer();
248   ResetStub();
249
250   EchoRequest request;
251   EchoResponse response;
252   request.set_message("Hello hello hello hello");
253   ClientContext context;
254   context.set_compression_algorithm(GRPC_COMPRESS_GZIP);
255   Status s = stub_->Echo(&context, request, &response);
256   EXPECT_EQ(response.message(), request.message());
257   EXPECT_TRUE(s.ok());
258 }
259
260 INSTANTIATE_TEST_CASE_P(ServerBuilderPluginTest, ServerBuilderPluginTest,
261                         ::testing::Values(false, true));
262
263 }  // namespace testing
264 }  // namespace grpc
265
266 int main(int argc, char** argv) {
267   grpc::testing::TestEnvironment env(argc, argv);
268   ::testing::InitGoogleTest(&argc, argv);
269   return RUN_ALL_TESTS();
270 }