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