3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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"
39 #include <gtest/gtest.h>
41 #define PLUGIN_NAME "TestServerBuilderPlugin"
46 class TestServerBuilderPlugin : public ServerBuilderPlugin {
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;
55 grpc::string name() override { return PLUGIN_NAME; }
57 void InitServer(ServerInitializer* si) override {
58 init_server_is_called_ = true;
59 if (register_service_) {
60 si->RegisterService(service_);
64 void Finish(ServerInitializer* si) override { finish_is_called_ = true; }
66 void ChangeArguments(const grpc::string& name, void* value) override {
67 change_arguments_is_called_ = true;
70 bool has_async_methods() const override {
71 if (register_service_) {
72 return service_->has_async_methods();
77 bool has_sync_methods() const override {
78 if (register_service_) {
79 return service_->has_synchronous_methods();
84 void SetRegisterService() { register_service_ = true; }
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_; }
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_;
98 class InsertPluginServerBuilderOption : public ServerBuilderOption {
100 InsertPluginServerBuilderOption() { register_service_ = false; }
102 void UpdateArguments(ChannelArguments* arg) override {}
105 std::vector<std::unique_ptr<ServerBuilderPlugin>>* plugins) override {
108 std::unique_ptr<TestServerBuilderPlugin> plugin(
109 new TestServerBuilderPlugin());
110 if (register_service_) plugin->SetRegisterService();
111 plugins->emplace_back(std::move(plugin));
114 void SetRegisterService() { register_service_ = true; }
117 bool register_service_;
120 std::unique_ptr<ServerBuilderPlugin> CreateTestServerBuilderPlugin() {
121 return std::unique_ptr<ServerBuilderPlugin>(new TestServerBuilderPlugin());
124 void AddTestServerBuilderPlugin() {
125 static bool already_here = false;
126 if (already_here) return;
128 ::grpc::ServerBuilder::InternalAddPluginFactory(
129 &CreateTestServerBuilderPlugin);
132 // Force AddServerBuilderPlugin() to be called at static initialization time.
133 struct StaticTestPluginInitializer {
134 StaticTestPluginInitializer() { AddTestServerBuilderPlugin(); }
135 } static_plugin_initializer_test_;
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> {
142 ServerBuilderPluginTest() {}
144 void SetUp() override {
145 port_ = grpc_pick_unused_port_or_die();
146 builder_.reset(new ServerBuilder());
149 void InsertPlugin() {
151 // Add ServerBuilder plugin in static initialization
154 // Add ServerBuilder plugin using ServerBuilder::SetOption()
155 builder_->SetOption(std::unique_ptr<ServerBuilderOption>(
156 new InsertPluginServerBuilderOption()));
160 void InsertPluginWithTestService() {
162 // Add ServerBuilder plugin in static initialization
163 auto plugin = CheckPresent();
165 plugin->SetRegisterService();
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));
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());
187 string target = "dns:localhost:" + to_string(port_);
188 channel_ = grpc::CreateChannel(target, InsecureChannelCredentials());
189 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
192 void TearDown() override {
193 auto plugin = CheckPresent();
195 EXPECT_TRUE(plugin->init_server_is_called());
196 EXPECT_TRUE(plugin->finish_is_called());
203 string to_string(const int number) {
204 std::stringstream strs;
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_;
220 TestServerBuilderPlugin* CheckPresent() {
221 auto it = builder_->plugins_.begin();
222 for (; it != builder_->plugins_.end(); it++) {
223 if ((*it)->name() == PLUGIN_NAME) break;
225 if (it != builder_->plugins_.end()) {
226 return static_cast<TestServerBuilderPlugin*>(it->get());
235 while (cq_->Next(&tag, &ok))
240 TEST_P(ServerBuilderPluginTest, PluginWithoutServiceTest) {
245 TEST_P(ServerBuilderPluginTest, PluginWithServiceTest) {
246 InsertPluginWithTestService();
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());
260 INSTANTIATE_TEST_CASE_P(ServerBuilderPluginTest, ServerBuilderPluginTest,
261 ::testing::Values(false, true));
263 } // namespace testing
266 int main(int argc, char** argv) {
267 grpc::testing::TestEnvironment env(argc, argv);
268 ::testing::InitGoogleTest(&argc, argv);
269 return RUN_ALL_TESTS();