diff options
author | Yuchen Zeng <zyc@google.com> | 2016-05-03 12:18:13 -0700 |
---|---|---|
committer | Yuchen Zeng <zyc@google.com> | 2016-05-03 12:18:13 -0700 |
commit | 3b8f3354de5af07ea595713623bcc19cd19d6dfe (patch) | |
tree | 8cda8524a4dae95846d1a6cf08689a216470b7e2 /test/cpp | |
parent | c049035bedad8e013a439bf2934d01e616da4227 (diff) |
Add plugins at the time of static initialization
Diffstat (limited to 'test/cpp')
-rw-r--r-- | test/cpp/end2end/server_builder_plugin_test.cc | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/test/cpp/end2end/server_builder_plugin_test.cc b/test/cpp/end2end/server_builder_plugin_test.cc index 9ed176d29d..0d44999a08 100644 --- a/test/cpp/end2end/server_builder_plugin_test.cc +++ b/test/cpp/end2end/server_builder_plugin_test.cc @@ -115,6 +115,11 @@ class InsertPluginServerBuilderOption : public ServerBuilderOption { void UpdatePlugins( std::map<grpc::string, std::unique_ptr<ServerBuilderPlugin>>* plugins) GRPC_OVERRIDE { + auto it = plugins->begin(); + while (it != plugins->end()) { + plugins->erase(it++); + } + std::unique_ptr<TestServerBuilderPlugin> plugin( new TestServerBuilderPlugin()); if (register_service_) plugin->SetRegisterService(); @@ -127,13 +132,24 @@ class InsertPluginServerBuilderOption : public ServerBuilderOption { bool register_service_; }; -namespace sBPTestServerBuilderPlugin { - std::unique_ptr<ServerBuilderPlugin> CreateTestServerBuilderPlugin() { return std::unique_ptr<ServerBuilderPlugin>(new TestServerBuilderPlugin()); } -} // namespace sBPTestServerBuilderPlugin +void grpc_AddServerBuilderPlugin_reflection() { + static bool already_here = false; + if (already_here) return; + already_here = true; + ::grpc::ServerBuilder::InternalAddPluginFactory( + &CreateTestServerBuilderPlugin); +} + +// Force AddServerBuilderPlugin() to be called at static initialization time. +struct StaticPluginInitializer_reflection { + StaticPluginInitializer_reflection() { + grpc_AddServerBuilderPlugin_reflection(); + } +} static_plugin_initializer_reflection_; class ServerBuilderPluginTest : public ::testing::TestWithParam<bool> { public: @@ -146,8 +162,7 @@ class ServerBuilderPluginTest : public ::testing::TestWithParam<bool> { void InsertPlugin() { if (GetParam()) { - // Add ServerBuilder plugin directly - GRPC_INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin); + // Add ServerBuilder plugin in static initialization EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); } else { // Add ServerBuilder plugin using ServerBuilder::SetOption() @@ -158,8 +173,7 @@ class ServerBuilderPluginTest : public ::testing::TestWithParam<bool> { void InsertPluginWithTestService() { if (GetParam()) { - // Add ServerBuilder plugin directly - GRPC_INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin); + // Add ServerBuilder plugin in static initialization EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); auto plugin = static_cast<TestServerBuilderPlugin*>( builder_->plugins_[PLUGIN_NAME].get()); |