From a42ec2134117f4a4db994467edcff52e6f347f3d Mon Sep 17 00:00:00 2001 From: Yuchen Zeng Date: Fri, 29 Apr 2016 13:03:06 -0700 Subject: Server builder plugin --- test/cpp/end2end/async_end2end_test.cc | 28 +++ test/cpp/end2end/server_builder_plugin_test.cc | 234 +++++++++++++++++++++++++ 2 files changed, 262 insertions(+) create mode 100644 test/cpp/end2end/server_builder_plugin_test.cc (limited to 'test/cpp/end2end') diff --git a/test/cpp/end2end/async_end2end_test.cc b/test/cpp/end2end/async_end2end_test.cc index 7e4d6046d6..0de6c74c47 100644 --- a/test/cpp/end2end/async_end2end_test.cc +++ b/test/cpp/end2end/async_end2end_test.cc @@ -197,6 +197,28 @@ class Verifier { bool spin_; }; +// This class disables the server builder plugins that may add sync services to +// the server. If there are sync services, UnimplementedRpc test will triger +// the sync unkown rpc routine on the server side, rather than the async one +// that needs to be tested here. +class ServerBuilderSyncPluginDisabler : public ::grpc::ServerBuilderOption { + public: + void UpdateArguments(ChannelArguments* arg) GRPC_OVERRIDE {} + + void UpdatePlugins( + std::map>* plugins) + GRPC_OVERRIDE { + auto plugin = plugins->begin(); + while (plugin != plugins->end()) { + if ((*plugin).second->has_sync_methods()) { + plugins->erase(plugin++); + } else { + plugin++; + } + } + } +}; + class AsyncEnd2endTest : public ::testing::TestWithParam { protected: AsyncEnd2endTest() {} @@ -213,6 +235,12 @@ class AsyncEnd2endTest : public ::testing::TestWithParam { grpc::InsecureServerCredentials()); builder.RegisterService(&service_); cq_ = builder.AddCompletionQueue(); + + // TODO(zyc): make a test option to choose wheather sync plugins should be + // deleted + std::unique_ptr sync_plugin_disabler( + new ServerBuilderSyncPluginDisabler()); + builder.SetOption(move(sync_plugin_disabler)); server_ = builder.BuildAndStart(); gpr_tls_set(&g_is_async_end2end_test, 1); diff --git a/test/cpp/end2end/server_builder_plugin_test.cc b/test/cpp/end2end/server_builder_plugin_test.cc new file mode 100644 index 0000000000..e42bd10832 --- /dev/null +++ b/test/cpp/end2end/server_builder_plugin_test.cc @@ -0,0 +1,234 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "src/proto/grpc/testing/echo.grpc.pb.h" +#include "test/core/util/port.h" +#include "test/core/util/test_config.h" +#include "test/cpp/end2end/test_service_impl.h" + +#define PLUGIN_NAME "TestServerBuilderPlugin" + +namespace grpc { +namespace testing { + +class TestServerBuilderPlugin : public ServerBuilderPlugin { + public: + TestServerBuilderPlugin() : service_(new TestServiceImpl()) { + init_server_is_called_ = false; + finish_is_called_ = false; + change_arguments_is_called_ = false; + } + + grpc::string name() GRPC_OVERRIDE { return PLUGIN_NAME; } + + void InitServer(ServerInitializer* si) GRPC_OVERRIDE { + init_server_is_called_ = true; + if (register_service_) { + si->RegisterService(service_); + } + } + + void Finish(ServerInitializer* si) GRPC_OVERRIDE { finish_is_called_ = true; } + + void ChangeArguments(const grpc::string& name, void* value) GRPC_OVERRIDE { + change_arguments_is_called_ = true; + } + + bool has_async_methods() const GRPC_OVERRIDE { return register_service_; } + + bool has_sync_methods() const GRPC_OVERRIDE { return register_service_; } + + void SetRegisterService() { register_service_ = true; } + + bool init_server_is_called() { return init_server_is_called_; } + bool finish_is_called() { return finish_is_called_; } + bool change_arguments_is_called() { return change_arguments_is_called_; } + + private: + bool init_server_is_called_; + bool finish_is_called_; + bool change_arguments_is_called_; + bool register_service_; + std::shared_ptr service_; +}; + +class InsertPluginServerBuilderOption : public ServerBuilderOption { + public: + InsertPluginServerBuilderOption() { register_service_ = false; } + + void UpdateArguments(ChannelArguments* arg) GRPC_OVERRIDE {} + + void UpdatePlugins( + std::map>* plugins) + GRPC_OVERRIDE { + std::unique_ptr plugin( + new TestServerBuilderPlugin()); + if (register_service_) plugin->SetRegisterService(); + (*plugins)[plugin->name()] = std::move(plugin); + } + + void SetRegisterService() { register_service_ = true; } + + private: + bool register_service_; +}; + +namespace sBPTestServerBuilderPlugin { + +std::unique_ptr CreateTestServerBuilderPlugin() { + return std::unique_ptr(new TestServerBuilderPlugin()); +} + +} // namespace sBPTestServerBuilderPlugin + +class ServerBuilderPluginTest : public ::testing::TestWithParam { + public: + ServerBuilderPluginTest() {} + + void SetUp() GRPC_OVERRIDE { + port_ = grpc_pick_unused_port_or_die(); + builder_.reset(new ServerBuilder()); + } + + void InsertPlugin() { + if (GetParam()) { + // Add ServerBuilder plugin directly + INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin); + EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); + } else { + // Add ServerBuilder plugin using ServerBuilder::SetOption() + builder_->SetOption(std::unique_ptr( + new InsertPluginServerBuilderOption())); + } + } + + void InsertPluginWithTestService() { + if (GetParam()) { + // Add ServerBuilder plugin directly + INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin); + EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); + auto plugin = static_cast( + builder_->plugins_[PLUGIN_NAME].get()); + EXPECT_TRUE(plugin != nullptr); + plugin->SetRegisterService(); + } else { + // Add ServerBuilder plugin using ServerBuilder::SetOption() + std::unique_ptr option( + new InsertPluginServerBuilderOption()); + option->SetRegisterService(); + builder_->SetOption(std::move(option)); + } + } + + void StartServer() { + grpc::string server_address = "localhost:" + to_string(port_); + builder_->AddListeningPort(server_address, InsecureServerCredentials()); + server_ = builder_->BuildAndStart(); + EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); + } + + void ResetStub() { + string target = "dns:localhost:" + to_string(port_); + channel_ = CreateChannel(target, InsecureChannelCredentials()); + stub_ = grpc::testing::EchoTestService::NewStub(channel_); + } + + void TearDown() GRPC_OVERRIDE { + EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); + auto plugin = static_cast( + builder_->plugins_[PLUGIN_NAME].get()); + EXPECT_TRUE(plugin != nullptr); + EXPECT_TRUE(plugin->init_server_is_called()); + EXPECT_TRUE(plugin->finish_is_called()); + } + + string to_string(const int number) { + std::stringstream strs; + strs << number; + return strs.str(); + } + + protected: + std::shared_ptr channel_; + std::unique_ptr builder_; + std::unique_ptr stub_; + std::unique_ptr server_; + TestServiceImpl service_; + int port_; +}; + +TEST_P(ServerBuilderPluginTest, PluginWithoutServiceTest) { + InsertPlugin(); + StartServer(); +} + +TEST_P(ServerBuilderPluginTest, PluginWithServiceTest) { + InsertPluginWithTestService(); + StartServer(); + ResetStub(); + + EchoRequest request; + EchoResponse response; + request.set_message("Hello hello hello hello"); + ClientContext context; + context.set_compression_algorithm(GRPC_COMPRESS_GZIP); + Status s = stub_->Echo(&context, request, &response); + EXPECT_EQ(response.message(), request.message()); + EXPECT_TRUE(s.ok()); +} + +INSTANTIATE_TEST_CASE_P(ServerBuilderPluginTest, ServerBuilderPluginTest, + ::testing::Values(false, true)); + +} // namespace testing +} // namespace grpc + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} -- cgit v1.2.3 From 018bf1a8649e5036dba04343db6e4cd668eb3e24 Mon Sep 17 00:00:00 2001 From: Yuchen Zeng Date: Fri, 29 Apr 2016 13:39:17 -0700 Subject: modified has_async_methods and has_sync_methods of TestServerBuilderPlugin --- test/cpp/end2end/server_builder_plugin_test.cc | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'test/cpp/end2end') diff --git a/test/cpp/end2end/server_builder_plugin_test.cc b/test/cpp/end2end/server_builder_plugin_test.cc index e42bd10832..bebf1d1979 100644 --- a/test/cpp/end2end/server_builder_plugin_test.cc +++ b/test/cpp/end2end/server_builder_plugin_test.cc @@ -78,9 +78,19 @@ class TestServerBuilderPlugin : public ServerBuilderPlugin { change_arguments_is_called_ = true; } - bool has_async_methods() const GRPC_OVERRIDE { return register_service_; } + bool has_async_methods() const GRPC_OVERRIDE { + if (register_service_) { + return service_->has_async_methods(); + } + return false; + } - bool has_sync_methods() const GRPC_OVERRIDE { return register_service_; } + bool has_sync_methods() const GRPC_OVERRIDE { + if (register_service_) { + return service_->has_synchronous_methods(); + } + return false; + } void SetRegisterService() { register_service_ = true; } -- cgit v1.2.3 From c049035bedad8e013a439bf2934d01e616da4227 Mon Sep 17 00:00:00 2001 From: Yuchen Zeng Date: Fri, 29 Apr 2016 14:05:08 -0700 Subject: Add GRPC_ prefix for macros --- include/grpc++/impl/server_builder_plugin.h | 4 ++-- test/cpp/end2end/server_builder_plugin_test.cc | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'test/cpp/end2end') diff --git a/include/grpc++/impl/server_builder_plugin.h b/include/grpc++/impl/server_builder_plugin.h index 2cf1dfdbec..7cf369e346 100644 --- a/include/grpc++/impl/server_builder_plugin.h +++ b/include/grpc++/impl/server_builder_plugin.h @@ -64,12 +64,12 @@ class ServerBuilderPlugin { } // namespace grpc -#define DECLARE_PLUGIN(plugin_name) \ +#define GRPC_DECLARE_PLUGIN(plugin_name) \ namespace sBP##plugin_name { \ extern std::unique_ptr Create##plugin_name(); \ } -#define INIT_PLUGIN(map, plugin_name) \ +#define GRPC_INIT_PLUGIN(map, plugin_name) \ { \ std::unique_ptr plugin = \ sBP##plugin_name::Create##plugin_name(); \ diff --git a/test/cpp/end2end/server_builder_plugin_test.cc b/test/cpp/end2end/server_builder_plugin_test.cc index bebf1d1979..9ed176d29d 100644 --- a/test/cpp/end2end/server_builder_plugin_test.cc +++ b/test/cpp/end2end/server_builder_plugin_test.cc @@ -147,7 +147,7 @@ class ServerBuilderPluginTest : public ::testing::TestWithParam { void InsertPlugin() { if (GetParam()) { // Add ServerBuilder plugin directly - INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin); + GRPC_INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin); EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); } else { // Add ServerBuilder plugin using ServerBuilder::SetOption() @@ -159,7 +159,7 @@ class ServerBuilderPluginTest : public ::testing::TestWithParam { void InsertPluginWithTestService() { if (GetParam()) { // Add ServerBuilder plugin directly - INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin); + GRPC_INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin); EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); auto plugin = static_cast( builder_->plugins_[PLUGIN_NAME].get()); -- cgit v1.2.3 From 3b8f3354de5af07ea595713623bcc19cd19d6dfe Mon Sep 17 00:00:00 2001 From: Yuchen Zeng Date: Tue, 3 May 2016 12:18:13 -0700 Subject: Add plugins at the time of static initialization --- include/grpc++/impl/server_builder_plugin.h | 10 --------- include/grpc++/server_builder.h | 3 +++ src/cpp/server/server_builder.cc | 18 +++++++++++++++++ test/cpp/end2end/server_builder_plugin_test.cc | 28 +++++++++++++++++++------- 4 files changed, 42 insertions(+), 17 deletions(-) (limited to 'test/cpp/end2end') diff --git a/include/grpc++/impl/server_builder_plugin.h b/include/grpc++/impl/server_builder_plugin.h index 7cf369e346..f792c4b321 100644 --- a/include/grpc++/impl/server_builder_plugin.h +++ b/include/grpc++/impl/server_builder_plugin.h @@ -64,16 +64,6 @@ class ServerBuilderPlugin { } // namespace grpc -#define GRPC_DECLARE_PLUGIN(plugin_name) \ - namespace sBP##plugin_name { \ - extern std::unique_ptr Create##plugin_name(); \ - } -#define GRPC_INIT_PLUGIN(map, plugin_name) \ - { \ - std::unique_ptr plugin = \ - sBP##plugin_name::Create##plugin_name(); \ - map[plugin->name()] = std::move(plugin); \ - } #endif // GRPCXX_IMPL_SERVER_BUILDER_PLUGIN_H diff --git a/include/grpc++/server_builder.h b/include/grpc++/server_builder.h index a47b5c71cf..52064b1434 100644 --- a/include/grpc++/server_builder.h +++ b/include/grpc++/server_builder.h @@ -113,6 +113,9 @@ class ServerBuilder { /// Return a running server which is ready for processing calls. std::unique_ptr BuildAndStart(); + static void InternalAddPluginFactory( + std::unique_ptr (*CreatePlugin)()); + private: friend class ::grpc::testing::ServerBuilderPluginTest; diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index 5dc73ed1e4..b6e48efa8d 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -41,9 +41,21 @@ namespace grpc { +static std::vector (*)()>* plugin_list; +static gpr_once once_init_plugin_list = GPR_ONCE_INIT; + +static void do_plugin_list_init(void) { + plugin_list = new std::vector (*)()>(); +} + ServerBuilder::ServerBuilder() : max_message_size_(-1), generic_service_(nullptr) { grpc_compression_options_init(&compression_options_); + gpr_once_init(&once_init_plugin_list, do_plugin_list_init); + for (auto factory : (*plugin_list)) { + std::unique_ptr plugin = factory(); + plugins_[plugin->name()] = std::move(plugin); + } } std::unique_ptr ServerBuilder::AddCompletionQueue() { @@ -156,4 +168,10 @@ std::unique_ptr ServerBuilder::BuildAndStart() { return server; } +void ServerBuilder::InternalAddPluginFactory( + std::unique_ptr (*CreatePlugin)()) { + gpr_once_init(&once_init_plugin_list, do_plugin_list_init); + (*plugin_list).push_back(CreatePlugin); +} + } // namespace grpc 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>* plugins) GRPC_OVERRIDE { + auto it = plugins->begin(); + while (it != plugins->end()) { + plugins->erase(it++); + } + std::unique_ptr plugin( new TestServerBuilderPlugin()); if (register_service_) plugin->SetRegisterService(); @@ -127,13 +132,24 @@ class InsertPluginServerBuilderOption : public ServerBuilderOption { bool register_service_; }; -namespace sBPTestServerBuilderPlugin { - std::unique_ptr CreateTestServerBuilderPlugin() { return std::unique_ptr(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 { public: @@ -146,8 +162,7 @@ class ServerBuilderPluginTest : public ::testing::TestWithParam { 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 { 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( builder_->plugins_[PLUGIN_NAME].get()); -- cgit v1.2.3 From 4c07008610922e15c9f406b89e1bf35f80a9728f Mon Sep 17 00:00:00 2001 From: yang-g Date: Thu, 5 May 2016 23:27:13 -0700 Subject: Add a test for server returning error with debug info in trailer. --- src/proto/grpc/testing/echo_messages.proto | 7 +++++++ test/cpp/end2end/end2end_test.cc | 25 +++++++++++++++++++++++++ test/cpp/end2end/test_service_impl.cc | 8 ++++++++ test/cpp/end2end/test_service_impl.h | 1 + 4 files changed, 41 insertions(+) (limited to 'test/cpp/end2end') diff --git a/src/proto/grpc/testing/echo_messages.proto b/src/proto/grpc/testing/echo_messages.proto index 1be1966f10..b405acf043 100644 --- a/src/proto/grpc/testing/echo_messages.proto +++ b/src/proto/grpc/testing/echo_messages.proto @@ -32,6 +32,12 @@ syntax = "proto3"; package grpc.testing; +// Message to be echoed back serialized in trailer. +message DebugInfo { + repeated string stack_entries = 1; + string detail = 2; +} + message RequestParams { bool echo_deadline = 1; int32 client_cancel_after_us = 2; @@ -43,6 +49,7 @@ message RequestParams { string expected_client_identity = 8; // will force check_auth_context. bool skip_cancelled_check = 9; string expected_transport_security_type = 10; + DebugInfo debug_info = 11; } message EchoRequest { diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index 0c9313f88f..bef1561b09 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -975,6 +975,31 @@ TEST_P(End2endTest, NonExistingService) { EXPECT_EQ("", s.error_message()); } +// Ask the server to send back a serialized proto in trailer. +// This is an example of setting error details. +TEST_P(End2endTest, BinaryTrailerTest) { + ResetStub(); + EchoRequest request; + EchoResponse response; + ClientContext context; + + request.mutable_param()->set_echo_metadata(true); + DebugInfo* info = request.mutable_param()->mutable_debug_info(); + info->add_stack_entries("stack_entry_1"); + info->add_stack_entries("stack_entry_2"); + info->add_stack_entries("stack_entry_3"); + info->set_detail("detailed debug info"); + grpc::string expected_string = info->SerializeAsString(); + request.set_message("Hello"); + + Status s = stub_->Echo(&context, request, &response); + EXPECT_FALSE(s.ok()); + auto trailers = context.GetServerTrailingMetadata(); + EXPECT_EQ(1, trailers.count(kDebugInfoTrailerKey)); + auto iter = trailers.find(kDebugInfoTrailerKey); + EXPECT_EQ(expected_string, iter->second); +} + ////////////////////////////////////////////////////////////////////////// // Test with and without a proxy. class ProxyEnd2endTest : public End2endTest { diff --git a/test/cpp/end2end/test_service_impl.cc b/test/cpp/end2end/test_service_impl.cc index 2f5dd6d49e..cbaee92228 100644 --- a/test/cpp/end2end/test_service_impl.cc +++ b/test/cpp/end2end/test_service_impl.cc @@ -135,6 +135,14 @@ Status TestServiceImpl::Echo(ServerContext* context, const EchoRequest* request, context->AddTrailingMetadata(ToString(iter->first), ToString(iter->second)); } + // Terminate rpc with error and debug info in trailer. + if (request->param().debug_info().stack_entries_size() || + !request->param().debug_info().detail().empty()) { + grpc::string serialized_debug_info = + request->param().debug_info().SerializeAsString(); + context->AddTrailingMetadata(kDebugInfoTrailerKey, serialized_debug_info); + return Status::CANCELLED; + } } if (request->has_param() && (request->param().expected_client_identity().length() > 0 || diff --git a/test/cpp/end2end/test_service_impl.h b/test/cpp/end2end/test_service_impl.h index 1ab6ced9e0..c89f88c900 100644 --- a/test/cpp/end2end/test_service_impl.h +++ b/test/cpp/end2end/test_service_impl.h @@ -47,6 +47,7 @@ namespace testing { const int kNumResponseStreamsMsgs = 3; const char* const kServerCancelAfterReads = "cancel_after_reads"; const char* const kServerTryCancelRequest = "server_try_cancel"; +const char* const kDebugInfoTrailerKey = "debug-info-bin"; typedef enum { DO_NOT_CANCEL = 0, -- cgit v1.2.3 From 080528abb725702c7559c1be058eb8d3ea792fa8 Mon Sep 17 00:00:00 2001 From: yang-g Date: Fri, 6 May 2016 13:12:00 -0700 Subject: Add parsing back to protobuf example --- test/cpp/end2end/end2end_test.cc | 3 +++ 1 file changed, 3 insertions(+) (limited to 'test/cpp/end2end') diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index bef1561b09..40ba0c0b43 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -998,6 +998,9 @@ TEST_P(End2endTest, BinaryTrailerTest) { EXPECT_EQ(1, trailers.count(kDebugInfoTrailerKey)); auto iter = trailers.find(kDebugInfoTrailerKey); EXPECT_EQ(expected_string, iter->second); + // Parse the returned trailer into a DebugInfo proto. + DebugInfo returned_info; + EXPECT_TRUE(returned_info.ParseFromString(ToString(iter->second))); } ////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3 From 7d099a5c907d9ab164416ea875ae07a3074adedb Mon Sep 17 00:00:00 2001 From: Yuchen Zeng Date: Fri, 6 May 2016 13:21:36 -0700 Subject: Fix naming and comment problems --- include/grpc++/impl/server_builder_plugin.h | 2 -- include/grpc++/server_builder.h | 1 + src/cpp/server/server_builder.cc | 10 ++++++---- test/cpp/end2end/server_builder_plugin_test.cc | 18 ++++++++---------- 4 files changed, 15 insertions(+), 16 deletions(-) (limited to 'test/cpp/end2end') diff --git a/include/grpc++/impl/server_builder_plugin.h b/include/grpc++/impl/server_builder_plugin.h index f792c4b321..1e157efa11 100644 --- a/include/grpc++/impl/server_builder_plugin.h +++ b/include/grpc++/impl/server_builder_plugin.h @@ -64,6 +64,4 @@ class ServerBuilderPlugin { } // namespace grpc - - #endif // GRPCXX_IMPL_SERVER_BUILDER_PLUGIN_H diff --git a/include/grpc++/server_builder.h b/include/grpc++/server_builder.h index 52064b1434..ad629521cb 100644 --- a/include/grpc++/server_builder.h +++ b/include/grpc++/server_builder.h @@ -113,6 +113,7 @@ class ServerBuilder { /// Return a running server which is ready for processing calls. std::unique_ptr BuildAndStart(); + /// For internal use only: Register a ServerBuilderPlugin factory function. static void InternalAddPluginFactory( std::unique_ptr (*CreatePlugin)()); diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index b6e48efa8d..9658a56745 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -41,18 +41,20 @@ namespace grpc { -static std::vector (*)()>* plugin_list; +static std::vector (*)()>* + g_plugin_factory_list; static gpr_once once_init_plugin_list = GPR_ONCE_INIT; static void do_plugin_list_init(void) { - plugin_list = new std::vector (*)()>(); + g_plugin_factory_list = + new std::vector (*)()>(); } ServerBuilder::ServerBuilder() : max_message_size_(-1), generic_service_(nullptr) { grpc_compression_options_init(&compression_options_); gpr_once_init(&once_init_plugin_list, do_plugin_list_init); - for (auto factory : (*plugin_list)) { + for (auto factory : (*g_plugin_factory_list)) { std::unique_ptr plugin = factory(); plugins_[plugin->name()] = std::move(plugin); } @@ -171,7 +173,7 @@ std::unique_ptr ServerBuilder::BuildAndStart() { void ServerBuilder::InternalAddPluginFactory( std::unique_ptr (*CreatePlugin)()) { gpr_once_init(&once_init_plugin_list, do_plugin_list_init); - (*plugin_list).push_back(CreatePlugin); + (*g_plugin_factory_list).push_back(CreatePlugin); } } // namespace grpc diff --git a/test/cpp/end2end/server_builder_plugin_test.cc b/test/cpp/end2end/server_builder_plugin_test.cc index 0d44999a08..87e3709d7d 100644 --- a/test/cpp/end2end/server_builder_plugin_test.cc +++ b/test/cpp/end2end/server_builder_plugin_test.cc @@ -115,10 +115,7 @@ class InsertPluginServerBuilderOption : public ServerBuilderOption { void UpdatePlugins( std::map>* plugins) GRPC_OVERRIDE { - auto it = plugins->begin(); - while (it != plugins->end()) { - plugins->erase(it++); - } + plugins->clear(); std::unique_ptr plugin( new TestServerBuilderPlugin()); @@ -136,7 +133,7 @@ std::unique_ptr CreateTestServerBuilderPlugin() { return std::unique_ptr(new TestServerBuilderPlugin()); } -void grpc_AddServerBuilderPlugin_reflection() { +void AddTestServerBuilderPlugin() { static bool already_here = false; if (already_here) return; already_here = true; @@ -145,12 +142,13 @@ void grpc_AddServerBuilderPlugin_reflection() { } // Force AddServerBuilderPlugin() to be called at static initialization time. -struct StaticPluginInitializer_reflection { - StaticPluginInitializer_reflection() { - grpc_AddServerBuilderPlugin_reflection(); - } -} static_plugin_initializer_reflection_; +struct StaticTestPluginInitializer { + StaticTestPluginInitializer() { AddTestServerBuilderPlugin(); } +} static_plugin_initializer_test_; +// When the param boolean is true, the ServerBuilder plugin will be added at the +// time of static initialization. When it's false, the ServerBuilder plugin will +// be added using ServerBuilder::SetOption(). class ServerBuilderPluginTest : public ::testing::TestWithParam { public: ServerBuilderPluginTest() {} -- cgit v1.2.3 From 39e71c33d1a15b85af7c36f08736124a2537896f Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 10 May 2016 10:21:41 -0700 Subject: fix compiling error --- test/cpp/end2end/end2end_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/cpp/end2end') diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index 40ba0c0b43..e3408bff75 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -995,7 +995,7 @@ TEST_P(End2endTest, BinaryTrailerTest) { Status s = stub_->Echo(&context, request, &response); EXPECT_FALSE(s.ok()); auto trailers = context.GetServerTrailingMetadata(); - EXPECT_EQ(1, trailers.count(kDebugInfoTrailerKey)); + EXPECT_EQ(1u, trailers.count(kDebugInfoTrailerKey)); auto iter = trailers.find(kDebugInfoTrailerKey); EXPECT_EQ(expected_string, iter->second); // Parse the returned trailer into a DebugInfo proto. -- cgit v1.2.3